Skip to content

Commit bd33ad1

Browse files
committed
Solving comments
The convertion methods has been added fot the version 2.0 it was added a test to validate the convertSwaggerToOpenapi method. convertSwaggerToOpenapi throws an error when it fails
1 parent afbdbcb commit bd33ad1

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

lib/options.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ module.exports = {
149149
description: 'Whether or not schemas should be faked.',
150150
external: false,
151151
usage: ['CONVERSION'],
152-
supportedIn: [VERSION30, VERSION31]
152+
supportedIn: [VERSION20, VERSION30, VERSION31]
153153
},
154154
{
155155
name: 'Schema resolution nesting limit',
@@ -161,7 +161,7 @@ module.exports = {
161161
' option works correctly "optimizeConversion" option needs to be disabled)',
162162
external: false,
163163
usage: ['CONVERSION'],
164-
supportedIn: [VERSION30, VERSION31]
164+
supportedIn: [VERSION20, VERSION30, VERSION31]
165165
},
166166
{
167167
name: 'Include auth info in example requests',
@@ -277,7 +277,7 @@ module.exports = {
277277
description: 'Whether to set optional parameters as disabled',
278278
external: true,
279279
usage: ['CONVERSION'],
280-
supportedIn: [VERSION30, VERSION31]
280+
supportedIn: [VERSION20, VERSION30, VERSION31]
281281
},
282282
{
283283
name: 'Keep implicit headers',
@@ -287,7 +287,7 @@ module.exports = {
287287
description: 'Whether to keep implicit headers from the OpenAPI specification, which are removed by default.',
288288
external: true,
289289
usage: ['CONVERSION'],
290-
supportedIn: [VERSION30, VERSION31]
290+
supportedIn: [VERSION20, VERSION30, VERSION31]
291291
},
292292
{
293293
name: 'Include webhooks',

lib/swaggerUtils/swaggerToOpenapi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
);
1515
}
1616
catch (error) {
17-
return error;
17+
throw error;
1818
}
1919

2020
}

test/unit/swaggerToOpenapi.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { convertSwaggerToOpenapi } = require('../../lib/swaggerUtils/swaggerToOpenapi'),
2+
fs = require('fs'),
3+
path = require('path'),
4+
SWAGGER_20_FOLDER_JSON = '../data/valid_swagger/json/',
5+
SWAGGER_20_INVALID_FOLDER_JSON = '../data/invalid_swagger/',
6+
utils = require('../../lib/swaggerUtils/schemaUtilsSwagger'),
7+
expect = require('chai').expect;
8+
9+
describe('Test swaggerToOpenapi method', async function() {
10+
it('Should convert a swagger file to an openapi', async function() {
11+
const fileSource = path.join(__dirname, SWAGGER_20_FOLDER_JSON + '/sampleswagger.json'),
12+
fileData = fs.readFileSync(fileSource, 'utf8'),
13+
parsedSpec = utils.parseSpec(fileData);
14+
let result = await convertSwaggerToOpenapi(parsedSpec.openapi);
15+
expect(result.openapi.openapi).to.be.equal('3.0.0');
16+
});
17+
18+
it('Should throw an error when swagger file is not complete', async function() {
19+
const fileSource = path.join(__dirname, SWAGGER_20_INVALID_FOLDER_JSON + '/invalid_no_info.json'),
20+
fileData = fs.readFileSync(fileSource, 'utf8'),
21+
parsedSpec = utils.parseSpec(fileData);
22+
try {
23+
await convertSwaggerToOpenapi(parsedSpec.openapi);
24+
expect.fail();
25+
}
26+
catch (error) {
27+
expect(error.message).to.be.equal('Unsupported swagger/OpenAPI version: undefined');
28+
}
29+
});
30+
});

0 commit comments

Comments
 (0)