Search in sources :

Example 1 with CombinedVisitorAdapter

use of io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter 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 2 with CombinedVisitorAdapter

use of io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter in project apicurio-data-models by Apicurio.

the class RenameSecuritySchemeCommand method _doSecuritySchemeRename.

/**
 * Does the work of renaming a path from one name to another.
 * @param document
 * @param from
 * @param to
 * @private
 */
private void _doSecuritySchemeRename(Document document, String from, String to) {
    SecurityScheme scheme = null;
    // Different place to find the security scheme depending on the version.
    if (document.getDocumentType() == DocumentType.openapi2) {
        Oas20Document doc20 = (Oas20Document) document;
        if (ModelUtils.isDefined(doc20.securityDefinitions)) {
            // If the "to" scheme already exists, do nothing!
            if (ModelUtils.isDefined(doc20.securityDefinitions.getSecurityScheme(to))) {
                return;
            }
            scheme = doc20.securityDefinitions.removeSecurityScheme(from);
        }
    } else if (document.getDocumentType() == DocumentType.openapi3) {
        Oas30Document doc30 = (Oas30Document) document;
        if (ModelUtils.isDefined(doc30.components)) {
            // If the "to" scheme already exists, do nothing!
            if (!this.isNullOrUndefined(doc30.components.getSecurityScheme(to))) {
                return;
            }
            scheme = doc30.components.removeSecurityScheme(from);
        }
    } else {
        Aai20Document aai20Document = (Aai20Document) document;
        if (ModelUtils.isDefined(aai20Document.components)) {
            // If the "to" scheme already exists, do nothing!
            if (!this.isNullOrUndefined(aai20Document.components.getSecurityScheme(to))) {
                return;
            }
            scheme = aai20Document.components.removeSecurityScheme(from);
        }
    }
    // If we didn't find a scheme with the "from" name, then nothing to do.
    if (this.isNullOrUndefined(scheme)) {
        return;
    }
    // Now we have the scheme - rename it!
    scheme.rename(to);
    if (document.getDocumentType() == DocumentType.openapi2) {
        Oas20Document doc20 = (Oas20Document) document;
        doc20.securityDefinitions.addSecurityScheme(to, (Oas20SecurityScheme) scheme);
    } else if (document.getDocumentType() == DocumentType.openapi3) {
        Oas30Document doc30 = (Oas30Document) document;
        doc30.components.addSecurityScheme(to, (Oas30SecurityScheme) scheme);
    } else {
        Aai20Document aai20Document = (Aai20Document) document;
        aai20Document.components.addSecurityScheme(to, (Aai20SecurityScheme) scheme);
    }
    // Now find all security requirements that reference the scheme and change them too.
    VisitorUtil.visitTree(document, new CombinedVisitorAdapter() {

        @Override
        public void visitSecurityRequirement(SecurityRequirement node) {
            List<String> scopes = node.removeSecurityRequirementItem(from);
            if (ModelUtils.isDefined(scopes)) {
                node.addSecurityRequirementItem(to, scopes);
            }
        }
    }, TraverserDirection.down);
}
Also used : Oas30Document(io.apicurio.datamodels.openapi.v3.models.Oas30Document) Aai20SecurityScheme(io.apicurio.datamodels.asyncapi.v2.models.Aai20SecurityScheme) Oas20Document(io.apicurio.datamodels.openapi.v2.models.Oas20Document) Oas30SecurityScheme(io.apicurio.datamodels.openapi.v3.models.Oas30SecurityScheme) Aai20Document(io.apicurio.datamodels.asyncapi.v2.models.Aai20Document) CombinedVisitorAdapter(io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter) List(java.util.List) SecurityScheme(io.apicurio.datamodels.core.models.common.SecurityScheme) Oas30SecurityScheme(io.apicurio.datamodels.openapi.v3.models.Oas30SecurityScheme) Aai20SecurityScheme(io.apicurio.datamodels.asyncapi.v2.models.Aai20SecurityScheme) Oas20SecurityScheme(io.apicurio.datamodels.openapi.v2.models.Oas20SecurityScheme) SecurityRequirement(io.apicurio.datamodels.core.models.common.SecurityRequirement)

Aggregations

CombinedVisitorAdapter (io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter)2 Aai20Document (io.apicurio.datamodels.asyncapi.v2.models.Aai20Document)1 Aai20SecurityScheme (io.apicurio.datamodels.asyncapi.v2.models.Aai20SecurityScheme)1 Operation (io.apicurio.datamodels.core.models.common.Operation)1 SecurityRequirement (io.apicurio.datamodels.core.models.common.SecurityRequirement)1 SecurityScheme (io.apicurio.datamodels.core.models.common.SecurityScheme)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 OasPathItem (io.apicurio.datamodels.openapi.models.OasPathItem)1 Oas20Document (io.apicurio.datamodels.openapi.v2.models.Oas20Document)1 Oas20SecurityScheme (io.apicurio.datamodels.openapi.v2.models.Oas20SecurityScheme)1 Oas30Document (io.apicurio.datamodels.openapi.v3.models.Oas30Document)1 Oas30SecurityScheme (io.apicurio.datamodels.openapi.v3.models.Oas30SecurityScheme)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1