1
1
'use strict' ;
2
2
3
- const { convertSwaggerToOpenapi } = require ( './swaggerUtils/swaggerToOpenapi.js' ) ;
4
-
5
3
// This is the default collection name if one can't be inferred from the OpenAPI spec
6
4
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' ) ,
8
7
BROWSER = 'browser' ,
9
8
Ajv = require ( 'ajv' ) ,
10
9
addFormats = require ( 'ajv-formats' ) ,
@@ -239,7 +238,7 @@ class SchemaPack {
239
238
240
239
// convert method, this is called when you want to convert a schema that you've already loaded
241
240
// in the constructor
242
- async convert ( callback ) {
241
+ convert ( callback ) {
243
242
let openapi ,
244
243
options = this . computedOptions ,
245
244
analysis ,
@@ -256,122 +255,119 @@ class SchemaPack {
256
255
return callback ( new OpenApiErr ( 'The schema must be validated before attempting conversion' ) ) ;
257
256
}
258
257
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 ) {
265
260
return callback ( error ) ;
266
261
}
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
+ } ) ;
283
286
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
+ }
289
292
}
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
+ } ) ;
291
303
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 ) ) ;
308
305
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 ) ;
310
310
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
+ }
315
314
316
- // Update options on the basis of analysis.
317
- options = schemaUtils . determineOptions ( analysis , options ) ;
318
- }
319
315
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
+ }
320
341
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
+ }
345
352
}
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 ) ;
356
355
}
357
- }
358
- catch ( e ) {
359
- return callback ( e ) ;
360
- }
361
356
362
- collectionJSON = generatedStore . collection . toJSON ( ) ;
357
+ collectionJSON = generatedStore . collection . toJSON ( ) ;
363
358
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 ;
368
363
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
+ } ) ;
375
371
} ) ;
376
372
}
377
373
0 commit comments