diff --git a/lib/parser.js b/lib/parser.js index 7465352c4..329d1244a 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -8,17 +8,18 @@ const parser = module.exports; * Convert the template defined value `apiVersion: 'v1'` to only contain the numeric value `1`. */ parser.sanitizeTemplateApiVersion = (apiVersion) => { + if (!apiVersion) return; if (apiVersion && apiVersion.length > 1) { - return apiVersion.substring(1); + return Number(apiVersion.substring(1)); } - return apiVersion; + return Number(apiVersion); }; parser.parse = async (asyncapi, oldOptions, generator) => { let apiVersion = this.sanitizeTemplateApiVersion(generator.templateConfig.apiVersion); // Defaulting to apiVersion v1 to convert it to the Parser-API v1 afterwards. if (!this.usesNewAPI(generator.templateConfig)) { - apiVersion = '1'; + apiVersion = 1; } const options = convertOldOptionsToNew(oldOptions, generator); const parser = NewParser(apiVersion, {parserOptions: options, includeSchemaParsers: true}); @@ -34,7 +35,7 @@ parser.parse = async (asyncapi, oldOptions, generator) => { * If the template expect one of the Parser-API versions, it must be above 0 */ parser.usesNewAPI = (templateConfig = {}) => { - return Number(this.sanitizeTemplateApiVersion(templateConfig.apiVersion)) > 0; + return this.sanitizeTemplateApiVersion(templateConfig.apiVersion) > 0; }; /** diff --git a/test/parser.test.js b/test/parser.test.js index e496a28e9..da7d76c38 100644 --- a/test/parser.test.js +++ b/test/parser.test.js @@ -8,21 +8,21 @@ describe('Parser', () => { describe('sanitizeTemplateApiVersion', () => { it('should return version number when given `v2` syntax', () => { const rawVersion = 'v2'; - const expectedVersion = '2'; + const expectedVersion = 2; const sanitizedVersion = sanitizeTemplateApiVersion(rawVersion); expect(sanitizedVersion).toStrictEqual(expectedVersion); }); it('should return version number when given `v1` syntax', () => { const rawVersion = 'v1'; - const expectedVersion = '1'; + 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 expectedVersion = 1; const sanitizedVersion = sanitizeTemplateApiVersion(rawVersion); expect(sanitizedVersion).toStrictEqual(expectedVersion);