From 03401a126cd280c798a5c910e9c3583eb19726bc Mon Sep 17 00:00:00 2001 From: "jonas-lt@live.dk" Date: Thu, 28 Sep 2023 11:47:28 +0200 Subject: [PATCH] fix remaining errors --- lib/parser.js | 2 +- lib/templateConfigValidator.js | 2 +- test/templateConfigValidator.test.js | 53 +++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/lib/parser.js b/lib/parser.js index 26a71e52f..7465352c4 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -26,7 +26,7 @@ parser.parse = async (asyncapi, oldOptions, generator) => { if (!document) { return {document, diagnostics}; } - const correctDocument = this.getProperApiDocument(document, this.templateConfig); + const correctDocument = this.getProperApiDocument(document, generator.templateConfig); return {document: correctDocument, diagnostics}; }; diff --git a/lib/templateConfigValidator.js b/lib/templateConfigValidator.js index 01c98daf7..b6be15864 100644 --- a/lib/templateConfigValidator.js +++ b/lib/templateConfigValidator.js @@ -29,7 +29,7 @@ module.exports.validateTemplateConfig = (templateConfig, templateParams, asyncap isTemplateCompatible(generator, apiVersion); isRequiredParamProvided(parameters, templateParams); isProvidedTemplateRendererSupported(templateConfig); - if (asyncapiDocument) { + if (asyncapiDocument && templateParams.server) { let server; if (usesNewAPI(templateConfig)) { server = asyncapiDocument.servers().get(templateParams.server); diff --git a/test/templateConfigValidator.test.js b/test/templateConfigValidator.test.js index c090aa765..005f3abbf 100644 --- a/test/templateConfigValidator.test.js +++ b/test/templateConfigValidator.test.js @@ -1,3 +1,4 @@ +/* eslint-disable sonarjs/no-duplicate-string */ const { validateTemplateConfig } = require('../lib/templateConfigValidator'); const fs = require('fs'); const path = require('path'); @@ -10,7 +11,7 @@ describe('Template Configuration Validator', () => { let asyncapiDocument; beforeAll(async () => { - const { document } = await parse(dummyYAML, {}, {templateConfig: {apiVersion: 'v2'}}); + const { document } = await parse(dummyYAML, {}, {templateConfig: {}}); asyncapiDocument = document; }); @@ -189,20 +190,62 @@ describe('Template Configuration Validator', () => { expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Server "dummy-mqtt" uses the mqtt protocol but this template only supports the following ones: myprotocol.'); }); - describe('should work with v2 apiVersion', () => { + describe('should work with v1 apiVersion', () => { let asyncapiDocument; + const v2TemplateConfig = {apiVersion: 'v1'}; + beforeAll(async () => { + const { document } = await parse(dummyYAML, {}, {templateConfig: v2TemplateConfig}); + asyncapiDocument = document; + }); + + it('Validation throw error if specified server is not in asyncapi document', () => { + const templateParams = { + server: 'myserver' + }; + const templateConfig = { + ...v2TemplateConfig, + parameters: { + server: { + description: '' + } + } + }; + + expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Couldn\'t find server with name myserver.'); + }); + it('Validation throw error if given protocol is not supported by template', () => { + const templateParams = { + server: 'dummy-mqtt' + }; + const templateConfig = { + ...v2TemplateConfig, + supportedProtocols: ['myprotocol'], + parameters: { + server: { + description: '' + } + } + }; + + expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Server "dummy-mqtt" uses the mqtt protocol but this template only supports the following ones: myprotocol.'); + }); + }); + + describe('should work with v2 apiVersion', () => { + let asyncapiDocument; + const v2TemplateConfig = {apiVersion: 'v2'}; beforeAll(async () => { - const { document } = await parse(dummyYAML, {}, {templateConfig: {apiVersion: 'v2'}}); + const { document } = await parse(dummyYAML, {}, {templateConfig: v2TemplateConfig}); asyncapiDocument = document; }); - // eslint-disable-next-line sonarjs/no-identical-functions it('Validation throw error if specified server is not in asyncapi document', () => { const templateParams = { server: 'myserver' }; const templateConfig = { + ...v2TemplateConfig, parameters: { server: { description: '' @@ -213,12 +256,12 @@ describe('Template Configuration Validator', () => { expect(() => validateTemplateConfig(templateConfig, templateParams, asyncapiDocument)).toThrow('Couldn\'t find server with name myserver.'); }); - // eslint-disable-next-line sonarjs/no-identical-functions it('Validation throw error if given protocol is not supported by template', () => { const templateParams = { server: 'dummy-mqtt' }; const templateConfig = { + ...v2TemplateConfig, supportedProtocols: ['myprotocol'], parameters: { server: {