diff --git a/lib/generator.js b/lib/generator.js index 8b3e4b276..2caf855ac 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -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; } diff --git a/lib/parser.js b/lib/parser.js index 303022732..9b6fc82a5 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -1,5 +1,5 @@ const fs = require('fs'); -const { convertToOldAPI } = require('@asyncapi/parser/cjs'); +const { convertToOldAPI } = require('@asyncapi/parser'); const { ConvertDocumentParserAPIVersion, NewParser } = require('@smoya/multi-parser'); const parser = module.exports; @@ -7,16 +7,21 @@ const parser = module.exports; /** * Conver the template defined value `apiVersion: 'v1'` to only contain the numeric value `1`. */ -function sanitizeTemplateApiVersion(apiVersion) { +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. + if (!this.usesNewAPI(generator.templateConfig)) { + apiVersion = '1'; + } const options = convertOldOptionsToNew(oldOptions, generator); - const parser = NewParser('1', options); + const parser = NewParser(apiVersion, {parserOptions: options, includeSchemaParsers: true}); return parser.parse(asyncapi, options); }; @@ -24,14 +29,14 @@ parser.parse = (asyncapi, oldOptions, generator) => { * If the template expect one of the new parser API versions, it must be above 0 */ parser.usesNewAPI = (templateConfig = {}) => { - return Number(sanitizeTemplateApiVersion(templateConfig.apiVersion)) > 0; + return Number(this.sanitizeTemplateApiVersion(templateConfig.apiVersion)) > 0; }; /** * Based on the current parsed AsyncAPI document, convert it to expected API version from the template. */ parser.getProperApiDocument = (asyncapiDocument, templateConfig = {}) => { - const apiVersion = sanitizeTemplateApiVersion(templateConfig.apiVersion); + const apiVersion = this.sanitizeTemplateApiVersion(templateConfig.apiVersion); if (apiVersion === undefined) { // Convert to old version return convertToOldAPI(asyncapiDocument); diff --git a/package-lock.json b/package-lock.json index a13fc2399..710646df3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,12 +9,10 @@ "version": "1.10.12", "license": "Apache-2.0", "dependencies": { - "@asyncapi/avro-schema-parser": "^3.0.2", "@asyncapi/generator-react-sdk": "^0.2.23", - "@asyncapi/openapi-schema-parser": "^3.0.4", - "@asyncapi/raml-dt-schema-parser": "^4.0.4", + "@asyncapi/parser": "2.1.0", "@npmcli/arborist": "^2.2.4", - "@smoya/multi-parser": "2.0.0", + "@smoya/multi-parser": "3.0.0", "ajv": "^8.12.0", "chokidar": "^3.4.0", "commander": "^6.1.0", @@ -96,11 +94,11 @@ } }, "node_modules/@asyncapi/avro-schema-parser": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/@asyncapi/avro-schema-parser/-/avro-schema-parser-3.0.2.tgz", - "integrity": "sha512-TZZedLaflgyYivwidJPqTN2LMk+Lqg0IByXtdzNYQZLNpLpcyEnXCxongoW0TVYgYGLWAghN3AmMOrrsVcGGOw==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/@asyncapi/avro-schema-parser/-/avro-schema-parser-3.0.3.tgz", + "integrity": "sha512-XprbDYPFJ0nc963hPCjbEmM3iu6ypKg/70EFVl0MZJCLbLw/+gBbPy95uV3Qaofm5UQgSI+aTobGhc8rMre4VA==", "dependencies": { - "@asyncapi/parser": "^2.0.3", + "@asyncapi/parser": "^2.1.0", "@types/json-schema": "^7.0.11", "avsc": "^5.7.6" } @@ -302,6 +300,16 @@ "webidl-conversions": "^3.0.0" } }, + "node_modules/@asyncapi/protobuf-schema-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@asyncapi/protobuf-schema-parser/-/protobuf-schema-parser-3.0.0.tgz", + "integrity": "sha512-kjoLrll611K+xYC/iBUlSnZsCHbrhL999ItVHZhObUOjUB991XgonqbSAaihiiDXTYgceOLhJKAN5llkV/LOOA==", + "dependencies": { + "@asyncapi/parser": "^2.1.0", + "@types/protocol-buffers-schema": "^3.4.1", + "protocol-buffers-schema": "^3.6.0" + } + }, "node_modules/@asyncapi/raml-dt-schema-parser": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/@asyncapi/raml-dt-schema-parser/-/raml-dt-schema-parser-4.0.4.tgz", @@ -3042,10 +3050,14 @@ } }, "node_modules/@smoya/multi-parser": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@smoya/multi-parser/-/multi-parser-2.0.0.tgz", - "integrity": "sha512-Acm0hNvcxBbMN4Hr8hzEuWP3N84K1AQvSOgJn2YAm9/BDhqNWZtPM7xSplpC+np/SGp50AeCYJcKdAeueG6zPw==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@smoya/multi-parser/-/multi-parser-3.0.0.tgz", + "integrity": "sha512-HtA/Png8FQ2vwiKKMSOqNkYGERUFqieeekwtuMsImmgnaiacyq97itehbep179qO/TvHb+3luRZAJ433fHgIxg==", "dependencies": { + "@asyncapi/avro-schema-parser": "^3.0.3", + "@asyncapi/openapi-schema-parser": "^3.0.4", + "@asyncapi/protobuf-schema-parser": "^3.0.0", + "@asyncapi/raml-dt-schema-parser": "^4.0.4", "parserv2": "npm:@asyncapi/parser@^2.1.0", "parserv3": "npm:@asyncapi/parser@^2.2.0-next-major-spec.2" } @@ -3469,6 +3481,14 @@ "integrity": "sha512-5qOlnZscTn4xxM5MeGXAMOsIOIKIbh9e85zJWfBRVPlRMEVawzoPhINYbRGkBZCI8LxvBe7tJCdWiarA99OZfQ==", "dev": true }, + "node_modules/@types/protocol-buffers-schema": { + "version": "3.4.1", + "resolved": "https://registry.npmjs.org/@types/protocol-buffers-schema/-/protocol-buffers-schema-3.4.1.tgz", + "integrity": "sha512-CBpqIDa1+/F3Z5EL8Uz/t+1eygIinJiMS37KP8O9TN+n38OlckYQhU+t/vYpsF7XhSDuiZS0zAJyfRrAeDKDUw==", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/stack-utils": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-1.0.1.tgz", @@ -11650,6 +11670,11 @@ "react-is": "^16.13.1" } }, + "node_modules/protocol-buffers-schema": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/protocol-buffers-schema/-/protocol-buffers-schema-3.6.0.tgz", + "integrity": "sha512-TdDRD+/QNdrCGCE7v8340QyuXd4kIWIgapsE2+n/SaGiSSbomYl4TjHlvIoCWRpE7wFt02EpB35VVA2ImcBVqw==" + }, "node_modules/psl": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", diff --git a/package.json b/package.json index c37567bf3..688cb1803 100644 --- a/package.json +++ b/package.json @@ -48,11 +48,9 @@ "license": "Apache-2.0", "homepage": "https://github.com/asyncapi/generator", "dependencies": { - "@asyncapi/avro-schema-parser": "^3.0.2", "@asyncapi/generator-react-sdk": "^0.2.23", - "@asyncapi/openapi-schema-parser": "^3.0.4", - "@smoya/multi-parser": "2.0.0", - "@asyncapi/raml-dt-schema-parser": "^4.0.4", + "@asyncapi/parser": "2.1.0", + "@smoya/multi-parser": "3.0.0", "@npmcli/arborist": "^2.2.4", "ajv": "^8.12.0", "chokidar": "^3.4.0", diff --git a/test/docs/dummyV3.yml b/test/docs/dummyV3.yml new file mode 100644 index 000000000..f72ea6ece --- /dev/null +++ b/test/docs/dummyV3.yml @@ -0,0 +1,31 @@ +asyncapi: 3.0.0 +info: + title: Account Service + version: 1.0.0 + description: This service is in charge of processing user signups +channels: + user/signedup: + address: user/signedup + messages: + subscribe.message: + $ref: '#/components/messages/UserSignedUp' +operations: + user/signedup.subscribe: + action: send + channel: + $ref: '#/channels/user~1signedup' + messages: + - $ref: '#/components/messages/UserSignedUp' +components: + messages: + UserSignedUp: + payload: + type: object + properties: + displayName: + type: string + description: Name of the user + email: + type: string + format: email + description: Email of the user diff --git a/test/parser.test.js b/test/parser.test.js new file mode 100644 index 000000000..311f465e8 --- /dev/null +++ b/test/parser.test.js @@ -0,0 +1,80 @@ +const fs = require('fs'); +const path = require('path'); +const { sanitizeTemplateApiVersion, usesNewAPI, parse } = require('../lib/parser'); +const dummyV2Document = fs.readFileSync(path.resolve(__dirname, './docs/dummy.yml'), 'utf8'); +const dummyV3Document = fs.readFileSync(path.resolve(__dirname, './docs/dummyV3.yml'), 'utf8'); + +describe('Parser', () => { + describe('sanitizeTemplateApiVersion', () => { + it('should return version number when given `v1` syntax', () => { + const rawVersion = 'v1'; + const expectedVersion = '1'; + const sanitizedVersion = sanitizeTemplateApiVersion(rawVersion); + + expect(sanitizedVersion).toStrictEqual(expectedVersion); + }); + it('should return version number when given `1` syntax', () => { + const rawVersion = '1'; + const expectedVersion = '1'; + const sanitizedVersion = sanitizeTemplateApiVersion(rawVersion); + + expect(sanitizedVersion).toStrictEqual(expectedVersion); + }); + }); + describe('usesNewAPI', () => { + it('should use new parser api if v1', () => { + const templateConfig = { + apiVersion: 'v1' + }; + const isUsingNewAPI = usesNewAPI(templateConfig); + + expect(isUsingNewAPI).toStrictEqual(true); + }); + it('should use new parser api if v2', () => { + const templateConfig = { + apiVersion: 'v2' + }; + const isUsingNewAPI = usesNewAPI(templateConfig); + + expect(isUsingNewAPI).toStrictEqual(true); + }); + it('should not use new API if no apiVersion', () => { + const templateConfig = { }; + const isUsingNewAPI = usesNewAPI(templateConfig); + + expect(isUsingNewAPI).toStrictEqual(false); + }); + }); + describe('parse', () => { + it('should be able to parse AsyncAPI v2 document for parser API v1', async () => { + const parsedDocument = await parse(dummyV2Document, {}, {templateConfig: {apiVersion: 'v1'}}); + + expect(parsedDocument).toBeDefined(); + expect(parsedDocument.document.version()).toEqual('2.3.0'); + }); + it('should be able to parse AsyncAPI v2 document for parser API v2', async () => { + const parsedDocument = await parse(dummyV2Document, {}, {templateConfig: {apiVersion: 'v2'}}); + + expect(parsedDocument).toBeDefined(); + expect(parsedDocument.document.version()).toEqual('2.3.0'); + }); + it('should not be able to parse AsyncAPI v3 document for parser API v1', async () => { + const parsedDocument = await parse(dummyV3Document, {}, {templateConfig: {apiVersion: 'v1'}}); + + expect(parsedDocument).toBeDefined(); + expect(parsedDocument.document).not.toBeDefined(); + }); + it('should not be able to parse AsyncAPI v3 document for old parser', async () => { + const parsedDocument = await parse(dummyV3Document, {}, {templateConfig: {}}); + + expect(parsedDocument).toBeDefined(); + expect(parsedDocument.document).not.toBeDefined(); + }); + it('should be able to parse AsyncAPI v3 document for parser API v2', async () => { + const parsedDocument = await parse(dummyV3Document, {}, {templateConfig: {apiVersion: 'v2'}}); + + expect(parsedDocument).toBeDefined(); + expect(parsedDocument.document.version()).toEqual('3.0.0'); + }); + }); +});