Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Parser-API version 2 support #1017

Merged
merged 9 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The `generator` property from `package.json` file must contain a JSON object tha
"generator":
{
"renderer": "react",
"apiVersion": "v1",
"apiVersion": "v2",
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
"supportedProtocols": ["amqp", "mqtt"],
"parameters": {
"server": {
Expand Down
2 changes: 1 addition & 1 deletion docs/generator-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ The **package.json** file is used to define the dependencies for your template.
"description": "A template that generates a Python MQTT client using MQTT.",
"generator": {
"renderer": "react",
"apiVersion": "v1",
"apiVersion": "v2",
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
"generator": ">=1.10.0 <2.0.0",
"supportedProtocols": ["mqtt"]
},
Expand Down
6 changes: 3 additions & 3 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class Generator {
validateTemplateConfig(this.templateConfig, this.templateParams, asyncapiDocument);
await this.configureTemplate();

// use new or old document API based on `templateConfig.apiVersion` value
// use the expected document API based on `templateConfig.apiVersion` value
this.asyncapi = asyncapiDocument = getProperApiDocument(asyncapiDocument, this.templateConfig);

if (!isReactTemplate(this.templateConfig)) {
Expand Down Expand Up @@ -261,7 +261,7 @@ class Generator {
/** @type {AsyncAPIDocument} Parsed AsyncAPI schema. See {@link https://github.com/asyncapi/parser-js/blob/master/API.md#module_@asyncapi/parser+AsyncAPIDocument|AsyncAPIDocument} for details on object structure. */
const { document, diagnostics } = await parse(asyncapiString, parseOptions, this);
if (!document) {
const err = new Error('Input is not a corrent AsyncAPI document so it cannot be processed.');
const err = new Error('Input is not a correct AsyncAPI document so it cannot be processed.');
err.diagnostics = diagnostics;
throw err;
}
Expand Down Expand Up @@ -436,7 +436,7 @@ class Generator {
}
});
}

if (asyncapiDocument.hasComponents()) {
for (const [key, value] of Object.entries(asyncapiDocument.components().parameters())) {
parameters.set(key, value);
Expand Down
50 changes: 32 additions & 18 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
const fs = require('fs');

const { Parser, convertToOldAPI } = require('@asyncapi/parser/cjs');
const { OpenAPISchemaParser } = require('@asyncapi/openapi-schema-parser');
const { AvroSchemaParser } = require('@asyncapi/avro-schema-parser');
const { RamlDTSchemaParser } = require('@asyncapi/raml-dt-schema-parser');
const { ProtoBuffSchemaParser } = require('@asyncapi/protobuf-schema-parser');
const { convertToOldAPI } = require('@asyncapi/parser');
const { ConvertDocumentParserAPIVersion, NewParser } = require('@smoya/multi-parser');

const parser = module.exports;

const defaultParser = new Parser({
schemaParsers: [
OpenAPISchemaParser(),
AvroSchemaParser(),
RamlDTSchemaParser(),
ProtoBuffSchemaParser(),
],
});
/**
* Conver the template defined value `apiVersion: 'v1'` to only contain the numeric value `1`.
*/
parser.sanitizeTemplateApiVersion = (apiVersion) => {
if (apiVersion && apiVersion.length > 1) {
return apiVersion.substring('1');
}
return apiVersion;
};

parser.parse = (asyncapi, oldOptions, generator) => {
let apiVersion = this.sanitizeTemplateApiVersion(generator.templateConfig.apiVersion);
// Defaulting to v1 parser to convert it to the old parserAPI afterwards.
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
if (!this.usesNewAPI(generator.templateConfig)) {
apiVersion = '1';
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
}
const options = convertOldOptionsToNew(oldOptions, generator);
return defaultParser.parse(asyncapi, options);
const parser = NewParser(apiVersion, {parserOptions: options, includeSchemaParsers: true});
return parser.parse(asyncapi, options);
};

/**
* If the template expect one of the new parser API versions, it must be above 0
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
*/
parser.usesNewAPI = (templateConfig = {}) => {
return templateConfig.apiVersion === 'v1';
return Number(this.sanitizeTemplateApiVersion(templateConfig.apiVersion)) > 0;
};

parser.getProperApiDocument = (asyncapiDocument, templateConfig) => {
return parser.usesNewAPI(templateConfig) ? asyncapiDocument : convertToOldAPI(asyncapiDocument);
/**
* Based on the current parsed AsyncAPI document, convert it to expected API version from the template.
*/
parser.getProperApiDocument = (asyncapiDocument, templateConfig = {}) => {
const apiVersion = this.sanitizeTemplateApiVersion(templateConfig.apiVersion);
if (apiVersion === undefined) {
// Convert to old version
jonaslagoni marked this conversation as resolved.
Show resolved Hide resolved
return convertToOldAPI(asyncapiDocument);
}
return ConvertDocumentParserAPIVersion(asyncapiDocument, apiVersion);
};

// The new options for the v2 Parser are different from those for the v1 version, but in order not to release Generator v2, we are converting the old options of Parser to the new ones.
Expand Down
1 change: 1 addition & 0 deletions lib/templateConfigValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const ajv = new Ajv({ allErrors: true });
// See https://github.com/asyncapi/parser-api
const supportedParserAPIMajorVersions = [
'v1',
'v2'
];

/**
Expand Down
Loading