From 19ac3862c49fe82998dab79a5df8ec45536afb16 Mon Sep 17 00:00:00 2001 From: Danilo Date: Mon, 18 Apr 2022 10:41:56 -0300 Subject: [PATCH 1/2] fix(channels): channels path for asyncapi --- README.md | 1 + .../{openapiConfig.js => asyncApiConfig.js} | 4 +-- examples/eventDriven/src/customSpec.json | 7 +++-- examples/eventDriven/src/customSpec.yml | 5 ++-- src/specification.js | 26 ++++++++++++++----- test/cli.spec.js | 4 +-- test/files/v3/callback/openapi.json | 1 - test/files/v3/links/openapi.json | 1 - test/files/v3/petstore/openapi.json | 1 - test/lib.spec.js | 1 - 10 files changed, 29 insertions(+), 22 deletions(-) rename examples/eventDriven/{openapiConfig.js => asyncApiConfig.js} (55%) diff --git a/README.md b/README.md index ec9fe332..5ca5398c 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ yarn add swagger-jsdoc - OpenAPI 3.x - Swagger 2 +- AsyncAPI 2.0 ## Documentation diff --git a/examples/eventDriven/openapiConfig.js b/examples/eventDriven/asyncApiConfig.js similarity index 55% rename from examples/eventDriven/openapiConfig.js rename to examples/eventDriven/asyncApiConfig.js index 60b84905..11a8e353 100644 --- a/examples/eventDriven/openapiConfig.js +++ b/examples/eventDriven/asyncApiConfig.js @@ -2,7 +2,7 @@ module.exports = { info: { title: 'User Event API', version: '1.0.0', + description: ' User Event API Specification', }, - description: ' User Event API Specification', - openapi: '3.0.0', + asyncapi: '2.0.0', }; diff --git a/examples/eventDriven/src/customSpec.json b/examples/eventDriven/src/customSpec.json index a2555db7..ebc25acc 100644 --- a/examples/eventDriven/src/customSpec.json +++ b/examples/eventDriven/src/customSpec.json @@ -1,11 +1,10 @@ { "info": { "title": "User Event API", - "version": "1.0.0" + "version": "1.0.0", + "description": " User Event API Specification" }, - "description": " User Event API Specification", - "openapi": "3.0.0", - "paths": {}, + "asyncapi": "2.0.0", "components": { "messages": { "UserSignedIn": { diff --git a/examples/eventDriven/src/customSpec.yml b/examples/eventDriven/src/customSpec.yml index 02ff90eb..ba670f52 100644 --- a/examples/eventDriven/src/customSpec.yml +++ b/examples/eventDriven/src/customSpec.yml @@ -1,9 +1,8 @@ info: title: User Event API version: 1.0.0 -description: " User Event API Specification" -openapi: 3.0.0 -paths: {} + description: " User Event API Specification" +asyncapi: 2.0.0 components: messages: UserSignedIn: diff --git a/src/specification.js b/src/specification.js index 85856ee0..7708cb1a 100644 --- a/src/specification.js +++ b/src/specification.js @@ -18,7 +18,6 @@ const { * @returns {object} swaggerObject */ function prepare(definition) { - let version; const swaggerObject = JSON.parse(JSON.stringify(definition)); const specificationTemplate = { v2: [ @@ -35,19 +34,32 @@ function prepare(definition) { 'parameters', 'securityDefinitions', 'components', + ], + v4: [ + 'components', 'channels', ], }; - if (swaggerObject.openapi) { - version = 'v3'; - } else if (swaggerObject.swagger) { - version = 'v2'; - } else { - version = 'v2'; + const getVersion = () => { + if(swaggerObject.asyncapi) { + return 'v4'; + } + + if(swaggerObject.openapi) { + return 'v3'; + } + + if(swaggerObject.swagger) { + return 'v2'; + } + swaggerObject.swagger = '2.0'; + return 'v2'; } + const version = getVersion(); + specificationTemplate[version].forEach((property) => { swaggerObject[property] = swaggerObject[property] || {}; }); diff --git a/test/cli.spec.js b/test/cli.spec.js index e16b810b..33ab4c5d 100644 --- a/test/cli.spec.js +++ b/test/cli.spec.js @@ -123,7 +123,7 @@ describe('CLI module', () => { it('should generate json final file from separated files', async () => { const result = await sh( - `${bin} -d examples/eventDriven/openapiConfig.js examples/eventDriven/src/modules/**/*.yml -o examples/eventDriven/src/customSpec.json` + `${bin} -d examples/eventDriven/asyncApiConfig.js examples/eventDriven/src/modules/**/*.yml -o examples/eventDriven/src/customSpec.json` ); expect(result.stdout).toMatchSnapshot(); @@ -131,7 +131,7 @@ describe('CLI module', () => { it('should generate yml final file from separated files', async () => { const result = await sh( - `${bin} -d examples/eventDriven/openapiConfig.js examples/eventDriven/src/modules/**/*.yml -o examples/eventDriven/src/customSpec.yml` + `${bin} -d examples/eventDriven/asyncApiConfig.js examples/eventDriven/src/modules/**/*.yml -o examples/eventDriven/src/customSpec.yml` ); expect(result.stdout).toMatchSnapshot(); diff --git a/test/files/v3/callback/openapi.json b/test/files/v3/callback/openapi.json index 300d2bb2..07a6f42d 100644 --- a/test/files/v3/callback/openapi.json +++ b/test/files/v3/callback/openapi.json @@ -79,6 +79,5 @@ } }, "components": {}, - "channels": {}, "tags": [] } diff --git a/test/files/v3/links/openapi.json b/test/files/v3/links/openapi.json index ec091365..5a67d42e 100644 --- a/test/files/v3/links/openapi.json +++ b/test/files/v3/links/openapi.json @@ -61,6 +61,5 @@ } } }, - "channels": {}, "tags": [] } diff --git a/test/files/v3/petstore/openapi.json b/test/files/v3/petstore/openapi.json index 06aeec58..6d359d07 100644 --- a/test/files/v3/petstore/openapi.json +++ b/test/files/v3/petstore/openapi.json @@ -152,6 +152,5 @@ } } }, - "channels": {}, "tags": [] } diff --git a/test/lib.spec.js b/test/lib.spec.js index a23320c6..3f65f08b 100644 --- a/test/lib.spec.js +++ b/test/lib.spec.js @@ -111,7 +111,6 @@ describe('Main lib module', () => { }, paths: {}, components: {}, - channels: {}, tags: [], }); }); From dfca1e15b15553695a5d8303c9812a94d91c94b2 Mon Sep 17 00:00:00 2001 From: Danilo Date: Mon, 18 Apr 2022 10:46:05 -0300 Subject: [PATCH 2/2] chore(eslint): eslint --- src/specification.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/specification.js b/src/specification.js index 7708cb1a..bf0dedb1 100644 --- a/src/specification.js +++ b/src/specification.js @@ -35,28 +35,25 @@ function prepare(definition) { 'securityDefinitions', 'components', ], - v4: [ - 'components', - 'channels', - ], + v4: ['components', 'channels'], }; const getVersion = () => { - if(swaggerObject.asyncapi) { + if (swaggerObject.asyncapi) { return 'v4'; } - if(swaggerObject.openapi) { + if (swaggerObject.openapi) { return 'v3'; } - if(swaggerObject.swagger) { + if (swaggerObject.swagger) { return 'v2'; } swaggerObject.swagger = '2.0'; return 'v2'; - } + }; const version = getVersion();