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