Skip to content

Commit

Permalink
Implemented DeleteAllOperationsCommand.
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWittmann committed Jan 6, 2025
1 parent 2ea9411 commit c443ea4
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<maven.compiler.target>11</maven.compiler.target>

<!-- Node Versions -->
<version.node-js>16.16.0</version.node-js>
<version.node-js>20.18.1</version.node-js>

<!-- Dependency Versions -->
<version.com.fasterxml.jackson>2.12.5</version.com.fasterxml.jackson>
Expand Down
28 changes: 17 additions & 11 deletions src/main/java/io/apicurio/datamodels/cmd/CommandFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.apicurio.datamodels.cmd.commands.DeleteAllChildSchemasCommand;
import io.apicurio.datamodels.cmd.commands.DeleteAllExamplesCommand;
import io.apicurio.datamodels.cmd.commands.DeleteAllHeadersCommand;
import io.apicurio.datamodels.cmd.commands.DeleteAllOperationsCommand;
import io.apicurio.datamodels.cmd.commands.DeleteContactCommand;
import io.apicurio.datamodels.cmd.commands.DeleteExtensionCommand;
import io.apicurio.datamodels.cmd.commands.DeleteLicenseCommand;
Expand All @@ -22,6 +23,7 @@
import io.apicurio.datamodels.models.openapi.OpenApiHeader;
import io.apicurio.datamodels.models.openapi.OpenApiMediaType;
import io.apicurio.datamodels.models.openapi.OpenApiParameter;
import io.apicurio.datamodels.models.openapi.OpenApiPathItem;
import io.apicurio.datamodels.models.openapi.OpenApiResponse;
import io.apicurio.datamodels.util.CommandUtil;

Expand Down Expand Up @@ -55,48 +57,52 @@ public static ICommand createChangeVersionCommand(String newVersion) {
return new ChangeVersionCommand(newVersion);
}

public static final ICommand createChangeContactCommand(String name, String email, String url) {
public static ICommand createChangeContactCommand(String name, String email, String url) {
return new ChangeContactCommand(name, email, url);
}

public static final ICommand createChangeLicenseCommand(String name, String url) {
public static ICommand createChangeLicenseCommand(String name, String url) {
return new ChangeLicenseCommand(name, url);
}

public static final ICommand createDeleteLicenseCommand(Info info) {
public static ICommand createDeleteLicenseCommand(Info info) {
return new DeleteLicenseCommand(info);
}

public static final ICommand createDeleteContactCommand(Info info) {
public static ICommand createDeleteContactCommand(Info info) {
return new DeleteContactCommand(info);
}

public static final ICommand createDeleteExtensionCommand(Extensible parent, String extensionName) {
public static ICommand createDeleteExtensionCommand(Extensible parent, String extensionName) {
return new DeleteExtensionCommand(parent, extensionName);
}

public static final ICommand createDeleteMediaTypeCommand(OpenApiMediaType mediaType) {
public static ICommand createDeleteMediaTypeCommand(OpenApiMediaType mediaType) {
return new DeleteMediaTypeCommand(mediaType);
}

public static final ICommand createDeleteAllChildSchemasCommand(Schema parent, String type) {
public static ICommand createDeleteAllChildSchemasCommand(Schema parent, String type) {
return new DeleteAllChildSchemasCommand(parent, type);
}

public static final ICommand createDeleteAllMediaTypeExamplesCommand(OpenApiMediaType mediaType) {
public static ICommand createDeleteAllMediaTypeExamplesCommand(OpenApiMediaType mediaType) {
return new DeleteAllExamplesCommand(mediaType);
}

public static final ICommand createDeleteAllParameterExamplesCommand(OpenApiParameter parameter) {
public static ICommand createDeleteAllParameterExamplesCommand(OpenApiParameter parameter) {
return new DeleteAllExamplesCommand(parameter);
}

public static final ICommand createDeleteAllHeaderExamplesCommand(OpenApiHeader header) {
public static ICommand createDeleteAllHeaderExamplesCommand(OpenApiHeader header) {
return new DeleteAllExamplesCommand(header);
}

public static final ICommand createDeleteAllHeadersCommand(OpenApiResponse header) {
public static ICommand createDeleteAllHeadersCommand(OpenApiResponse header) {
return new DeleteAllHeadersCommand(header);
}

public static ICommand createDeleteAllPathItemOperationsCommand(OpenApiPathItem pathItem) {
return new DeleteAllOperationsCommand(pathItem);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.apicurio.datamodels.cmd.commands;

import io.apicurio.datamodels.Library;
import io.apicurio.datamodels.cmd.AbstractCommand;
import io.apicurio.datamodels.models.Document;
import io.apicurio.datamodels.models.Node;
Expand Down Expand Up @@ -36,7 +35,7 @@ public ChangePropertyCommand() {
public ChangePropertyCommand(Node node, String property, T newValue) {
super();
if (NodeUtil.isDefined(node)) {
this._nodePath = Library.createNodePath(node);
this._nodePath = NodePathUtil.createNodePath(node);
}
this._property = property;
this._newValue = newValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public DeleteAllChildSchemasCommand() {
}

public DeleteAllChildSchemasCommand(Schema parent, String type) {
this._schemaPath = Library.createNodePath((Node) parent);
this._schemaPath = NodePathUtil.createNodePath((Node) parent);
this._childSchemaType = type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public DeleteAllExamplesCommand() {
}

public DeleteAllExamplesCommand(OpenApiMediaType parent) {
this._parentPath = Library.createNodePath(parent);
this._parentPath = NodePathUtil.createNodePath(parent);
}

public DeleteAllExamplesCommand(OpenApiParameter parent) {
this._parentPath = Library.createNodePath(parent);
this._parentPath = NodePathUtil.createNodePath(parent);
}

public DeleteAllExamplesCommand(OpenApiHeader parent) {
this._parentPath = Library.createNodePath(parent);
this._parentPath = NodePathUtil.createNodePath(parent);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.apicurio.datamodels.cmd.commands;

import com.fasterxml.jackson.databind.JsonNode;
import io.apicurio.datamodels.Library;
import io.apicurio.datamodels.cmd.AbstractCommand;
import io.apicurio.datamodels.models.Document;
import io.apicurio.datamodels.models.Extensible;
Expand All @@ -27,7 +26,7 @@ public DeleteAllExtensionsCommand() {
}

public DeleteAllExtensionsCommand(Extensible parent) {
this._parentPath = Library.createNodePath((Node) parent);
this._parentPath = NodePathUtil.createNodePath((Node) parent);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public DeleteAllHeadersCommand() {
}

public DeleteAllHeadersCommand(OpenApiResponse parent) {
this._parentPath = Library.createNodePath(parent);
this._parentPath = NodePathUtil.createNodePath(parent);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package io.apicurio.datamodels.cmd.commands;

import com.fasterxml.jackson.databind.node.ObjectNode;
import io.apicurio.datamodels.Library;
import io.apicurio.datamodels.cmd.AbstractCommand;
import io.apicurio.datamodels.models.Document;
import io.apicurio.datamodels.models.openapi.OpenApiOperation;
import io.apicurio.datamodels.models.openapi.OpenApiPathItem;
import io.apicurio.datamodels.models.openapi.v30.OpenApi30Operation;
import io.apicurio.datamodels.models.openapi.v30.OpenApi30PathItem;
import io.apicurio.datamodels.models.openapi.v31.OpenApi31Operation;
import io.apicurio.datamodels.models.openapi.v31.OpenApi31PathItem;
import io.apicurio.datamodels.paths.NodePath;
import io.apicurio.datamodels.paths.NodePathUtil;
import io.apicurio.datamodels.util.LoggerUtil;
import io.apicurio.datamodels.util.ModelTypeUtil;
import io.apicurio.datamodels.util.NodeUtil;

import java.util.HashMap;
import java.util.Map;

/**
* A command used to delete all operations from a path
* @author eric.wittmann@gmail.com
*/
public class DeleteAllOperationsCommand extends AbstractCommand {

private static final String[] ALL_METHODS = new String[] {
"get", "put", "post", "delete", "head", "patch", "options", "trace"
};

public NodePath _parentPath;
public Map<String, ObjectNode> _oldOperations;

public DeleteAllOperationsCommand() {
}

public DeleteAllOperationsCommand(OpenApiPathItem parent) {
this._parentPath = NodePathUtil.createNodePath(parent);
}

/**
* @see io.apicurio.datamodels.cmd.ICommand#execute(Document)
*/
@Override
public void execute(Document document) {
LoggerUtil.info("[DeleteAllOperationsCommand] Executing.");
this._oldOperations = new HashMap<>();

OpenApiPathItem parent = (OpenApiPathItem) NodePathUtil.resolveNodePath(this._parentPath, document);
if (this.isNullOrUndefined(parent)) {
return;
}

// Save the old operations (if any)
for (String method : ALL_METHODS) {
OpenApiOperation oldOp = (OpenApiOperation) NodeUtil.getProperty(parent, method);
if (!this.isNullOrUndefined(oldOp)) {
this._oldOperations.put(method, Library.writeNode(oldOp));
NodeUtil.setProperty(parent, method, null);
}
}
}

/**
* @see io.apicurio.datamodels.cmd.ICommand#undo(Document)
*/
@Override
public void undo(Document document) {
LoggerUtil.info("[DeleteAllOperationsCommand] Reverting.");
if (this._oldOperations == null || this._oldOperations.isEmpty()) {
return;
}

OpenApiPathItem parent = (OpenApiPathItem) NodePathUtil.resolveNodePath(this._parentPath, document);
if (this.isNullOrUndefined(parent)) {
return;
}

for (String method : this._oldOperations.keySet()) {
OpenApiOperation operation = parent.createOperation();
Library.readNode(this._oldOperations.get(method), operation);
setOperation(parent, operation, method);
}
}

private void setOperation(OpenApiPathItem parent, OpenApiOperation operation, String method) {
switch (method) {
case "get":
parent.setGet(operation);
break;
case "put":
parent.setPut(operation);
break;
case "post":
parent.setPost(operation);
break;
case "delete":
parent.setDelete(operation);
break;
case "head":
parent.setHead(operation);
break;
case "patch":
parent.setPatch(operation);
break;
case "options":
parent.setOptions(operation);
break;
case "trace":
if (ModelTypeUtil.isOpenApi30Model(parent)) {
((OpenApi30PathItem) parent).setTrace((OpenApi30Operation) operation);
} else if (ModelTypeUtil.isOpenApi31Model(parent)) {
((OpenApi31PathItem) parent).setTrace((OpenApi31Operation) operation);
}
break;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.apicurio.datamodels.cmd.commands;

import com.fasterxml.jackson.databind.JsonNode;
import io.apicurio.datamodels.Library;
import io.apicurio.datamodels.cmd.AbstractCommand;
import io.apicurio.datamodels.models.Document;
import io.apicurio.datamodels.models.Extensible;
Expand Down Expand Up @@ -29,7 +28,7 @@ public DeleteExtensionCommand() {
}

public DeleteExtensionCommand(Extensible parent, String extensionName) {
this._parentPath = Library.createNodePath((Node) parent);
this._parentPath = NodePathUtil.createNodePath((Node) parent);
this._name = extensionName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public DeleteMediaTypeCommand() {

public DeleteMediaTypeCommand(OpenApiMediaType mediaType) {
this._mediaTypeName = mediaType.mapPropertyName();
this._mediaTypePath = Library.createNodePath(mediaType);
this._parentPath = Library.createNodePath(mediaType.parent());
this._mediaTypePath = NodePathUtil.createNodePath(mediaType);
this._parentPath = NodePathUtil.createNodePath(mediaType.parent());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public DeleteNodeCommand() {

public DeleteNodeCommand(String property, Node from) {
this._property = property;
this._parentPath = Library.createNodePath(from);
this._parentPath = NodePathUtil.createNodePath(from);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/ts/src/io/apicurio/datamodels/util/CommandUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {DeleteAllChildSchemasCommand} from "../cmd/commands/DeleteAllChildSchema
import {DeleteAllExamplesCommand} from "../cmd/commands/DeleteAllExamplesCommand";
import {DeleteAllExtensionsCommand} from "../cmd/commands/DeleteAllExtensionsCommand";
import {DeleteAllHeadersCommand} from "../cmd/commands/DeleteAllHeadersCommand";
import {DeleteAllOperationsCommand} from "../cmd/commands/DeleteAllOperationsCommand";
import {DeleteContactCommand} from "../cmd/commands/DeleteContactCommand";
import {DeleteExtensionCommand} from "../cmd/commands/DeleteExtensionCommand";
import {DeleteLicenseCommand} from "../cmd/commands/DeleteLicenseCommand";
Expand Down Expand Up @@ -46,6 +47,7 @@ const commandSuppliers: { [key: string]: Supplier } = {
"DeleteAllExamplesCommand": () => { return new DeleteAllExamplesCommand(); },
"DeleteAllExtensionsCommand": () => { return new DeleteAllExtensionsCommand(); },
"DeleteAllHeadersCommand": () => { return new DeleteAllHeadersCommand(); },
"DeleteAllOperationsCommand": () => { return new DeleteAllOperationsCommand(); },
"DeleteContactCommand": () => { return new DeleteContactCommand(); },
"DeleteExtensionCommand": () => { return new DeleteExtensionCommand(); },
"DeleteLicenseCommand": () => { return new DeleteLicenseCommand(); },
Expand Down
4 changes: 3 additions & 1 deletion src/test/resources/fixtures/cmd/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@
{ "name": "[OpenAPI 3] {Delete All Child Schemas} - All Of", "test": "commands/delete-all-child-schemas/openapi-3/delete-all-schemas" },
{ "name": "[OpenAPI 3] {Delete All Examples} - Delete All Examples", "test": "commands/delete-all-examples/openapi-3/delete-all-examples" },
{ "name": "[OpenAPI 3] {Delete All Extensions} - Delete All Extensions", "test": "commands/delete-all-extensions/openapi-3/delete-all-extensions" },
{ "name": "[OpenAPI 3] {Delete All Headers} - Delete All Headers Response", "test": "commands/delete-all-headers/openapi-3/delete-all-headers-response" }
{ "name": "[OpenAPI 3] {Delete All Headers} - Delete All Headers Response", "test": "commands/delete-all-headers/openapi-3/delete-all-headers-response" },
{ "name": "[OpenAPI 2] {Delete All Operations} - Delete All Operations", "test": "commands/delete-all-operations/openapi-2/delete-all-operations" },
{ "name": "[OpenAPI 3] {Delete All Operations} - Delete All Operations", "test": "commands/delete-all-operations/openapi-3/delete-all-operations" }
]

0 comments on commit c443ea4

Please sign in to comment.