Skip to content

Commit

Permalink
Merge pull request #316 from daniloab/fix/channels-path
Browse files Browse the repository at this point in the history
fix(channels): channels path for asyncapi
  • Loading branch information
daniloab authored Apr 18, 2022
2 parents 7141ec4 + dfca1e1 commit 4b50b92
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ yarn add swagger-jsdoc

- OpenAPI 3.x
- Swagger 2
- AsyncAPI 2.0

## Documentation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
7 changes: 3 additions & 4 deletions examples/eventDriven/src/customSpec.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
5 changes: 2 additions & 3 deletions examples/eventDriven/src/customSpec.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
27 changes: 18 additions & 9 deletions src/specification.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const {
* @returns {object} swaggerObject
*/
function prepare(definition) {
let version;
const swaggerObject = JSON.parse(JSON.stringify(definition));
const specificationTemplate = {
v2: [
Expand All @@ -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] || {};
Expand Down
4 changes: 2 additions & 2 deletions test/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ 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();
});

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();
Expand Down
1 change: 0 additions & 1 deletion test/files/v3/callback/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,5 @@
}
},
"components": {},
"channels": {},
"tags": []
}
1 change: 0 additions & 1 deletion test/files/v3/links/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,5 @@
}
}
},
"channels": {},
"tags": []
}
1 change: 0 additions & 1 deletion test/files/v3/petstore/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,5 @@
}
}
},
"channels": {},
"tags": []
}
1 change: 0 additions & 1 deletion test/lib.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ describe('Main lib module', () => {
},
paths: {},
components: {},
channels: {},
tags: [],
});
});
Expand Down

0 comments on commit 4b50b92

Please sign in to comment.