Search in sources :

Example 1 with Operation

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;
}
Also used : Node(io.apicurio.datamodels.core.models.Node) Operation(io.apicurio.datamodels.core.models.common.Operation) Document(io.apicurio.datamodels.core.models.Document) ServerVariable(io.apicurio.datamodels.core.models.common.ServerVariable) Oas20PropertySchema(io.apicurio.datamodels.openapi.v2.models.Oas20Schema.Oas20PropertySchema) InvocationTargetException(java.lang.reflect.InvocationTargetException) OasPathItem(io.apicurio.datamodels.openapi.models.OasPathItem) Oas30PropertySchema(io.apicurio.datamodels.openapi.v3.models.Oas30Schema.Oas30PropertySchema) INamed(io.apicurio.datamodels.core.models.common.INamed)

Example 2 with Operation

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;
    });
}
Also used : OasParameter(io.apicurio.datamodels.openapi.models.OasParameter) IOasParameterParent(io.apicurio.datamodels.openapi.models.IOasParameterParent) OasOperation(io.apicurio.datamodels.openapi.models.OasOperation) CombinedVisitorAdapter(io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter) OasPathItem(io.apicurio.datamodels.openapi.models.OasPathItem) ArrayList(java.util.ArrayList) OasOperation(io.apicurio.datamodels.openapi.models.OasOperation) Operation(io.apicurio.datamodels.core.models.common.Operation)

Example 3 with Operation

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;
}
Also used : OasPathItem(io.apicurio.datamodels.openapi.models.OasPathItem) Operation(io.apicurio.datamodels.core.models.common.Operation)

Aggregations

Operation (io.apicurio.datamodels.core.models.common.Operation)3 OasPathItem (io.apicurio.datamodels.openapi.models.OasPathItem)3 CombinedVisitorAdapter (io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter)1 Document (io.apicurio.datamodels.core.models.Document)1 Node (io.apicurio.datamodels.core.models.Node)1 INamed (io.apicurio.datamodels.core.models.common.INamed)1 ServerVariable (io.apicurio.datamodels.core.models.common.ServerVariable)1 IOasParameterParent (io.apicurio.datamodels.openapi.models.IOasParameterParent)1 OasOperation (io.apicurio.datamodels.openapi.models.OasOperation)1 OasParameter (io.apicurio.datamodels.openapi.models.OasParameter)1 Oas20PropertySchema (io.apicurio.datamodels.openapi.v2.models.Oas20Schema.Oas20PropertySchema)1 Oas30PropertySchema (io.apicurio.datamodels.openapi.v3.models.Oas30Schema.Oas30PropertySchema)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1