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;
}
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);
}
}
}
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);
}
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());
}
}
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;
});
}
Aggregations