Skip to content

Commit

Permalink
Added new parent interface for parameter parent
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWittmann committed Jan 8, 2025
1 parent f0f7e1e commit a9966f9
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 165 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<version.org.codehaus.mojo.exec-maven-plugin>3.5.0</version.org.codehaus.mojo.exec-maven-plugin>
<version.com.github.eirslett.frontend-maven-plugin>1.15.1</version.com.github.eirslett.frontend-maven-plugin>
<version.org.sonatype.plugins.nexus-staging-maven-plugin>1.7.0</version.org.sonatype.plugins.nexus-staging-maven-plugin>
<version.apicurio-unified-model-generator-plugin>1.2.6-SNAPSHOT</version.apicurio-unified-model-generator-plugin>
<version.apicurio-unified-model-generator-plugin>1.2.6</version.apicurio-unified-model-generator-plugin>
<version.jsweet-maven-plugin>3.1.1.RedHat</version.jsweet-maven-plugin>
</properties>

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/io/apicurio/datamodels/cmd/CommandFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.apicurio.datamodels.models.openapi.OpenApiMediaType;
import io.apicurio.datamodels.models.openapi.OpenApiOperation;
import io.apicurio.datamodels.models.openapi.OpenApiParameter;
import io.apicurio.datamodels.models.openapi.OpenApiParametersParent;
import io.apicurio.datamodels.models.openapi.OpenApiPathItem;
import io.apicurio.datamodels.models.openapi.OpenApiResponse;
import io.apicurio.datamodels.models.openapi.OpenApiServersParent;
Expand Down Expand Up @@ -138,11 +139,11 @@ public static ICommand createDeleteAllPathItemOperationsCommand(OpenApiPathItem
}

public static ICommand createDeleteAllPathItemParametersCommand(OpenApiPathItem parent, String type) {
return new DeleteAllParametersCommand(parent, type);
return new DeleteAllParametersCommand((OpenApiParametersParent) parent, type);
}

public static ICommand createDeleteAllOperationParametersCommand(OpenApiOperation parent, String type) {
return new DeleteAllParametersCommand(parent, type);
return new DeleteAllParametersCommand((OpenApiParametersParent) parent, type);
}

public static ICommand createDeleteAllPropertiesCommand(Schema schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
import io.apicurio.datamodels.cmd.AbstractCommand;
import io.apicurio.datamodels.models.Document;
import io.apicurio.datamodels.models.Node;
import io.apicurio.datamodels.models.Operation;
import io.apicurio.datamodels.models.openapi.OpenApiOperation;
import io.apicurio.datamodels.models.openapi.OpenApiParameter;
import io.apicurio.datamodels.models.openapi.OpenApiPathItem;
import io.apicurio.datamodels.models.visitors.CombinedVisitorAdapter;
import io.apicurio.datamodels.models.openapi.OpenApiParametersParent;
import io.apicurio.datamodels.paths.NodePath;
import io.apicurio.datamodels.paths.NodePathUtil;
import io.apicurio.datamodels.util.LoggerUtil;
Expand All @@ -32,13 +29,8 @@ public class DeleteAllParametersCommand extends AbstractCommand {
public DeleteAllParametersCommand() {
}

public DeleteAllParametersCommand(OpenApiPathItem parent, String type) {
this._parentPath = Library.createNodePath(parent);
this._paramType = type;
}

public DeleteAllParametersCommand(OpenApiOperation parent, String type) {
this._parentPath = Library.createNodePath(parent);
public DeleteAllParametersCommand(OpenApiParametersParent parent, String type) {
this._parentPath = Library.createNodePath((Node) parent);
this._paramType = type;
}

Expand All @@ -50,8 +42,8 @@ public void execute(Document document) {
LoggerUtil.info("[DeleteAllParameters] Executing.");
this._oldParams = new ArrayList<>();

Node parent = NodePathUtil.resolveNodePath(this._parentPath, document);
List<OpenApiParameter> parameters = getParameters(parent);
OpenApiParametersParent parent = (OpenApiParametersParent) NodePathUtil.resolveNodePath(this._parentPath, document);
List<OpenApiParameter> parameters = parent.getParameters();
if (this.isNullOrUndefined(parent) || this.isNullOrUndefined(parameters) || parameters.isEmpty()) {
return;
}
Expand All @@ -70,7 +62,7 @@ public void execute(Document document) {
}

paramsToRemove.forEach(paramToRemove -> {
parameters.remove(paramToRemove);
parent.removeParameter(paramToRemove);
});
}

Expand All @@ -85,79 +77,16 @@ public void undo(Document document) {
return;
}

Node parent = NodePathUtil.resolveNodePath(this._parentPath, document);
OpenApiParametersParent parent = (OpenApiParametersParent) NodePathUtil.resolveNodePath(this._parentPath, document);
if (this.isNullOrUndefined(parent)) {
return;
}

this._oldParams.forEach(paramObj -> {
OpenApiParameter parameter = createParameter(parent);
OpenApiParameter parameter = parent.createParameter();
Library.readNode(paramObj, parameter);
addParameter(parent, parameter);
parent.addParameter(parameter);
});
}

private void addParameter(Node parent, OpenApiParameter parameter) {
AddParameterVisitor gpv = new AddParameterVisitor(parameter);
parent.accept(gpv);
}

private OpenApiParameter createParameter(Node parent) {
CreateParameterVisitor gpv = new CreateParameterVisitor();
parent.accept(gpv);
return gpv._param;
}

private List<OpenApiParameter> getParameters(Node parent) {
GetParametersVisitor gpv = new GetParametersVisitor();
parent.accept(gpv);
return gpv._params;
}

private class GetParametersVisitor extends CombinedVisitorAdapter {
List<OpenApiParameter> _params;

@Override
public void visitPathItem(OpenApiPathItem node) {
this._params = node.getParameters();
}

@Override
public void visitOperation(Operation node) {
this._params = ((OpenApiOperation) node).getParameters();
}
}

private class CreateParameterVisitor extends CombinedVisitorAdapter {
OpenApiParameter _param;

@Override
public void visitPathItem(OpenApiPathItem node) {
this._param = node.createParameter();
}

@Override
public void visitOperation(Operation node) {
this._param = ((OpenApiOperation) node).createParameter();
}
}

private class AddParameterVisitor extends CombinedVisitorAdapter {
OpenApiParameter _param;

public AddParameterVisitor(OpenApiParameter param) {
this._param = param;
}

@Override
public void visitPathItem(OpenApiPathItem node) {
node.addParameter(_param);
}

@Override
public void visitOperation(Operation node) {
((OpenApiOperation) node).addParameter(_param);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import io.apicurio.datamodels.models.openapi.OpenApiExample;
import io.apicurio.datamodels.models.openapi.OpenApiHeader;
import io.apicurio.datamodels.models.openapi.OpenApiOAuthFlow;
import io.apicurio.datamodels.models.openapi.OpenApiParametersParent;
import io.apicurio.datamodels.models.openapi.OpenApiPathItem;
import io.apicurio.datamodels.models.openapi.OpenApiPaths;
import io.apicurio.datamodels.models.openapi.OpenApiResponse;
Expand Down Expand Up @@ -419,18 +420,18 @@ public void visitParameter(Parameter node) {

// Now we have normal handling of a parameter, examples include path params, query params, header params, etc.
Node parent30 = this.lookup(param20.parent());
OpenApi30Parameter param30 = createAndAddParameter(parent30);
OpenApi30Parameter param30 = createAndAddParameter((OpenApiParametersParent) parent30);
this.transformParam(param20, param30);

this.copyExtensions(param20, param30);
this.mapNode(param20, param30);
}
}

private OpenApi30Parameter createAndAddParameter(Node parent30) {
OpenApiParameterCreator paramCreator = new OpenApiParameterCreator();
parent30.accept(paramCreator);
return (OpenApi30Parameter) paramCreator.parameter;
private OpenApi30Parameter createAndAddParameter(OpenApiParametersParent parent30) {
OpenApi30Parameter newParam = (OpenApi30Parameter) parent30.createParameter();
parent30.addParameter(newParam);
return newParam;
}

private OpenApi30Parameter transformParam(OpenApi20Parameter node, OpenApi30Parameter param30) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import io.apicurio.datamodels.models.openapi.OpenApiOperation;
import io.apicurio.datamodels.models.openapi.OpenApiParameter;
import io.apicurio.datamodels.models.openapi.OpenApiPathItem;
import io.apicurio.datamodels.models.openapi.OpenApiParametersParent;
import io.apicurio.datamodels.refs.ReferenceUtil;
import io.apicurio.datamodels.validation.ValidationRule;
import io.apicurio.datamodels.validation.ValidationRuleMetaData;
Expand Down Expand Up @@ -56,7 +56,7 @@ protected boolean isValidMultipartType(String typeName) {
*/
protected List<OpenApiParameter> mergeParameters(OpenApiOperation node) {
Map<String, OpenApiParameter> indexedParams = new HashMap<>();
OpenApiPathItem parentNode = (OpenApiPathItem) node.parent();
OpenApiParametersParent parentNode = (OpenApiParametersParent) node.parent();
// Get the parameters from pathItem
if (hasValue(parentNode.getParameters())) {
for (OpenApiParameter param : parentNode.getParameters()) {
Expand All @@ -68,8 +68,8 @@ protected List<OpenApiParameter> mergeParameters(OpenApiOperation node) {
}
}
// Overwrite parameters from parent
if (hasValue(node.getParameters())) {
for (OpenApiParameter param : node.getParameters()) {
if (hasValue(((OpenApiParametersParent) node).getParameters())) {
for (OpenApiParameter param : ((OpenApiParametersParent) node).getParameters()) {
OpenApiParameter resolutionResult = (OpenApiParameter) ReferenceUtil.resolveNodeRef(param);
if (hasValue(resolutionResult)) {
String key = resolutionResult.getIn() + "-" + resolutionResult.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

package io.apicurio.datamodels.validation.rules.other;

import java.util.List;

import io.apicurio.datamodels.models.Parameter;
import io.apicurio.datamodels.models.openapi.OpenApiParameter;
import io.apicurio.datamodels.models.openapi.OpenApiParametersParent;
import io.apicurio.datamodels.validation.ValidationRule;
import io.apicurio.datamodels.validation.ValidationRuleMetaData;
import io.apicurio.datamodels.visitors.ParametersFromParentVisitor;

import java.util.List;

/**
* Implements the Body Parameter Uniqueness validation rule (can only have 1 body param).
Expand All @@ -45,9 +45,7 @@ public OasBodyParameterUniquenessValidationRule(ValidationRuleMetaData ruleInfo)
public void visitParameter(Parameter node) {
OpenApiParameter param = (OpenApiParameter) node;
if (equals(param.getIn(), "body")) {
ParametersFromParentVisitor paramsViz = new ParametersFromParentVisitor();
node.parent().accept(paramsViz);
List<OpenApiParameter> params = paramsViz.getParameters();
List<OpenApiParameter> params = ((OpenApiParametersParent) node.parent()).getParameters();
int count = countBodyParams(params);
this.reportIf(count > 1, node, "in", map());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.apicurio.datamodels.models.Operation;
import io.apicurio.datamodels.models.openapi.OpenApiOperation;
import io.apicurio.datamodels.models.openapi.OpenApiParameter;
import io.apicurio.datamodels.models.openapi.OpenApiParametersParent;
import io.apicurio.datamodels.models.openapi.OpenApiPathItem;
import io.apicurio.datamodels.refs.ReferenceUtil;
import io.apicurio.datamodels.validation.ValidationRule;
Expand Down Expand Up @@ -58,9 +59,9 @@ private int getParamCount(List<OpenApiParameter> params, String paramName, Strin

/**
* Validates that all parameter name and "in" combinations are unique
* @param params
*/
private void ensureUnique(List<OpenApiParameter> params) {
private void ensureUnique(OpenApiParametersParent paramsParent) {
List<OpenApiParameter> params = paramsParent.getParameters();
if (!hasValue(params)) {
return;
}
Expand Down Expand Up @@ -96,7 +97,7 @@ private void ensureUnique(List<OpenApiParameter> params) {
*/
@Override
public void visitPathItem(OpenApiPathItem node) {
this.ensureUnique(node.getParameters());
this.ensureUnique((OpenApiParametersParent) node);
}

/**
Expand All @@ -105,7 +106,7 @@ public void visitPathItem(OpenApiPathItem node) {
@Override
public void visitOperation(Operation node) {
OpenApiOperation op = (OpenApiOperation) node;
this.ensureUnique(op.getParameters());
this.ensureUnique((OpenApiParametersParent) op);
}

}

This file was deleted.

6 changes: 6 additions & 0 deletions src/main/resources/specs/openapi/openapi-2.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ traits:
properties:
- name: '$ref'
type: string
- name: ParametersParent
properties:
- name: parameters
type: '[Parameter]'
- name: SchemaLike
transparent: true
properties:
Expand Down Expand Up @@ -152,6 +156,7 @@ entities:
traits:
- Extensible
- Referenceable
- ParametersParent
properties:
- name: get
type: Operation
Expand All @@ -177,6 +182,7 @@ entities:
- name: Operation
traits:
- Extensible
- ParametersParent
properties:
- name: tags
type: '[string]'
Expand Down
Loading

0 comments on commit a9966f9

Please sign in to comment.