Search in sources :

Example 1 with IOasParameterParent

use of io.apicurio.datamodels.openapi.models.IOasParameterParent in project apicurio-data-models by Apicurio.

the class NewParamCommand method execute.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#execute(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void execute(Document document) {
    LoggerCompat.info("[NewParamCommand] Executing.");
    this._created = false;
    IOasParameterParent parent = (IOasParameterParent) this._parentPath.resolve(document);
    if (this.isNullOrUndefined(parent)) {
        LoggerCompat.info("[NewParamCommand] Parent node (operation or path item) is null.");
        return;
    }
    if (this.hasParam(this._paramName, this._paramType, parent)) {
        LoggerCompat.info("[NewParamCommand] Param %s of type %s already exists.", this._paramName, this._paramType);
        return;
    }
    List<OasParameter> parameters = parent.getParameters();
    if (this.isNullOrUndefined(parameters)) {
        parameters = new ArrayList<>();
        NodeCompat.setProperty(parent, Constants.PROP_PARAMETERS, parameters);
    }
    OasParameter param = parent.createParameter();
    boolean configured = false;
    // If overriding a param from the path level, clone it!
    if (this._override) {
        OasParameter oparam = this.findOverridableParam((OasOperation) parent);
        if (ModelUtils.isDefined(oparam)) {
            Library.readNode(Library.writeNode(oparam), param);
            configured = true;
        }
    }
    // If not overriding, then set the basics only.
    if (!configured) {
        param.in = this._paramType;
        param.name = this._paramName;
        if (NodeCompat.equals(param.in, "path")) {
            param.required = true;
        }
        if (ModelUtils.isDefined(this._description)) {
            param.description = this._description;
        }
        if (ModelUtils.isDefined(this._newType)) {
            this._setParameterType(param);
        }
    }
    parent.addParameter(param);
    LoggerCompat.info("[NewParamCommand] Param %s of type %s created successfully.", param.name, param.in);
    this._created = true;
}
Also used : OasParameter(io.apicurio.datamodels.openapi.models.OasParameter) IOasParameterParent(io.apicurio.datamodels.openapi.models.IOasParameterParent)

Example 2 with IOasParameterParent

use of io.apicurio.datamodels.openapi.models.IOasParameterParent in project apicurio-data-models by Apicurio.

the class NewParamCommand method undo.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void undo(Document document) {
    LoggerCompat.info("[NewParamCommand] Reverting.");
    if (!this._created) {
        return;
    }
    IOasParameterParent parent = (IOasParameterParent) this._parentPath.resolve(document);
    if (this.isNullOrUndefined(parent)) {
        return;
    }
    OasParameter theParam = null;
    List<OasParameter> parameters = parent.getParameters();
    for (OasParameter param : parameters) {
        if (NodeCompat.equals(param.in, this._paramType) && NodeCompat.equals(param.name, this._paramName)) {
            theParam = param;
            break;
        }
    }
    // If found, remove it from the params.
    if (ModelUtils.isDefined(theParam)) {
        parameters.remove(parameters.indexOf(theParam));
        if (parameters.size() == 0) {
            NodeCompat.setProperty(parent, Constants.PROP_PARAMETERS, null);
        }
    }
}
Also used : OasParameter(io.apicurio.datamodels.openapi.models.OasParameter) IOasParameterParent(io.apicurio.datamodels.openapi.models.IOasParameterParent)

Example 3 with IOasParameterParent

use of io.apicurio.datamodels.openapi.models.IOasParameterParent in project apicurio-data-models by Apicurio.

the class ChangeParameterTypeCommand method undo.

/**
 * @see io.apicurio.datamodels.cmd.ICommand#undo(io.apicurio.datamodels.core.models.Document)
 */
@Override
public void undo(Document document) {
    LoggerCompat.info("[ChangeParameterTypeCommand] Reverting.");
    Parameter param = (Parameter) this._paramPath.resolve(document);
    if (this.isNullOrUndefined(param)) {
        return;
    }
    IOasParameterParent parent = (IOasParameterParent) param.parent();
    Parameter oldParam = parent.createParameter();
    Library.readNode(this._oldParameter, oldParam);
    this.doRestoreParameter(param, oldParam);
// let pindex: number = parent.parameters.indexOf(param);
// parent.parameters.splice(pindex, 1, oldParam);
}
Also used : IOasParameterParent(io.apicurio.datamodels.openapi.models.IOasParameterParent) Parameter(io.apicurio.datamodels.core.models.common.Parameter)

Example 4 with IOasParameterParent

use of io.apicurio.datamodels.openapi.models.IOasParameterParent in project apicurio-data-models by Apicurio.

the class OasBodyParameterUniquenessValidationRule method visitParameter.

/**
 * @see io.apicurio.datamodels.combined.visitors.CombinedAllNodeVisitor#visitParameter(io.apicurio.datamodels.core.models.common.Parameter)
 */
@Override
public void visitParameter(Parameter node) {
    OasParameter param = (OasParameter) node;
    List<OasParameter> params = ((IOasParameterParent) (node.parent())).getParameters();
    if (equals(param.in, "body")) {
        int count = countBodyParams(params);
        this.reportIf(count > 1, node, Constants.PROP_IN, map());
    }
}
Also used : OasParameter(io.apicurio.datamodels.openapi.models.OasParameter) IOasParameterParent(io.apicurio.datamodels.openapi.models.IOasParameterParent)

Example 5 with IOasParameterParent

use of io.apicurio.datamodels.openapi.models.IOasParameterParent 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)

Aggregations

IOasParameterParent (io.apicurio.datamodels.openapi.models.IOasParameterParent)10 OasParameter (io.apicurio.datamodels.openapi.models.OasParameter)8 ArrayList (java.util.ArrayList)3 Operation (io.apicurio.datamodels.core.models.common.Operation)2 Parameter (io.apicurio.datamodels.core.models.common.Parameter)2 OasPathItem (io.apicurio.datamodels.openapi.models.OasPathItem)2 Library (io.apicurio.datamodels.Library)1 CombinedVisitorAdapter (io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter)1 JsonCompat (io.apicurio.datamodels.compat.JsonCompat)1 NodeCompat (io.apicurio.datamodels.compat.NodeCompat)1 Constants (io.apicurio.datamodels.core.Constants)1 Document (io.apicurio.datamodels.core.models.Document)1 DocumentType (io.apicurio.datamodels.core.models.DocumentType)1 ExtensibleNode (io.apicurio.datamodels.core.models.ExtensibleNode)1 Extension (io.apicurio.datamodels.core.models.Extension)1 IReferenceNode (io.apicurio.datamodels.core.models.IReferenceNode)1 Node (io.apicurio.datamodels.core.models.Node)1 NodePath (io.apicurio.datamodels.core.models.NodePath)1 ValidationProblem (io.apicurio.datamodels.core.models.ValidationProblem)1 Contact (io.apicurio.datamodels.core.models.common.Contact)1