Skip to content

Commit

Permalink
Implemented DeleteAllParametersCommand.
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWittmann committed Jan 6, 2025
1 parent c443ea4 commit 26ea619
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/main/java/io/apicurio/datamodels/cmd/CommandFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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.DeleteAllParametersCommand;
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.Schema;
import io.apicurio.datamodels.models.openapi.OpenApiHeader;
import io.apicurio.datamodels.models.openapi.OpenApiMediaType;
import io.apicurio.datamodels.models.openapi.OpenApiOperation;
import io.apicurio.datamodels.models.openapi.OpenApiParameter;
import io.apicurio.datamodels.models.openapi.OpenApiPathItem;
import io.apicurio.datamodels.models.openapi.OpenApiResponse;
Expand Down Expand Up @@ -105,4 +107,12 @@ public static ICommand createDeleteAllPathItemOperationsCommand(OpenApiPathItem
return new DeleteAllOperationsCommand(pathItem);
}

public static final ICommand createDeleteAllPathItemParametersCommand(OpenApiPathItem parent, String type) {
return new DeleteAllParametersCommand(parent, type);
}

public static final ICommand createDeleteAllOperationParametersCommand(OpenApiOperation parent, String type) {
return new DeleteAllParametersCommand(parent, type);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Copyright 2019 Red Hat
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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.Node;
import io.apicurio.datamodels.models.Operation;
import io.apicurio.datamodels.models.openapi.OpenApiOperation;
import io.apicurio.datamodels.models.openapi.OpenApiParameter;
import io.apicurio.datamodels.models.openapi.OpenApiPathItem;
import io.apicurio.datamodels.models.visitors.CombinedVisitorAdapter;
import io.apicurio.datamodels.paths.NodePath;
import io.apicurio.datamodels.paths.NodePathUtil;
import io.apicurio.datamodels.util.LoggerUtil;
import io.apicurio.datamodels.util.NodeUtil;

import java.util.ArrayList;
import java.util.List;

/**
* A command used to delete all parameters from: PathItem, Operation
* @author eric.wittmann@gmail.com
*/
public class DeleteAllParametersCommand extends AbstractCommand {

public NodePath _parentPath;
public String _paramType;

public List<ObjectNode> _oldParams;

public DeleteAllParametersCommand() {
}

public DeleteAllParametersCommand(OpenApiPathItem parent, String type) {
this._parentPath = Library.createNodePath(parent);
this._paramType = type;
}

public DeleteAllParametersCommand(OpenApiOperation parent, String type) {
this._parentPath = Library.createNodePath(parent);
this._paramType = type;
}

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

Node parent = NodePathUtil.resolveNodePath(this._parentPath, document);
List<OpenApiParameter> parameters = getParameters(parent);
if (this.isNullOrUndefined(parent) || this.isNullOrUndefined(parameters) || parameters.isEmpty()) {
return;
}

// Save the params we're about to delete for later undo
List<OpenApiParameter> paramsToRemove = new ArrayList<>();
for (OpenApiParameter param : parameters) {
if (NodeUtil.equals(param.getIn(), this._paramType)) {
this._oldParams.add(Library.writeNode(param));
paramsToRemove.add(param);
}
}

if (this._oldParams.isEmpty()) {
return;
}

paramsToRemove.forEach(paramToRemove -> {
parameters.remove(paramToRemove);
});
}

/**
* @see io.apicurio.datamodels.cmd.ICommand#undo(Document)
*/
@Override
public void undo(Document document) {
LoggerUtil.info("[DeleteAllParameters] Reverting.");

if (this.isNullOrUndefined(this._oldParams) || this._oldParams.size() == 0) {
return;
}

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

this._oldParams.forEach(paramObj -> {
OpenApiParameter parameter = createParameter(parent);
Library.readNode(paramObj, parameter);
addParameter(parent, parameter);
});
}

private void addParameter(Node parent, OpenApiParameter parameter) {
AddParameterVisitor gpv = new AddParameterVisitor(parameter);
parent.accept(gpv);
}

private OpenApiParameter createParameter(Node parent) {
CreateParameterVisitor gpv = new CreateParameterVisitor();
parent.accept(gpv);
return gpv._param;
}

private List<OpenApiParameter> getParameters(Node parent) {
GetParametersVisitor gpv = new GetParametersVisitor();
parent.accept(gpv);
return gpv._params;
}

private class GetParametersVisitor extends CombinedVisitorAdapter {
List<OpenApiParameter> _params;

@Override
public void visitPathItem(OpenApiPathItem node) {
this._params = node.getParameters();
}

@Override
public void visitOperation(Operation node) {
this._params = ((OpenApiOperation) node).getParameters();
}
}

private class CreateParameterVisitor extends CombinedVisitorAdapter {
OpenApiParameter _param;

@Override
public void visitPathItem(OpenApiPathItem node) {
this._param = node.createParameter();
}

@Override
public void visitOperation(Operation node) {
this._param = ((OpenApiOperation) node).createParameter();
}
}

private class AddParameterVisitor extends CombinedVisitorAdapter {
OpenApiParameter _param;

public AddParameterVisitor(OpenApiParameter param) {
this._param = param;
}

@Override
public void visitPathItem(OpenApiPathItem node) {
node.addParameter(_param);
}

@Override
public void visitOperation(Operation node) {
((OpenApiOperation) node).addParameter(_param);
}
}

}
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 @@ -16,6 +16,7 @@ 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 {DeleteAllParametersCommand} from "../cmd/commands/DeleteAllParametersCommand";
import {DeleteContactCommand} from "../cmd/commands/DeleteContactCommand";
import {DeleteExtensionCommand} from "../cmd/commands/DeleteExtensionCommand";
import {DeleteLicenseCommand} from "../cmd/commands/DeleteLicenseCommand";
Expand Down Expand Up @@ -48,6 +49,7 @@ const commandSuppliers: { [key: string]: Supplier } = {
"DeleteAllExtensionsCommand": () => { return new DeleteAllExtensionsCommand(); },
"DeleteAllHeadersCommand": () => { return new DeleteAllHeadersCommand(); },
"DeleteAllOperationsCommand": () => { return new DeleteAllOperationsCommand(); },
"DeleteAllParametersCommand": () => { return new DeleteAllParametersCommand(); },
"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 @@ -38,5 +38,7 @@
{ "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 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" }
{ "name": "[OpenAPI 3] {Delete All Operations} - Delete All Operations", "test": "commands/delete-all-operations/openapi-3/delete-all-operations" },
{ "name": "[OpenAPI 2] {Delete All Params} - Delete All Params", "test": "commands/delete-all-parameters/openapi-2/delete-all-parameters" },
{ "name": "[OpenAPI 3] {Delete All Params} - Delete All Params", "test" : "commands/delete-all-parameters/openapi-3/delete-all-parameters" }
]

0 comments on commit 26ea619

Please sign in to comment.