|
3 | 3 | // This is the default collection name if one can't be inferred from the OpenAPI spec
|
4 | 4 | const COLLECTION_NAME = 'Imported from OpenAPI 3.0',
|
5 | 5 | { getConcreteSchemaUtils } = require('./common/versionUtils.js'),
|
| 6 | + { convertToOAS30IfSwagger } = require('./swaggerUtils/swaggerToOpenapi.js'), |
6 | 7 | BROWSER = 'browser',
|
7 | 8 | Ajv = require('ajv'),
|
8 | 9 | addFormats = require('ajv-formats'),
|
@@ -60,7 +61,10 @@ class SchemaPack {
|
60 | 61 | validate() {
|
61 | 62 | let input = this.input,
|
62 | 63 | json,
|
63 |
| - specParseResult; |
| 64 | + specParseResult, |
| 65 | + isFolder = this.input.type === 'folder'; |
| 66 | + |
| 67 | + this.computedOptions = Object.assign({ isFolder }, this.computedOptions); |
64 | 68 | if (!input) {
|
65 | 69 | return {
|
66 | 70 | result: false,
|
@@ -252,113 +256,121 @@ class SchemaPack {
|
252 | 256 | return callback(new OpenApiErr('The schema must be validated before attempting conversion'));
|
253 | 257 | }
|
254 | 258 |
|
255 |
| - // this cannot be attempted before validation |
256 |
| - specComponentsAndUtils = { concreteUtils }; |
257 |
| - Object.assign(specComponentsAndUtils, concreteUtils.getRequiredData(this.openapi)); |
| 259 | + // We only convert if swagger is found otherwise this.openapi remains the same |
| 260 | + convertToOAS30IfSwagger(concreteUtils, this.openapi, (error, newOpenapi) => { |
| 261 | + if (error) { |
| 262 | + return callback(error); |
| 263 | + } |
258 | 264 |
|
259 |
| - // create and sanitize basic spec |
260 |
| - openapi = this.openapi; |
261 |
| - openapi.servers = _.isEmpty(openapi.servers) ? [{ url: '/' }] : openapi.servers; |
262 |
| - openapi.securityDefs = _.get(openapi, 'components.securitySchemes', {}); |
263 |
| - openapi.baseUrl = _.get(openapi, 'servers.0.url', '{{baseURL}}'); |
| 265 | + this.openapi = newOpenapi; |
| 266 | + // this cannot be attempted before validation |
| 267 | + specComponentsAndUtils = { concreteUtils }; |
| 268 | + Object.assign(specComponentsAndUtils, concreteUtils.getRequiredData(this.openapi)); |
264 | 269 |
|
265 |
| - // TODO: Multiple server variables need to be saved as environments |
266 |
| - openapi.baseUrlVariables = _.get(openapi, 'servers.0.variables'); |
| 270 | + // create and sanitize basic spec |
| 271 | + openapi = this.openapi; |
| 272 | + openapi.servers = _.isEmpty(openapi.servers) ? [{ url: '/' }] : openapi.servers; |
| 273 | + openapi.securityDefs = _.get(openapi, 'components.securitySchemes', {}); |
| 274 | + openapi.baseUrl = _.get(openapi, 'servers.0.url', '{{baseURL}}'); |
267 | 275 |
|
268 |
| - // Fix {scheme} and {path} vars in the URL to :scheme and :path |
269 |
| - openapi.baseUrl = schemaUtils.fixPathVariablesInUrl(openapi.baseUrl); |
| 276 | + // TODO: Multiple server variables need to be saved as environments |
| 277 | + openapi.baseUrlVariables = _.get(openapi, 'servers.0.variables'); |
270 | 278 |
|
271 |
| - // Creating a new instance of a Postman collection |
272 |
| - // All generated folders and requests will go inside this |
273 |
| - generatedStore.collection = new sdk.Collection({ |
274 |
| - info: { |
275 |
| - name: _.isEmpty(_.get(openapi, 'info.title')) ? COLLECTION_NAME : _.get(openapi, 'info.title') |
276 |
| - } |
277 |
| - }); |
| 279 | + // Fix {scheme} and {path} vars in the URL to :scheme and :path |
| 280 | + openapi.baseUrl = schemaUtils.fixPathVariablesInUrl(openapi.baseUrl); |
278 | 281 |
|
279 |
| - if (openapi.security) { |
280 |
| - authHelper = schemaUtils.getAuthHelper(openapi, openapi.security); |
281 |
| - if (authHelper) { |
282 |
| - generatedStore.collection.auth = authHelper; |
| 282 | + // Creating a new instance of a Postman collection |
| 283 | + // All generated folders and requests will go inside this |
| 284 | + generatedStore.collection = new sdk.Collection({ |
| 285 | + info: { |
| 286 | + name: _.isEmpty(_.get(openapi, 'info.title')) ? COLLECTION_NAME : _.get(openapi, 'info.title') |
| 287 | + } |
| 288 | + }); |
| 289 | + |
| 290 | + if (openapi.security) { |
| 291 | + authHelper = schemaUtils.getAuthHelper(openapi, openapi.security); |
| 292 | + if (authHelper) { |
| 293 | + generatedStore.collection.auth = authHelper; |
| 294 | + } |
283 | 295 | }
|
284 |
| - } |
285 |
| - // ---- Collection Variables ---- |
286 |
| - // adding the collection variables for all the necessary root level variables |
287 |
| - // and adding them to the collection variables |
288 |
| - schemaUtils.convertToPmCollectionVariables( |
289 |
| - openapi.baseUrlVariables, |
290 |
| - 'baseUrl', |
291 |
| - openapi.baseUrl |
292 |
| - ).forEach((element) => { |
293 |
| - generatedStore.collection.variables.add(element); |
294 |
| - }); |
| 296 | + // ---- Collection Variables ---- |
| 297 | + // adding the collection variables for all the necessary root level variables |
| 298 | + // and adding them to the collection variables |
| 299 | + schemaUtils.convertToPmCollectionVariables( |
| 300 | + openapi.baseUrlVariables, |
| 301 | + 'baseUrl', |
| 302 | + openapi.baseUrl |
| 303 | + ).forEach((element) => { |
| 304 | + generatedStore.collection.variables.add(element); |
| 305 | + }); |
295 | 306 |
|
296 |
| - generatedStore.collection.describe(schemaUtils.getCollectionDescription(openapi)); |
| 307 | + generatedStore.collection.describe(schemaUtils.getCollectionDescription(openapi)); |
297 | 308 |
|
298 |
| - // Only change the stack limit if the optimizeConversion option is true |
299 |
| - if (options.optimizeConversion) { |
300 |
| - // Deciding stack limit based on size of the schema, number of refs and number of paths. |
301 |
| - analysis = schemaUtils.analyzeSpec(openapi); |
| 309 | + // Only change the stack limit if the optimizeConversion option is true |
| 310 | + if (options.optimizeConversion) { |
| 311 | + // Deciding stack limit based on size of the schema, number of refs and number of paths. |
| 312 | + analysis = schemaUtils.analyzeSpec(openapi); |
302 | 313 |
|
303 |
| - // Update options on the basis of analysis. |
304 |
| - options = schemaUtils.determineOptions(analysis, options); |
305 |
| - } |
| 314 | + // Update options on the basis of analysis. |
| 315 | + options = schemaUtils.determineOptions(analysis, options); |
| 316 | + } |
306 | 317 |
|
307 | 318 |
|
308 |
| - // ---- Collection Items ---- |
309 |
| - // Adding the collection items from openapi spec based on folderStrategy option |
310 |
| - // For tags, All operations are grouped based on respective tags object |
311 |
| - // For paths, All operations are grouped based on corresponding paths |
312 |
| - try { |
313 |
| - if (options.folderStrategy === 'tags') { |
314 |
| - schemaUtils.addCollectionItemsUsingTags( |
315 |
| - openapi, |
316 |
| - generatedStore, |
317 |
| - specComponentsAndUtils, |
318 |
| - options, |
319 |
| - schemaCache, |
320 |
| - concreteUtils |
321 |
| - ); |
322 |
| - } |
323 |
| - else { |
324 |
| - schemaUtils.addCollectionItemsUsingPaths( |
325 |
| - openapi, |
326 |
| - generatedStore, |
327 |
| - specComponentsAndUtils, |
328 |
| - options, |
329 |
| - schemaCache, |
330 |
| - concreteUtils |
331 |
| - ); |
332 |
| - } |
| 319 | + // ---- Collection Items ---- |
| 320 | + // Adding the collection items from openapi spec based on folderStrategy option |
| 321 | + // For tags, All operations are grouped based on respective tags object |
| 322 | + // For paths, All operations are grouped based on corresponding paths |
| 323 | + try { |
| 324 | + if (options.folderStrategy === 'tags') { |
| 325 | + schemaUtils.addCollectionItemsUsingTags( |
| 326 | + openapi, |
| 327 | + generatedStore, |
| 328 | + specComponentsAndUtils, |
| 329 | + options, |
| 330 | + schemaCache, |
| 331 | + concreteUtils |
| 332 | + ); |
| 333 | + } |
| 334 | + else { |
| 335 | + schemaUtils.addCollectionItemsUsingPaths( |
| 336 | + openapi, |
| 337 | + generatedStore, |
| 338 | + specComponentsAndUtils, |
| 339 | + options, |
| 340 | + schemaCache, |
| 341 | + concreteUtils |
| 342 | + ); |
| 343 | + } |
333 | 344 |
|
334 |
| - if (options.includeWebhooks) { |
335 |
| - schemaUtils.addCollectionItemsFromWebhooks( |
336 |
| - openapi, |
337 |
| - generatedStore, |
338 |
| - specComponentsAndUtils, |
339 |
| - options, |
340 |
| - schemaCache, |
341 |
| - concreteUtils |
342 |
| - ); |
| 345 | + if (options.includeWebhooks) { |
| 346 | + schemaUtils.addCollectionItemsFromWebhooks( |
| 347 | + openapi, |
| 348 | + generatedStore, |
| 349 | + specComponentsAndUtils, |
| 350 | + options, |
| 351 | + schemaCache, |
| 352 | + concreteUtils |
| 353 | + ); |
| 354 | + } |
| 355 | + } |
| 356 | + catch (e) { |
| 357 | + return callback(e); |
343 | 358 | }
|
344 |
| - } |
345 |
| - catch (e) { |
346 |
| - return callback(e); |
347 |
| - } |
348 | 359 |
|
349 |
| - collectionJSON = generatedStore.collection.toJSON(); |
| 360 | + collectionJSON = generatedStore.collection.toJSON(); |
350 | 361 |
|
351 |
| - // this needs to be deleted as even if version is not specified to sdk, |
352 |
| - // it returns a version property with value set as undefined |
353 |
| - // this fails validation against v2.1 collection schema definition. |
354 |
| - delete collectionJSON.info.version; |
| 362 | + // this needs to be deleted as even if version is not specified to sdk, |
| 363 | + // it returns a version property with value set as undefined |
| 364 | + // this fails validation against v2.1 collection schema definition. |
| 365 | + delete collectionJSON.info.version; |
355 | 366 |
|
356 |
| - return callback(null, { |
357 |
| - result: true, |
358 |
| - output: [{ |
359 |
| - type: 'collection', |
360 |
| - data: collectionJSON |
361 |
| - }] |
| 367 | + return callback(null, { |
| 368 | + result: true, |
| 369 | + output: [{ |
| 370 | + type: 'collection', |
| 371 | + data: collectionJSON |
| 372 | + }] |
| 373 | + }); |
362 | 374 | });
|
363 | 375 | }
|
364 | 376 |
|
|
0 commit comments