Search in sources :

Example 1 with SecurityRequirement

use of io.apicurio.datamodels.core.models.common.SecurityRequirement in project apicurio-data-models by Apicurio.

the class DeleteSecurityRequirementCommand method undo.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void undo(Document document) {
    LoggerCompat.info("[DeleteSecurityRequirementCommand] Reverting.");
    if (this.isNullOrUndefined(this._oldRequirement)) {
        return;
    }
    ISecurityRequirementParent parent = (ISecurityRequirementParent) this._parentPath.resolve(document);
    if (this.isNullOrUndefined(parent)) {
        return;
    }
    SecurityRequirement restoredRequirement = parent.createSecurityRequirement();
    Library.readNode(this._oldRequirement, restoredRequirement);
    parent.addSecurityRequirement(restoredRequirement);
}
Also used : ISecurityRequirementParent(io.apicurio.datamodels.core.models.common.ISecurityRequirementParent) SecurityRequirement(io.apicurio.datamodels.core.models.common.SecurityRequirement)

Example 2 with SecurityRequirement

use of io.apicurio.datamodels.core.models.common.SecurityRequirement in project apicurio-data-models by Apicurio.

the class DeleteAllSecurityRequirementsCommand method undo.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void undo(Document document) {
    LoggerCompat.info("[DeleteAllSecurityRequirementsCommand] Reverting.");
    if (ModelUtils.isDefined(this._oldSecurityRequirements) && this._oldSecurityRequirements.size() == 0) {
        return;
    }
    ISecurityRequirementParent parent = (ISecurityRequirementParent) this._parentPath.resolve(document);
    if (this.isNullOrUndefined(parent)) {
        return;
    }
    List<SecurityRequirement> requirements = parent.getSecurityRequirements();
    if (this.isNullOrUndefined(requirements)) {
        requirements = new ArrayList<>();
        NodeCompat.setProperty(parent, Constants.PROP_SECURITY, requirements);
    }
    for (Object oldSecurityRequirement : this._oldSecurityRequirements) {
        SecurityRequirement requirement = parent.createSecurityRequirement();
        Library.readNode(oldSecurityRequirement, requirement);
        requirements.add(requirement);
    }
}
Also used : ISecurityRequirementParent(io.apicurio.datamodels.core.models.common.ISecurityRequirementParent) SecurityRequirement(io.apicurio.datamodels.core.models.common.SecurityRequirement)

Example 3 with SecurityRequirement

use of io.apicurio.datamodels.core.models.common.SecurityRequirement in project apicurio-data-models by Apicurio.

the class SecurityRequirementUniquenessValidationRule method visitSecurityRequirement.

/**
 * @param node
 * @see io.apicurio.datamodels.combined.visitors.CombinedAllNodeVisitor#visitSecurityRequirement(io.apicurio.datamodels.core.models.common.SecurityRequirement)
 */
public void visitSecurityRequirement(SecurityRequirement node) {
    ISecurityRequirementParent parent = (ISecurityRequirementParent) node.parent();
    List<SecurityRequirement> securityRequirements = parent.getSecurityRequirements();
    if (securityRequirements.size() <= 1) {
        return;
    }
    if (checkForDuplicates(node, securityRequirements)) {
        this.report(node, Constants.PROP_SECURITY, map("securityReq", node.getSecurityRequirementNames().toString()));
    }
}
Also used : ISecurityRequirementParent(io.apicurio.datamodels.core.models.common.ISecurityRequirementParent) SecurityRequirement(io.apicurio.datamodels.core.models.common.SecurityRequirement)

Example 4 with SecurityRequirement

use of io.apicurio.datamodels.core.models.common.SecurityRequirement in project apicurio-data-models by Apicurio.

the class ReplaceSecurityRequirementCommand method undo.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void undo(Document document) {
    LoggerCompat.info("[ReplaceSecurityRequirementCommand] Reverting.");
    if (!this._replaced) {
        return;
    }
    ISecurityRequirementParent parent = (ISecurityRequirementParent) this._parentPath.resolve(document);
    if (this.isNullOrUndefined(parent)) {
        return;
    }
    SecurityRequirement replacementRequirement = parent.createSecurityRequirement();
    Library.readNode(this._newRequirement, replacementRequirement);
    int idx = this.indexOfRequirement(parent.getSecurityRequirements(), replacementRequirement);
    if (idx == -1) {
        return;
    }
    SecurityRequirement originalRequirement = parent.createSecurityRequirement();
    Library.readNode(this._oldRequirement, originalRequirement);
    parent.getSecurityRequirements().set(idx, originalRequirement);
}
Also used : ISecurityRequirementParent(io.apicurio.datamodels.core.models.common.ISecurityRequirementParent) SecurityRequirement(io.apicurio.datamodels.core.models.common.SecurityRequirement)

Example 5 with SecurityRequirement

use of io.apicurio.datamodels.core.models.common.SecurityRequirement 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

SecurityRequirement (io.apicurio.datamodels.core.models.common.SecurityRequirement)11 ISecurityRequirementParent (io.apicurio.datamodels.core.models.common.ISecurityRequirementParent)9 Aai20Document (io.apicurio.datamodels.asyncapi.v2.models.Aai20Document)1 Aai20SecurityScheme (io.apicurio.datamodels.asyncapi.v2.models.Aai20SecurityScheme)1 CombinedVisitorAdapter (io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter)1 SecurityScheme (io.apicurio.datamodels.core.models.common.SecurityScheme)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