use of io.apicurio.datamodels.core.models.common.Operation in project apicurio-data-models by Apicurio.
the class IoTestRunner method cloneNode.
static Node cloneNode(Node node) {
Constructor<? extends Node> constructor;
Node clonedNode = null;
try {
if (node instanceof INamed) {
constructor = node.getClass().getConstructor(String.class);
clonedNode = constructor.newInstance(((INamed) node).getName());
} else if (node instanceof OasPathItem) {
constructor = node.getClass().getConstructor(String.class);
clonedNode = constructor.newInstance(((OasPathItem) node).getPath());
} else if (node instanceof Operation) {
constructor = node.getClass().getConstructor(String.class);
clonedNode = constructor.newInstance(((Operation) node).getType());
} else if (node instanceof Oas20PropertySchema) {
constructor = node.getClass().getConstructor(String.class);
clonedNode = constructor.newInstance(((Oas20PropertySchema) node).getPropertyName());
} else if (node instanceof Oas30PropertySchema) {
constructor = node.getClass().getConstructor(String.class);
clonedNode = constructor.newInstance(((Oas30PropertySchema) node).getPropertyName());
} else if (node instanceof ServerVariable) {
constructor = node.getClass().getConstructor(String.class);
clonedNode = constructor.newInstance(((ServerVariable) node).getName());
} else {
constructor = node.getClass().getConstructor();
clonedNode = constructor.newInstance();
}
} catch (SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
System.err.println("Skipping partial I/O for: " + node.getClass().getSimpleName());
}
if (clonedNode != null) {
clonedNode._parent = node._parent;
clonedNode._ownerDocument = node.ownerDocument();
if (clonedNode instanceof Document) {
clonedNode._ownerDocument = (Document) clonedNode;
}
}
return clonedNode;
}
use of io.apicurio.datamodels.core.models.common.Operation in project apicurio-data-models by Apicurio.
the class RenameParameterCommand method _doParameterRename.
/**
* Does the work of renaming a param from one name to another.
* @param document
* @param from
* @param to
*/
private void _doParameterRename(Document document, String from, String to) {
IOasParameterParent parent = (IOasParameterParent) this._parentPath.resolve(document);
if (this.isNullOrUndefined(parent)) {
return;
}
// Find the param being changed, if not present bail.
OasParameter param = parent.getParameter(this._paramIn, from);
if (this.isNullOrUndefined(param)) {
return;
}
// Start a list of all the params we're going to rename.
List<OasParameter> allParams = new ArrayList<>();
allParams.add(param);
// param.name = to;
// Detect what type of parent we're dealing with.
isPathItem = false;
isOperation = false;
VisitorUtil.visitNode((Node) parent, new CombinedVisitorAdapter() {
@Override
public void visitPathItem(OasPathItem node) {
isPathItem = true;
}
@Override
public void visitOperation(Operation node) {
isOperation = true;
}
});
List<String> methods = NodeCompat.asList("get", "put", "post", "delete", "options", "head", "patch", "trace");
// If the parent is a path item, then we also need to rename any overriding operation params.
if (isPathItem) {
OasPathItem pathItem = (OasPathItem) parent;
for (String method : methods) {
OasOperation op = (OasOperation) NodeCompat.getProperty(pathItem, method);
if (!this.isNullOrUndefined(op)) {
OasParameter opParam = op.getParameter(_paramIn, from);
if (!this.isNullOrUndefined(opParam)) {
allParams.add(opParam);
}
}
}
}
// there IS a param defined at the path level, we'll also need to rename all params in our peer operations.
if (isOperation) {
OasOperation operation = (OasOperation) parent;
OasPathItem pathItem = (OasPathItem) operation.parent();
OasParameter pparam = pathItem.getParameter(_paramIn, from);
if (!this.isNullOrUndefined(pparam)) {
allParams.add(pparam);
for (String method : methods) {
OasOperation peerOperation = (OasOperation) NodeCompat.getProperty(pathItem, method);
if (ModelUtils.isDefined(peerOperation) && peerOperation != operation) {
OasParameter opParam = peerOperation.getParameter(_paramIn, from);
if (!this.isNullOrUndefined(opParam)) {
allParams.add(opParam);
}
}
}
}
}
// Now actually do the rename.
allParams.forEach(p -> {
p.name = to;
});
}
use of io.apicurio.datamodels.core.models.common.Operation in project apicurio-data-models by Apicurio.
the class DeleteOperationCommand method readNode.
/**
* @see io.apicurio.datamodels.cmd.commands.DeleteNodeCommand#readNode(io.apicurio.datamodels.core.models.Document, java.lang.Object)
*/
@Override
protected Operation readNode(Document doc, Object node) {
OasPathItem pathItem = (OasPathItem) this._parentPath.resolve(doc);
Operation operation = pathItem.createOperation(this._property);
Library.readNode(node, operation);
return operation;
}
Aggregations