Skip to content

Commit

Permalink
Implemented DeleteAllPropertiesCommand.
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWittmann committed Jan 6, 2025
1 parent 26ea619 commit d37bb69
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/io/apicurio/datamodels/cmd/CommandFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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.DeleteAllPropertiesCommand;
import io.apicurio.datamodels.cmd.commands.DeleteContactCommand;
import io.apicurio.datamodels.cmd.commands.DeleteExtensionCommand;
import io.apicurio.datamodels.cmd.commands.DeleteLicenseCommand;
Expand Down Expand Up @@ -115,4 +116,8 @@ public static final ICommand createDeleteAllOperationParametersCommand(OpenApiOp
return new DeleteAllParametersCommand(parent, type);
}

public static final ICommand createDeleteAllPropertiesCommand(Schema schema) {
return new DeleteAllPropertiesCommand(schema);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* 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.Schema;
import io.apicurio.datamodels.paths.NodePath;
import io.apicurio.datamodels.paths.NodePathUtil;
import io.apicurio.datamodels.util.LoggerUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* A command used to delete all properties from a schema.
* @author eric.wittmann@gmail.com
*/
public class DeleteAllPropertiesCommand extends AbstractCommand {

public NodePath _schemaPath;

public Map<String, ObjectNode> _oldProperties;
public List<String> _oldRequired;

public DeleteAllPropertiesCommand() {
}

public DeleteAllPropertiesCommand(Schema schema) {
this._schemaPath = Library.createNodePath(schema);
}

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

Schema schema = (Schema) NodePathUtil.resolveNodePath(this._schemaPath, document);

if (this.isNullOrUndefined(schema)) {
return;
}

List<String> propertyNames = new ArrayList<>();
propertyNames.addAll(schema.getProperties().keySet());
propertyNames.forEach(pname -> {
Schema pvalue = schema.getProperties().get(pname);
this._oldProperties.put(pname, Library.writeNode(pvalue));
if (isRequired(schema, pname)) {
this._oldRequired.add(pname);
}
schema.removeProperty(pname);
removeRequired(schema, pname);
});
if (schema.getRequired() != null && schema.getRequired().isEmpty()) {
schema.setRequired(null);
}
}

private void removeRequired(Schema schema, String pname) {
if (schema.getRequired() != null) {
schema.getRequired().remove(pname);
}
}

private boolean isRequired(Schema schema, String pname) {
return schema.getRequired() != null && schema.getRequired().contains(pname);
}

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

Schema schema = (Schema) NodePathUtil.resolveNodePath(this._schemaPath, document);
if (this.isNullOrUndefined(schema)) {
return;
}

this._oldProperties.keySet().forEach( pname -> {
Schema pschema = schema.createSchema();
Library.readNode(this._oldProperties.get(pname), pschema);
schema.addProperty(pname, pschema);
if (this._oldRequired.contains(pname)) {
addRequired(schema, pname);
}
});
}

private void addRequired(Schema schema, String pname) {
if (schema.getRequired() == null) {
schema.setRequired(new ArrayList<>());
}
if (!schema.getRequired().contains(pname)) {
schema.getRequired().add(pname);
}
}


}
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 @@ -17,6 +17,7 @@ import {DeleteAllExtensionsCommand} from "../cmd/commands/DeleteAllExtensionsCom
import {DeleteAllHeadersCommand} from "../cmd/commands/DeleteAllHeadersCommand";
import {DeleteAllOperationsCommand} from "../cmd/commands/DeleteAllOperationsCommand";
import {DeleteAllParametersCommand} from "../cmd/commands/DeleteAllParametersCommand";
import {DeleteAllPropertiesCommand} from "../cmd/commands/DeleteAllPropertiesCommand";
import {DeleteContactCommand} from "../cmd/commands/DeleteContactCommand";
import {DeleteExtensionCommand} from "../cmd/commands/DeleteExtensionCommand";
import {DeleteLicenseCommand} from "../cmd/commands/DeleteLicenseCommand";
Expand Down Expand Up @@ -50,6 +51,7 @@ const commandSuppliers: { [key: string]: Supplier } = {
"DeleteAllHeadersCommand": () => { return new DeleteAllHeadersCommand(); },
"DeleteAllOperationsCommand": () => { return new DeleteAllOperationsCommand(); },
"DeleteAllParametersCommand": () => { return new DeleteAllParametersCommand(); },
"DeleteAllPropertiesCommand": () => { return new DeleteAllPropertiesCommand(); },
"DeleteContactCommand": () => { return new DeleteContactCommand(); },
"DeleteExtensionCommand": () => { return new DeleteExtensionCommand(); },
"DeleteLicenseCommand": () => { return new DeleteLicenseCommand(); },
Expand Down
5 changes: 4 additions & 1 deletion src/test/resources/fixtures/cmd/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@
{ "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 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" }
{ "name": "[OpenAPI 3] {Delete All Params} - Delete All Params", "test" : "commands/delete-all-parameters/openapi-3/delete-all-parameters" },
{ "name": "[OpenAPI 2] {Delete All Properties} - Delete All Properties", "test": "commands/delete-all-properties/openapi-2/delete-all-properties" },
{ "name": "[OpenAPI 3] {Delete All Properties} - Delete All Properties", "test": "commands/delete-all-properties/openapi-3/delete-all-properties" },
{ "name": "[AsyncAPI 2] {Delete All Properties} - Delete All Properties", "test": "commands/delete-all-properties/asyncapi-2/delete-all-properties" }
]

0 comments on commit d37bb69

Please sign in to comment.