Skip to content

Commit

Permalink
update implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Sep 11, 2023
1 parent f4835fb commit 2f9b418
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
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
17 changes: 11 additions & 6 deletions lib/parser.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
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;

/**
* 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);
};

/**
* 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);
Expand Down
47 changes: 36 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 31 additions & 0 deletions test/docs/dummyV3.yml
Original file line number Diff line number Diff line change
@@ -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
80 changes: 80 additions & 0 deletions test/parser.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});

0 comments on commit 2f9b418

Please sign in to comment.