Skip to content

Commit fa623d3

Browse files
committed
Fix swagger to openapi convertion
Removing async await implementation and using callback implementation instead. Fixin the related tests
1 parent bd33ad1 commit fa623d3

File tree

3 files changed

+124
-123
lines changed

3 files changed

+124
-123
lines changed

lib/schemapack.js

Lines changed: 101 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict';
22

3-
const { convertSwaggerToOpenapi } = require('./swaggerUtils/swaggerToOpenapi.js');
4-
53
// This is the default collection name if one can't be inferred from the OpenAPI spec
64
const COLLECTION_NAME = 'Imported from OpenAPI 3.0',
7-
{ getConcreteSchemaUtils, isSwagger } = require('./common/versionUtils.js'),
5+
{ getConcreteSchemaUtils } = require('./common/versionUtils.js'),
6+
{ convertSwaggerToOpenapi } = require('./swaggerUtils/swaggerToOpenapi.js'),
87
BROWSER = 'browser',
98
Ajv = require('ajv'),
109
addFormats = require('ajv-formats'),
@@ -239,7 +238,7 @@ class SchemaPack {
239238

240239
// convert method, this is called when you want to convert a schema that you've already loaded
241240
// in the constructor
242-
async convert (callback) {
241+
convert (callback) {
243242
let openapi,
244243
options = this.computedOptions,
245244
analysis,
@@ -256,122 +255,119 @@ class SchemaPack {
256255
return callback(new OpenApiErr('The schema must be validated before attempting conversion'));
257256
}
258257

259-
if (isSwagger(concreteUtils.version)) {
260-
try {
261-
let result = await convertSwaggerToOpenapi(this.openapi);
262-
this.openapi = result.openapi;
263-
}
264-
catch (error) {
258+
convertSwaggerToOpenapi(concreteUtils, this.openapi, (error, newOpenapi) => {
259+
if (error) {
265260
return callback(error);
266261
}
267-
}
268-
// this cannot be attempted before validation
269-
specComponentsAndUtils = { concreteUtils };
270-
Object.assign(specComponentsAndUtils, concreteUtils.getRequiredData(this.openapi));
271-
272-
// create and sanitize basic spec
273-
openapi = this.openapi;
274-
openapi.servers = _.isEmpty(openapi.servers) ? [{ url: '/' }] : openapi.servers;
275-
openapi.securityDefs = _.get(openapi, 'components.securitySchemes', {});
276-
openapi.baseUrl = _.get(openapi, 'servers.0.url', '{{baseURL}}');
277-
278-
// TODO: Multiple server variables need to be saved as environments
279-
openapi.baseUrlVariables = _.get(openapi, 'servers.0.variables');
280-
281-
// Fix {scheme} and {path} vars in the URL to :scheme and :path
282-
openapi.baseUrl = schemaUtils.fixPathVariablesInUrl(openapi.baseUrl);
262+
this.openapi = newOpenapi;
263+
// this cannot be attempted before validation
264+
specComponentsAndUtils = { concreteUtils };
265+
Object.assign(specComponentsAndUtils, concreteUtils.getRequiredData(this.openapi));
266+
267+
// create and sanitize basic spec
268+
openapi = this.openapi;
269+
openapi.servers = _.isEmpty(openapi.servers) ? [{ url: '/' }] : openapi.servers;
270+
openapi.securityDefs = _.get(openapi, 'components.securitySchemes', {});
271+
openapi.baseUrl = _.get(openapi, 'servers.0.url', '{{baseURL}}');
272+
273+
// TODO: Multiple server variables need to be saved as environments
274+
openapi.baseUrlVariables = _.get(openapi, 'servers.0.variables');
275+
276+
// Fix {scheme} and {path} vars in the URL to :scheme and :path
277+
openapi.baseUrl = schemaUtils.fixPathVariablesInUrl(openapi.baseUrl);
278+
279+
// Creating a new instance of a Postman collection
280+
// All generated folders and requests will go inside this
281+
generatedStore.collection = new sdk.Collection({
282+
info: {
283+
name: _.isEmpty(_.get(openapi, 'info.title')) ? COLLECTION_NAME : _.get(openapi, 'info.title')
284+
}
285+
});
283286

284-
// Creating a new instance of a Postman collection
285-
// All generated folders and requests will go inside this
286-
generatedStore.collection = new sdk.Collection({
287-
info: {
288-
name: _.isEmpty(_.get(openapi, 'info.title')) ? COLLECTION_NAME : _.get(openapi, 'info.title')
287+
if (openapi.security) {
288+
authHelper = schemaUtils.getAuthHelper(openapi, openapi.security);
289+
if (authHelper) {
290+
generatedStore.collection.auth = authHelper;
291+
}
289292
}
290-
});
293+
// ---- Collection Variables ----
294+
// adding the collection variables for all the necessary root level variables
295+
// and adding them to the collection variables
296+
schemaUtils.convertToPmCollectionVariables(
297+
openapi.baseUrlVariables,
298+
'baseUrl',
299+
openapi.baseUrl
300+
).forEach((element) => {
301+
generatedStore.collection.variables.add(element);
302+
});
291303

292-
if (openapi.security) {
293-
authHelper = schemaUtils.getAuthHelper(openapi, openapi.security);
294-
if (authHelper) {
295-
generatedStore.collection.auth = authHelper;
296-
}
297-
}
298-
// ---- Collection Variables ----
299-
// adding the collection variables for all the necessary root level variables
300-
// and adding them to the collection variables
301-
schemaUtils.convertToPmCollectionVariables(
302-
openapi.baseUrlVariables,
303-
'baseUrl',
304-
openapi.baseUrl
305-
).forEach((element) => {
306-
generatedStore.collection.variables.add(element);
307-
});
304+
generatedStore.collection.describe(schemaUtils.getCollectionDescription(openapi));
308305

309-
generatedStore.collection.describe(schemaUtils.getCollectionDescription(openapi));
306+
// Only change the stack limit if the optimizeConversion option is true
307+
if (options.optimizeConversion) {
308+
// Deciding stack limit based on size of the schema, number of refs and number of paths.
309+
analysis = schemaUtils.analyzeSpec(openapi);
310310

311-
// Only change the stack limit if the optimizeConversion option is true
312-
if (options.optimizeConversion) {
313-
// Deciding stack limit based on size of the schema, number of refs and number of paths.
314-
analysis = schemaUtils.analyzeSpec(openapi);
311+
// Update options on the basis of analysis.
312+
options = schemaUtils.determineOptions(analysis, options);
313+
}
315314

316-
// Update options on the basis of analysis.
317-
options = schemaUtils.determineOptions(analysis, options);
318-
}
319315

316+
// ---- Collection Items ----
317+
// Adding the collection items from openapi spec based on folderStrategy option
318+
// For tags, All operations are grouped based on respective tags object
319+
// For paths, All operations are grouped based on corresponding paths
320+
try {
321+
if (options.folderStrategy === 'tags') {
322+
schemaUtils.addCollectionItemsUsingTags(
323+
openapi,
324+
generatedStore,
325+
specComponentsAndUtils,
326+
options,
327+
schemaCache,
328+
concreteUtils
329+
);
330+
}
331+
else {
332+
schemaUtils.addCollectionItemsUsingPaths(
333+
openapi,
334+
generatedStore,
335+
specComponentsAndUtils,
336+
options,
337+
schemaCache,
338+
concreteUtils
339+
);
340+
}
320341

321-
// ---- Collection Items ----
322-
// Adding the collection items from openapi spec based on folderStrategy option
323-
// For tags, All operations are grouped based on respective tags object
324-
// For paths, All operations are grouped based on corresponding paths
325-
try {
326-
if (options.folderStrategy === 'tags') {
327-
schemaUtils.addCollectionItemsUsingTags(
328-
openapi,
329-
generatedStore,
330-
specComponentsAndUtils,
331-
options,
332-
schemaCache,
333-
concreteUtils
334-
);
335-
}
336-
else {
337-
schemaUtils.addCollectionItemsUsingPaths(
338-
openapi,
339-
generatedStore,
340-
specComponentsAndUtils,
341-
options,
342-
schemaCache,
343-
concreteUtils
344-
);
342+
if (options.includeWebhooks) {
343+
schemaUtils.addCollectionItemsFromWebhooks(
344+
openapi,
345+
generatedStore,
346+
specComponentsAndUtils,
347+
options,
348+
schemaCache,
349+
concreteUtils
350+
);
351+
}
345352
}
346-
347-
if (options.includeWebhooks) {
348-
schemaUtils.addCollectionItemsFromWebhooks(
349-
openapi,
350-
generatedStore,
351-
specComponentsAndUtils,
352-
options,
353-
schemaCache,
354-
concreteUtils
355-
);
353+
catch (e) {
354+
return callback(e);
356355
}
357-
}
358-
catch (e) {
359-
return callback(e);
360-
}
361356

362-
collectionJSON = generatedStore.collection.toJSON();
357+
collectionJSON = generatedStore.collection.toJSON();
363358

364-
// this needs to be deleted as even if version is not specified to sdk,
365-
// it returns a version property with value set as undefined
366-
// this fails validation against v2.1 collection schema definition.
367-
delete collectionJSON.info.version;
359+
// this needs to be deleted as even if version is not specified to sdk,
360+
// it returns a version property with value set as undefined
361+
// this fails validation against v2.1 collection schema definition.
362+
delete collectionJSON.info.version;
368363

369-
return callback(null, {
370-
result: true,
371-
output: [{
372-
type: 'collection',
373-
data: collectionJSON
374-
}]
364+
return callback(null, {
365+
result: true,
366+
output: [{
367+
type: 'collection',
368+
data: collectionJSON
369+
}]
370+
});
375371
});
376372
}
377373

lib/swaggerUtils/swaggerToOpenapi.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
const Swagger2OpenAPI = require('swagger2openapi');
1+
const Swagger2OpenAPI = require('swagger2openapi'),
2+
{ isSwagger } = require('../common/versionUtils');
23

34
module.exports = {
4-
convertSwaggerToOpenapi: function(parsedSwagger) {
5-
try {
6-
return Swagger2OpenAPI.convertObj(
5+
convertSwaggerToOpenapi: function(concreteUtils, parsedSwagger, convertExecution) {
6+
if (isSwagger(concreteUtils.version)) {
7+
Swagger2OpenAPI.convertObj(
78
parsedSwagger,
89
{
910
fatal: false,
1011
patch: true,
1112
anchors: true,
1213
warnOnly: true
14+
},
15+
(error, newOpenapi) => {
16+
if (error) {
17+
return convertExecution(error);
18+
}
19+
return convertExecution(null, newOpenapi.openapi);
1320
}
1421
);
1522
}
16-
catch (error) {
17-
throw error;
23+
else {
24+
return convertExecution(null, parsedSwagger);
1825
}
19-
2026
}
2127
};

test/unit/swaggerToOpenapi.test.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,24 @@ const { convertSwaggerToOpenapi } = require('../../lib/swaggerUtils/swaggerToOpe
66
utils = require('../../lib/swaggerUtils/schemaUtilsSwagger'),
77
expect = require('chai').expect;
88

9-
describe('Test swaggerToOpenapi method', async function() {
10-
it('Should convert a swagger file to an openapi', async function() {
9+
describe('Test swaggerToOpenapi method', function() {
10+
it('Should convert a swagger file to an openapi', function() {
1111
const fileSource = path.join(__dirname, SWAGGER_20_FOLDER_JSON + '/sampleswagger.json'),
1212
fileData = fs.readFileSync(fileSource, 'utf8'),
1313
parsedSpec = utils.parseSpec(fileData);
14-
let result = await convertSwaggerToOpenapi(parsedSpec.openapi);
15-
expect(result.openapi.openapi).to.be.equal('3.0.0');
14+
convertSwaggerToOpenapi(utils, parsedSpec.openapi, (error, openapi) => {
15+
expect(error).to.be.null;
16+
expect(openapi.openapi).to.be.equal('3.0.0');
17+
});
1618
});
1719

18-
it('Should throw an error when swagger file is not complete', async function() {
20+
it('Should throw an error when swagger file is not complete', function() {
1921
const fileSource = path.join(__dirname, SWAGGER_20_INVALID_FOLDER_JSON + '/invalid_no_info.json'),
2022
fileData = fs.readFileSync(fileSource, 'utf8'),
2123
parsedSpec = utils.parseSpec(fileData);
22-
try {
23-
await convertSwaggerToOpenapi(parsedSpec.openapi);
24-
expect.fail();
25-
}
26-
catch (error) {
24+
convertSwaggerToOpenapi(utils, parsedSpec.openapi, (error, openapi) => {
2725
expect(error.message).to.be.equal('Unsupported swagger/OpenAPI version: undefined');
28-
}
26+
expect(openapi).to.be.undefined;
27+
});
2928
});
3029
});

0 commit comments

Comments
 (0)