Skip to content

Commit

Permalink
Added DeleteAllExamplesCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWittmann committed Jan 3, 2025
1 parent 95dbbd1 commit 4b7fb2d
Show file tree
Hide file tree
Showing 5 changed files with 322 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/main/java/io/apicurio/datamodels/cmd/CommandFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.apicurio.datamodels.cmd.commands.ChangeTitleCommand;
import io.apicurio.datamodels.cmd.commands.ChangeVersionCommand;
import io.apicurio.datamodels.cmd.commands.DeleteAllChildSchemasCommand;
import io.apicurio.datamodels.cmd.commands.DeleteAllExamplesCommand;
import io.apicurio.datamodels.cmd.commands.DeleteContactCommand;
import io.apicurio.datamodels.cmd.commands.DeleteExtensionCommand;
import io.apicurio.datamodels.cmd.commands.DeleteLicenseCommand;
Expand All @@ -17,7 +18,9 @@
import io.apicurio.datamodels.models.Info;
import io.apicurio.datamodels.models.Node;
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.OpenApiParameter;
import io.apicurio.datamodels.util.CommandUtil;

public class CommandFactory {
Expand Down Expand Up @@ -78,4 +81,16 @@ public static final ICommand createDeleteAllChildSchemasCommand(Schema parent, S
return new DeleteAllChildSchemasCommand(parent, type);
}

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

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

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
/*
* 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.Parameter;
import io.apicurio.datamodels.models.openapi.OpenApiExample;
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.v30.OpenApi30Example;
import io.apicurio.datamodels.models.openapi.v30.OpenApi30Header;
import io.apicurio.datamodels.models.openapi.v30.OpenApi30Parameter;
import io.apicurio.datamodels.models.openapi.v31.OpenApi31Example;
import io.apicurio.datamodels.models.openapi.v31.OpenApi31Header;
import io.apicurio.datamodels.models.openapi.v31.OpenApi31Parameter;
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.ModelTypeUtil;

import java.util.LinkedHashMap;
import java.util.Map;

/**
* A command used to delete a single mediaType from an operation.
* @author eric.wittmann@gmail.com
*/
public class DeleteAllExamplesCommand extends AbstractCommand {

public NodePath _parentPath;

public Map<String, ObjectNode> _oldExamples;

public DeleteAllExamplesCommand() {
}

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

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

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

@Override
public void execute(Document document) {
LoggerUtil.info("[DeleteAllExamplesCommand] Executing.");

if (this.isNullOrUndefined(document)) {
LoggerUtil.debug("[DeleteAllExamplesCommand] Could not execute the command, invalid argument.");
return;
}

if (this.isNullOrUndefined(this._parentPath)) {
LoggerUtil.debug("[DeleteAllExamplesCommand] Could not execute the command, problem when unmarshalling.");
return;
}

Node parent = NodePathUtil.resolveNodePath(this._parentPath, document);
if (this.isNullOrUndefined(parent)) {
LoggerUtil.debug("[DeleteAllExamplesCommand] Parent node not found.");
return;
}

// Get the examples from the parent
GetExamplesVisitor gev = new GetExamplesVisitor();
parent.accept(gev);

// Save the examples to enable undo
this._oldExamples = new LinkedHashMap<>();
gev.examples.keySet().forEach(k -> {
Node n = gev.examples.get(k);
this._oldExamples.put(k, Library.writeNode(n));
});

// Clear (remove all) the examples
ClearExamplesVisitor cev = new ClearExamplesVisitor();
parent.accept(cev);
}

/**
* @see io.apicurio.datamodels.cmd.ICommand#undo(Document)
*/
@Override
public void undo(Document document) {
if (this.isNullOrUndefined(document)) {
LoggerUtil.debug("[DeleteAllExamplesCommand] Could not revert the command, invalid argument.");
return;
}

if (this.isNullOrUndefined(this._parentPath)) {
LoggerUtil.debug("[DeleteAllExamplesCommand] Could not revert the command, problem when unmarshalling.");
return;
}

LoggerUtil.info("[DeleteAllExamplesCommand] Reverting.");

Node parent = NodePathUtil.resolveNodePath(this._parentPath, document);
if (this.isNullOrUndefined(parent)) {
LoggerUtil.info("[DeleteAllExamplesCommand] No parent node found.");
return;
}

if (this.isNullOrUndefined(this._oldExamples)) {
LoggerUtil.info("[DeleteAllExamplesCommand] Could not revert. Previous data is not available.");
return;
}

for (String k : this._oldExamples.keySet()) {
Node example = createExample(parent);
Library.readNode(this._oldExamples.get(k), (Node) example);
addExample(parent, k, example);
}
}

private Node createExample(Node parent) {
CreateExampleVisitor cev = new CreateExampleVisitor();
parent.accept(cev);
return cev.example;
}

private void addExample(Node parent, String name, Node example) {
AddExampleVisitor aev = new AddExampleVisitor(name, example);
parent.accept(aev);
}

private static abstract class ExamplesParentVisitor extends CombinedVisitorAdapter {

protected abstract void visitParameterOpenApi30(OpenApi30Parameter param);
protected abstract void visitParameterOpenApi31(OpenApi31Parameter param);
protected abstract void visitHeaderOpenApi30(OpenApi30Header param);
protected abstract void visitHeaderOpenApi31(OpenApi31Header param);

@Override
public void visitParameter(Parameter node) {
if (ModelTypeUtil.isOpenApi30Model(node)) {
visitParameterOpenApi30((OpenApi30Parameter) node);
} else if (ModelTypeUtil.isOpenApi31Model(node)) {
visitParameterOpenApi31((OpenApi31Parameter) node);
}
}

@Override
public void visitHeader(OpenApiHeader node) {
if (ModelTypeUtil.isOpenApi30Model(node)) {
visitHeaderOpenApi30((OpenApi30Header) node);
} else if (ModelTypeUtil.isOpenApi31Model(node)) {
visitHeaderOpenApi31((OpenApi31Header) node);
}
}

@Override
public abstract void visitMediaType(OpenApiMediaType node);
}

private static class GetExamplesVisitor extends ExamplesParentVisitor {
public Map<String, ? extends Node> examples;

@Override
protected void visitParameterOpenApi30(OpenApi30Parameter param) {
examples = param.getExamples();
}

@Override
protected void visitParameterOpenApi31(OpenApi31Parameter param) {
examples = param.getExamples();
}

@Override
protected void visitHeaderOpenApi30(OpenApi30Header param) {
examples = param.getExamples();
}

@Override
protected void visitHeaderOpenApi31(OpenApi31Header param) {
examples = param.getExamples();
}

@Override
public void visitMediaType(OpenApiMediaType node) {
examples = node.getExamples();
}
}

private static class ClearExamplesVisitor extends ExamplesParentVisitor {

@Override
protected void visitParameterOpenApi30(OpenApi30Parameter param) {
param.clearExamples();
}

@Override
protected void visitParameterOpenApi31(OpenApi31Parameter param) {
param.clearExamples();
}

@Override
protected void visitHeaderOpenApi30(OpenApi30Header param) {
param.clearExamples();
}

@Override
protected void visitHeaderOpenApi31(OpenApi31Header param) {
param.clearExamples();
}

@Override
public void visitMediaType(OpenApiMediaType node) {
node.clearExamples();
}
}

private static class CreateExampleVisitor extends ExamplesParentVisitor {

public Node example;

@Override
protected void visitParameterOpenApi30(OpenApi30Parameter param) {
example = param.createExample();
}

@Override
protected void visitParameterOpenApi31(OpenApi31Parameter param) {
example = param.createExample();
}

@Override
protected void visitHeaderOpenApi30(OpenApi30Header param) {
example = param.createExample();
}

@Override
protected void visitHeaderOpenApi31(OpenApi31Header param) {
example = param.createExample();
}

@Override
public void visitMediaType(OpenApiMediaType node) {
example = node.createExample();
}
}

private static class AddExampleVisitor extends ExamplesParentVisitor {
private final String name;
private final Node example;

public AddExampleVisitor(String name, Node example) {
this.name = name;
this.example = example;
}

@Override
protected void visitParameterOpenApi30(OpenApi30Parameter param) {
param.addExample(this.name, (OpenApi30Example) this.example);
}

@Override
protected void visitParameterOpenApi31(OpenApi31Parameter param) {
param.addExample(this.name, (OpenApi31Example) this.example);
}

@Override
protected void visitHeaderOpenApi30(OpenApi30Header param) {
param.addExample(this.name, (OpenApi30Example) this.example);
}

@Override
protected void visitHeaderOpenApi31(OpenApi31Header param) {
param.addExample(this.name, (OpenApi31Example) this.example);
}

@Override
public void visitMediaType(OpenApiMediaType node) {
node.addExample(this.name, (OpenApiExample) this.example);
}
}

}
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 @@ -12,6 +12,7 @@ import {ChangeContactCommand} from "../cmd/commands/ChangeContactCommand";
import {ChangeLicenseCommand} from "../cmd/commands/ChangeLicenseCommand";

import {DeleteAllChildSchemasCommand} from "../cmd/commands/DeleteAllChildSchemasCommand";
import {DeleteAllExamplesCommand} from "../cmd/commands/DeleteAllExamplesCommand";
import {DeleteContactCommand} from "../cmd/commands/DeleteContactCommand";
import {DeleteExtensionCommand} from "../cmd/commands/DeleteExtensionCommand";
import {DeleteLicenseCommand} from "../cmd/commands/DeleteLicenseCommand";
Expand Down Expand Up @@ -40,6 +41,7 @@ const commandSuppliers: { [key: string]: Supplier } = {
"ChangeLicenseCommand": () => { return new ChangeLicenseCommand(); },

"DeleteAllChildSchemasCommand": () => { return new DeleteAllChildSchemasCommand(); },
"DeleteAllExamplesCommand": () => { return new DeleteAllExamplesCommand(); },
"DeleteContactCommand": () => { return new DeleteContactCommand(); },
"DeleteExtensionCommand": () => { return new DeleteExtensionCommand(); },
"DeleteLicenseCommand": () => { return new DeleteLicenseCommand(); },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[{
"__type": "DeleteAllExamplesCommand",
"_oldExamples": {},
"_mediaTypePath": "/paths[/]/get/responses[500]/content[application/json]"
"_parentPath": "/paths[/]/get/responses[500]/content[application/json]"
}]
3 changes: 2 additions & 1 deletion src/test/resources/fixtures/cmd/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
{ "name": "[OpenAPI 3] {Delete Extension} - Delete Extension", "test": "commands/delete-extension/openapi-3/delete-extension" },
{ "name": "[OpenAPI 3] {Delete Media Type} - Delete Media Type Request Body", "test": "commands/delete-media-type/openapi-3/delete-media-type-requestBody" },
{ "name": "[OpenAPI 3] {Delete Media Type} - Delete Media Type Response", "test": "commands/delete-media-type/openapi-3/delete-media-type-response" },
{ "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 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" }
]

0 comments on commit 4b7fb2d

Please sign in to comment.