Skip to content

Commit

Permalink
Added support for AsyncAPI 3.0 (#792)
Browse files Browse the repository at this point in the history
* Added support for AsyncAPI 3.0

* Add missing functions from jsonutil.ts

* Upgrade umd dep
  • Loading branch information
EricWittmann authored Jul 23, 2024
1 parent 1d7471c commit 1924aa9
Show file tree
Hide file tree
Showing 44 changed files with 9,823 additions and 88 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.3.0</version.org.codehaus.mojo.exec-maven-plugin>
<version.com.github.eirslett.frontend-maven-plugin>1.15.0</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.0.14.Final</version.apicurio-unified-model-generator-plugin>
<version.apicurio-unified-model-generator-plugin>1.1.1.Final</version.apicurio-unified-model-generator-plugin>
<version.jsweet-maven-plugin>3.1.1.RedHat</version.jsweet-maven-plugin>
</properties>

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/apicurio/datamodels/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static Document readDocumentFromJSONString(String jsonString) {

/**
* Called to serialize a given data model node to a JSON object.
* @param node
* @param document
*/
public static ObjectNode writeDocument(Document document) {
ModelWriter writer = ModelWriterFactory.createModelWriter(document.root().modelType());
Expand Down Expand Up @@ -186,7 +186,7 @@ public static NodePath createNodePath(Node node) {

/**
* Called to create a node path instance for a stringified node path.
* @param node
* @param path
*/
public static NodePath parseNodePath(String path) {
return NodePathUtil.parseNodePath(path);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/apicurio/datamodels/ModelTypeDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public static ModelType discoverModelType(ObjectNode json) {
return ModelType.ASYNCAPI25;
} else if (asyncapi.startsWith("2.6")) {
return ModelType.ASYNCAPI26;
} else if (asyncapi.startsWith("3.0")) {
return ModelType.ASYNCAPI30;
} else {
throw new RuntimeException("Unknown/unsupported AsyncAPI version: " + asyncapi);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/apicurio/datamodels/VisitorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.apicurio.datamodels.models.asyncapi.v24.visitors.AsyncApi24Traverser;
import io.apicurio.datamodels.models.asyncapi.v25.visitors.AsyncApi25Traverser;
import io.apicurio.datamodels.models.asyncapi.v26.visitors.AsyncApi26Traverser;
import io.apicurio.datamodels.models.asyncapi.v30.visitors.AsyncApi30Traverser;
import io.apicurio.datamodels.models.openapi.v20.visitors.OpenApi20Traverser;
import io.apicurio.datamodels.models.openapi.v30.visitors.OpenApi30Traverser;
import io.apicurio.datamodels.models.openapi.v31.visitors.OpenApi31Traverser;
Expand Down Expand Up @@ -65,6 +66,9 @@ public static void visitTree(Node node, Visitor visitor, TraverserDirection dire
case ASYNCAPI26:
traverser = new AsyncApi26Traverser(visitor);
break;
case ASYNCAPI30:
traverser = new AsyncApi30Traverser(visitor);
break;
case OPENAPI20:
traverser = new OpenApi20Traverser(visitor);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import io.apicurio.datamodels.models.Operation;
import io.apicurio.datamodels.models.Schema;
import io.apicurio.datamodels.models.Tag;
import io.apicurio.datamodels.models.openapi.OpenApiDocument;
import io.apicurio.datamodels.models.openapi.OpenApiExternalDocumentation;
import io.apicurio.datamodels.models.visitors.CombinedVisitorAdapter;

public class ExternalDocsCreator extends CombinedVisitorAdapter {
Expand All @@ -19,8 +21,8 @@ public void visitSchema(Schema node) {

@Override
public void visitDocument(Document node) {
externalDocs = node.createExternalDocumentation();
node.setExternalDocs(externalDocs);
externalDocs = ((OpenApiDocument) node).createExternalDocumentation();
((OpenApiDocument) node).setExternalDocs((OpenApiExternalDocumentation) externalDocs);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@
import io.apicurio.datamodels.models.Tag;
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.OpenApiPathItem;
import io.apicurio.datamodels.models.openapi.OpenApiPaths;
import io.apicurio.datamodels.models.openapi.OpenApiResponse;
import io.apicurio.datamodels.models.openapi.OpenApiResponses;
import io.apicurio.datamodels.models.openapi.OpenApiSchema;
import io.apicurio.datamodels.models.openapi.OpenApiTag;
import io.apicurio.datamodels.models.openapi.OpenApiXML;
import io.apicurio.datamodels.models.openapi.v20.OpenApi20Contact;
import io.apicurio.datamodels.models.openapi.v20.OpenApi20Definitions;
Expand Down Expand Up @@ -139,7 +141,7 @@ public OpenApi30Document getResult() {
}

/**
* @see io.apicurio.datamodels.models.visitors.Visitor#visitDocument(io.apicurio.datamodels.core.models.Document)
* @see io.apicurio.datamodels.models.visitors.Visitor#visitDocument(Document)
*/
@Override
public void visitDocument(Document node) {
Expand Down Expand Up @@ -176,7 +178,7 @@ public void visitDocument(Document node) {
}

/**
* @see io.apicurio.datamodels.models.visitors.Visitor#visitInfo(io.apicurio.datamodels.core.models.common.Info)
* @see io.apicurio.datamodels.models.visitors.Visitor#visitInfo(Info)
*/
@Override
public void visitInfo(Info node) {
Expand All @@ -194,7 +196,7 @@ public void visitInfo(Info node) {
}

/**
* @see io.apicurio.datamodels.models.visitors.Visitor#visitContact(io.apicurio.datamodels.core.models.common.Contact)
* @see io.apicurio.datamodels.models.visitors.Visitor#visitContact(Contact)
*/
@Override
public void visitContact(Contact node) {
Expand All @@ -210,7 +212,7 @@ public void visitContact(Contact node) {
}

/**
* @see io.apicurio.datamodels.models.visitors.Visitor#visitLicense(io.apicurio.datamodels.core.models.common.License)
* @see io.apicurio.datamodels.models.visitors.Visitor#visitLicense(License)
*/
@Override
public void visitLicense(License node) {
Expand Down Expand Up @@ -249,7 +251,7 @@ public void visitPathItem(OpenApiPathItem node) {
}

/**
* @see io.apicurio.datamodels.models.visitors.Visitor#visitOperation(io.apicurio.datamodels.core.models.common.Operation)
* @see io.apicurio.datamodels.models.visitors.Visitor#visitOperation(Operation)
*/
@Override
public void visitOperation(Operation node) {
Expand Down Expand Up @@ -311,7 +313,7 @@ public void visitOperation(Operation node) {
}

/**
* @see io.apicurio.datamodels.models.visitors.Visitor#visitParameter(io.apicurio.datamodels.core.models.common.Parameter)
* @see io.apicurio.datamodels.models.visitors.Visitor#visitParameter(Parameter)
*/
@Override
public void visitParameter(Parameter node) {
Expand Down Expand Up @@ -484,7 +486,7 @@ public void visitParameterDefinition(Parameter node) {
}

/**
* @see io.apicurio.datamodels.models.visitors.Visitor#visitExternalDocumentation(io.apicurio.datamodels.core.models.common.ExternalDocumentation)
* @see io.apicurio.datamodels.models.visitors.Visitor#visitExternalDocumentation(ExternalDocumentation)
*/
@Override
public void visitExternalDocumentation(ExternalDocumentation node) {
Expand All @@ -501,9 +503,6 @@ public void visitExternalDocumentation(ExternalDocumentation node) {
this.mapNode(node, externalDocs30);
}

/**
* @see io.apicurio.datamodels.models.visitors.Visitor#visitSecurityRequirement(io.apicurio.datamodels.core.models.common.SecurityRequirement)
*/
@Override
public void visitSecurityRequirement(SecurityRequirement node) {
OpenApi20SecurityRequirement req = (OpenApi20SecurityRequirement) node;
Expand Down Expand Up @@ -598,7 +597,7 @@ private void transformResponse(OpenApi20Response node, OpenApi30Response respons
}

/**
* @see io.apicurio.datamodels.models.visitors.Visitor#visitSchema(io.apicurio.datamodels.core.models.common.Schema)
* @see io.apicurio.datamodels.models.visitors.Visitor#visitSchema(Schema)
*/
@Override
public void visitSchema(Schema node) {
Expand All @@ -620,9 +619,6 @@ public void visitSchema(Schema node) {
}
}

/**
* @see io.apicurio.datamodels.openapi.v2.visitors.IOpenApi20Visitor#visitHeaders(io.apicurio.datamodels.openapi.v2.models.OpenApi20Headers)
*/
@Override
public void visitHeaders(OpenApi20Headers node) {
OpenApi30Response parent30 = (OpenApi30Response) this.lookup(node.parent());
Expand Down Expand Up @@ -652,9 +648,6 @@ public void visitExample(OpenApiExample node) {
// Examples are processed as part of "transformResponse"
}

/**
* @see io.apicurio.datamodels.openapi.v2.visitors.IOpenApi20Visitor#visitItems(io.apicurio.datamodels.openapi.v2.models.OpenApi20Items)
*/
@Override
public void visitItems(OpenApi20Items node) {
OpenApi30Schema parent30 = this.findItemsParent(node);
Expand All @@ -667,28 +660,25 @@ public void visitItems(OpenApi20Items node) {
}

/**
* @see io.apicurio.datamodels.models.visitors.Visitor#visitTag(io.apicurio.datamodels.core.models.common.Tag)
* @see io.apicurio.datamodels.models.visitors.Visitor#visitTag(Tag)
*/
@Override
public void visitTag(Tag node) {
OpenApi30Document parent30 = this.doc30;
Tag tag30 = parent30.createTag();
OpenApiTag tag30 = parent30.createTag();
tag30.setName(node.getName());
tag30.setDescription(node.getDescription());
parent30.addTag(tag30);
this.mapNode(node, tag30);
}

/**
* @see io.apicurio.datamodels.openapi.v2.visitors.IOpenApi20Visitor#visitSecurityDefinitions(io.apicurio.datamodels.openapi.v2.models.OpenApi20SecurityDefinitions)
*/
@Override
public void visitSecurityDefinitions(OpenApi20SecurityDefinitions node) {
// OpenAPI 3 has no "Security Definitions" wrapper entity.
}

/**
* @see io.apicurio.datamodels.models.visitors.Visitor#visitSecurityScheme(io.apicurio.datamodels.core.models.common.SecurityScheme)
* @see io.apicurio.datamodels.models.visitors.Visitor#visitSecurityScheme(SecurityScheme)
*/
@Override
public void visitSecurityScheme(SecurityScheme node) {
Expand All @@ -710,9 +700,9 @@ public void visitSecurityScheme(SecurityScheme node) {
flows30.setImplicit(flows30.createOAuthFlow());
flows30.getImplicit().setAuthorizationUrl(scheme.getAuthorizationUrl());
if (!NodeUtil.isNullOrUndefined(scheme.getScopes())) {
flows30.getImplicit().setScopes(new LinkedHashMap<>());
((OpenApiOAuthFlow) flows30.getImplicit()).setScopes(new LinkedHashMap<>());
scheme.getScopes().getItemNames().forEach(scopeName -> {
flows30.getImplicit().getScopes().put(scopeName, scheme.getScopes().getItem(scopeName));
((OpenApiOAuthFlow) flows30.getImplicit()).getScopes().put(scopeName, scheme.getScopes().getItem(scopeName));
});
}
}
Expand All @@ -722,9 +712,9 @@ public void visitSecurityScheme(SecurityScheme node) {
flows30.getAuthorizationCode().setAuthorizationUrl(scheme.getAuthorizationUrl());
flows30.getAuthorizationCode().setTokenUrl(scheme.getTokenUrl());
if (!NodeUtil.isNullOrUndefined(scheme.getScopes())) {
flows30.getAuthorizationCode().setScopes(new LinkedHashMap<>());
((OpenApiOAuthFlow) flows30.getAuthorizationCode()).setScopes(new LinkedHashMap<>());
scheme.getScopes().getItemNames().forEach(scopeName -> {
flows30.getAuthorizationCode().getScopes().put(scopeName, scheme.getScopes().getItem(scopeName));
((OpenApiOAuthFlow) flows30.getAuthorizationCode()).getScopes().put(scopeName, scheme.getScopes().getItem(scopeName));
});
}
}
Expand All @@ -733,9 +723,9 @@ public void visitSecurityScheme(SecurityScheme node) {
flows30.setPassword(flows30.createOAuthFlow());
flows30.getPassword().setTokenUrl(scheme.getTokenUrl());
if (!NodeUtil.isNullOrUndefined(scheme.getScopes())) {
flows30.getPassword().setScopes(new LinkedHashMap<>());
((OpenApiOAuthFlow) flows30.getPassword()).setScopes(new LinkedHashMap<>());
scheme.getScopes().getItemNames().forEach(scopeName -> {
flows30.getPassword().getScopes().put(scopeName, scheme.getScopes().getItem(scopeName));
((OpenApiOAuthFlow) flows30.getPassword()).getScopes().put(scopeName, scheme.getScopes().getItem(scopeName));
});
}
}
Expand All @@ -744,9 +734,9 @@ public void visitSecurityScheme(SecurityScheme node) {
flows30.setClientCredentials(flows30.createOAuthFlow());
flows30.getClientCredentials().setTokenUrl(scheme.getTokenUrl());
if (!NodeUtil.isNullOrUndefined(scheme.getScopes())) {
flows30.getClientCredentials().setScopes(new LinkedHashMap<>());
((OpenApiOAuthFlow) flows30.getClientCredentials()).setScopes(new LinkedHashMap<>());
scheme.getScopes().getItemNames().forEach(scopeName -> {
flows30.getClientCredentials().getScopes().put(scopeName, scheme.getScopes().getItem(scopeName));
((OpenApiOAuthFlow) flows30.getClientCredentials()).getScopes().put(scopeName, scheme.getScopes().getItem(scopeName));
});
}
}
Expand All @@ -755,9 +745,6 @@ public void visitSecurityScheme(SecurityScheme node) {
this.mapNode(scheme, scheme30);
}

/**
* @see io.apicurio.datamodels.openapi.v2.visitors.IOpenApi20Visitor#visitScopes(io.apicurio.datamodels.openapi.v2.models.OpenApi20Scopes)
*/
@Override
public void visitScopes(OpenApi20Scopes node) {
// Note: scopes are handled during the processing of the security scheme. See `visitSecurityScheme` for details.
Expand Down Expand Up @@ -850,25 +837,16 @@ public void visitItemsSchema(OpenApiSchema node) {
this.mapNode(node, items30);
}

/**
* @see io.apicurio.datamodels.openapi.v2.visitors.IOpenApi20Visitor#visitDefinitions(io.apicurio.datamodels.openapi.v2.models.OpenApi20Definitions)
*/
@Override
public void visitDefinitions(OpenApi20Definitions node) {
// Note: there is no "definitions" entity in 3.0, so nothing to do here.
}

/**
* @see io.apicurio.datamodels.openapi.v2.visitors.IOpenApi20Visitor#visitParameterDefinitions(io.apicurio.datamodels.openapi.v2.models.OpenApi20ParameterDefinitions)
*/
@Override
public void visitParameterDefinitions(OpenApi20ParameterDefinitions node) {
// Note: there is no "parameters definitions" entity in 3.0, so nothing to do here.
}

/**
* @see io.apicurio.datamodels.openapi.v2.visitors.IOpenApi20Visitor#visitResponseDefinitions(io.apicurio.datamodels.openapi.v2.models.OpenApi20ResponseDefinitions)
*/
@Override
public void visitResponseDefinitions(OpenApi20ResponseDefinitions node) {
// Note: there is no "responses definitions" entity in 3.0, so nothing to do here.
Expand Down
Loading

0 comments on commit 1924aa9

Please sign in to comment.