Skip to content

Commit

Permalink
Added DeleteAllExtensionsCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWittmann committed Jan 3, 2025
1 parent 4b7fb2d commit 6d40d0d
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* Copyright 2019 JBoss Inc
*
* 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package io.apicurio.datamodels.cmd.commands;

import com.fasterxml.jackson.databind.node.ObjectNode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* 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 io.apicurio.datamodels.cmd.AbstractCommand;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* 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 io.apicurio.datamodels.cmd.AbstractCommand;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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;
import io.apicurio.datamodels.models.Node;
import io.apicurio.datamodels.paths.NodePath;
import io.apicurio.datamodels.paths.NodePathUtil;
import io.apicurio.datamodels.util.LoggerUtil;

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

/**
* A command used to delete all extensions from a node.
* @author [email protected]
*/
public class DeleteAllExtensionsCommand extends AbstractCommand {

public NodePath _parentPath;

public Map<String, JsonNode> _oldExtensions;

public DeleteAllExtensionsCommand() {
}

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

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

Extensible parent = (Extensible) NodePathUtil.resolveNodePath(this._parentPath, document);

Map<String, JsonNode> extensions = parent.getExtensions();

// Save the old extensions (if any)
if (!this.isNullOrUndefined(extensions)) {
extensions.keySet().forEach(k -> {
JsonNode value = extensions.get(k);
this._oldExtensions.put(k, value);
});
}

// Remove all extensions
parent.clearExtensions();
}

/**
* @see io.apicurio.datamodels.cmd.ICommand#undo(Document)
*/
@Override
public void undo(Document document) {
LoggerUtil.info("[DeleteAllExtensionsCommand] Reverting.");
if (this._oldExtensions.size() == 0) {
return;
}

Extensible parent = (Extensible) NodePathUtil.resolveNodePath(this._parentPath, document);

this._oldExtensions.keySet().forEach(k -> {
JsonNode value = _oldExtensions.get(k);
parent.addExtension(k, value);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* 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;
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 @@ -13,6 +13,7 @@ import {ChangeLicenseCommand} from "../cmd/commands/ChangeLicenseCommand";

import {DeleteAllChildSchemasCommand} from "../cmd/commands/DeleteAllChildSchemasCommand";
import {DeleteAllExamplesCommand} from "../cmd/commands/DeleteAllExamplesCommand";
import {DeleteAllExtensionsCommand} from "../cmd/commands/DeleteAllExtensionsCommand";
import {DeleteContactCommand} from "../cmd/commands/DeleteContactCommand";
import {DeleteExtensionCommand} from "../cmd/commands/DeleteExtensionCommand";
import {DeleteLicenseCommand} from "../cmd/commands/DeleteLicenseCommand";
Expand Down Expand Up @@ -42,6 +43,7 @@ const commandSuppliers: { [key: string]: Supplier } = {

"DeleteAllChildSchemasCommand": () => { return new DeleteAllChildSchemasCommand(); },
"DeleteAllExamplesCommand": () => { return new DeleteAllExamplesCommand(); },
"DeleteAllExtensionsCommand": () => { return new DeleteAllExtensionsCommand(); },
"DeleteContactCommand": () => { return new DeleteContactCommand(); },
"DeleteExtensionCommand": () => { return new DeleteExtensionCommand(); },
"DeleteLicenseCommand": () => { return new DeleteLicenseCommand(); },
Expand Down
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 @@ -34,5 +34,6 @@
{ "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 Examples} - Delete All Examples", "test": "commands/delete-all-examples/openapi-3/delete-all-examples" }
{ "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" }
]

0 comments on commit 6d40d0d

Please sign in to comment.