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..bf0dedb1 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,18 +34,28 @@ function prepare(definition) { 'parameters', 'securityDefinitions', 'components', - 'channels', ], + 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: [], }); });