Skip to content

Commit 6df66da

Browse files
authored
Merge pull request #509 from postmanlabs/split/develop/swaggerModuleIntegration
Split/develop/swagger module integration
2 parents 652c28e + 317e120 commit 6df66da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+10716
-108
lines changed

lib/common/versionUtils.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ function getConcreteSchemaUtils({ type, data }) {
123123
if (specVersion === DEFAULT_SPEC_VERSION) {
124124
concreteUtils = require('../30XUtils/schemaUtils30X');
125125
}
126+
else if (specVersion === VERSION_20.version) {
127+
concreteUtils = require('../swaggerUtils/schemaUtilsSwagger');
128+
}
126129
else {
127130
concreteUtils = require('../31XUtils/schemaUtils31X');
128131
}
@@ -142,8 +145,22 @@ function filterOptionsByVersion(options, version) {
142145
return options;
143146
}
144147

148+
/**
149+
* Calculates if thew current input is using swagger 2.0 spec
150+
* @param {string} version The current spec version
151+
* @returns {boolean} True if the current spec is using swagger 2.0 spec
152+
*/
153+
function isSwagger(version) {
154+
let isSwagger = false;
155+
if (version === VERSION_20.version) {
156+
isSwagger = true;
157+
}
158+
return isSwagger;
159+
}
160+
145161
module.exports = {
146162
getSpecVersion,
147163
getConcreteSchemaUtils,
148-
filterOptionsByVersion
164+
filterOptionsByVersion,
165+
isSwagger
149166
};

lib/options.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const _ = require('lodash'),
44
VALID_MODES = ['document', 'use'],
55
VERSION30 = '3.0',
66
VERSION31 = '3.1',
7-
SUPPORTED_VERSIONS = [VERSION30, VERSION31];
7+
VERSION20 = '2.0',
8+
SUPPORTED_VERSIONS = [VERSION20, VERSION30, VERSION31];
89

910
/**
1011
* Takes a list of arguments and resolve them acording its content
@@ -66,7 +67,7 @@ module.exports = {
6667
' values: `description`, `operationid`, `url`.',
6768
external: true,
6869
usage: ['CONVERSION', 'VALIDATION'],
69-
supportedIn: [VERSION30, VERSION31]
70+
supportedIn: [VERSION20, VERSION30, VERSION31]
7071
},
7172
{
7273
name: 'Set indent character',
@@ -77,7 +78,7 @@ module.exports = {
7778
description: 'Option for setting indentation character',
7879
external: true,
7980
usage: ['CONVERSION'],
80-
supportedIn: [VERSION30, VERSION31]
81+
supportedIn: [VERSION20, VERSION30, VERSION31]
8182
},
8283
{
8384
name: 'Collapse redundant folders',
@@ -88,7 +89,7 @@ module.exports = {
8889
'persistent folder-level data.',
8990
external: true,
9091
usage: ['CONVERSION'],
91-
supportedIn: [VERSION30, VERSION31]
92+
supportedIn: [VERSION20, VERSION30, VERSION31]
9293
},
9394
{
9495
name: 'Optimize conversion',
@@ -99,7 +100,7 @@ module.exports = {
99100
' the performance of conversion.',
100101
external: true,
101102
usage: ['CONVERSION'],
102-
supportedIn: [VERSION30, VERSION31]
103+
supportedIn: [VERSION20, VERSION30, VERSION31]
103104
},
104105
{
105106
name: 'Request parameter generation',
@@ -113,7 +114,7 @@ module.exports = {
113114
' in the schema.',
114115
external: true,
115116
usage: ['CONVERSION'],
116-
supportedIn: [VERSION30, VERSION31]
117+
supportedIn: [VERSION20, VERSION30, VERSION31]
117118
},
118119
{
119120
name: 'Response parameter generation',
@@ -127,7 +128,7 @@ module.exports = {
127128
' in the schema.',
128129
external: true,
129130
usage: ['CONVERSION'],
130-
supportedIn: [VERSION30, VERSION31]
131+
supportedIn: [VERSION20, VERSION30, VERSION31]
131132
},
132133
{
133134
name: 'Folder organization',
@@ -138,7 +139,7 @@ module.exports = {
138139
description: 'Select whether to create folders according to the spec’s paths or tags.',
139140
external: true,
140141
usage: ['CONVERSION'],
141-
supportedIn: [VERSION30, VERSION31]
142+
supportedIn: [VERSION20, VERSION30, VERSION31]
142143
},
143144
{
144145
name: 'Enable Schema Faking',
@@ -148,7 +149,7 @@ module.exports = {
148149
description: 'Whether or not schemas should be faked.',
149150
external: false,
150151
usage: ['CONVERSION'],
151-
supportedIn: [VERSION30, VERSION31]
152+
supportedIn: [VERSION20, VERSION30, VERSION31]
152153
},
153154
{
154155
name: 'Schema resolution nesting limit',
@@ -160,7 +161,7 @@ module.exports = {
160161
' option works correctly "optimizeConversion" option needs to be disabled)',
161162
external: false,
162163
usage: ['CONVERSION'],
163-
supportedIn: [VERSION30, VERSION31]
164+
supportedIn: [VERSION20, VERSION30, VERSION31]
164165
},
165166
{
166167
name: 'Include auth info in example requests',
@@ -170,7 +171,7 @@ module.exports = {
170171
description: 'Select whether to include authentication parameters in the example request',
171172
external: true,
172173
usage: ['CONVERSION'],
173-
supportedIn: [VERSION30, VERSION31]
174+
supportedIn: [VERSION20, VERSION30, VERSION31]
174175
},
175176
{
176177
name: 'Short error messages during request <> schema validation',
@@ -276,7 +277,7 @@ module.exports = {
276277
description: 'Whether to set optional parameters as disabled',
277278
external: true,
278279
usage: ['CONVERSION'],
279-
supportedIn: [VERSION30, VERSION31]
280+
supportedIn: [VERSION20, VERSION30, VERSION31]
280281
},
281282
{
282283
name: 'Keep implicit headers',
@@ -286,7 +287,7 @@ module.exports = {
286287
description: 'Whether to keep implicit headers from the OpenAPI specification, which are removed by default.',
287288
external: true,
288289
usage: ['CONVERSION'],
289-
supportedIn: [VERSION30, VERSION31]
290+
supportedIn: [VERSION20, VERSION30, VERSION31]
290291
},
291292
{
292293
name: 'Include webhooks',

lib/schemapack.js

Lines changed: 104 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// This is the default collection name if one can't be inferred from the OpenAPI spec
44
const COLLECTION_NAME = 'Imported from OpenAPI 3.0',
55
{ getConcreteSchemaUtils } = require('./common/versionUtils.js'),
6+
{ convertToOAS30IfSwagger } = require('./swaggerUtils/swaggerToOpenapi.js'),
67
BROWSER = 'browser',
78
Ajv = require('ajv'),
89
addFormats = require('ajv-formats'),
@@ -60,7 +61,10 @@ class SchemaPack {
6061
validate() {
6162
let input = this.input,
6263
json,
63-
specParseResult;
64+
specParseResult,
65+
isFolder = this.input.type === 'folder';
66+
67+
this.computedOptions = Object.assign({ isFolder }, this.computedOptions);
6468
if (!input) {
6569
return {
6670
result: false,
@@ -252,113 +256,121 @@ class SchemaPack {
252256
return callback(new OpenApiErr('The schema must be validated before attempting conversion'));
253257
}
254258

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+
}
258264

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));
264269

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}}');
267275

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');
270278

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);
278281

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+
}
283295
}
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+
});
295306

296-
generatedStore.collection.describe(schemaUtils.getCollectionDescription(openapi));
307+
generatedStore.collection.describe(schemaUtils.getCollectionDescription(openapi));
297308

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);
302313

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+
}
306317

307318

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+
}
333344

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);
343358
}
344-
}
345-
catch (e) {
346-
return callback(e);
347-
}
348359

349-
collectionJSON = generatedStore.collection.toJSON();
360+
collectionJSON = generatedStore.collection.toJSON();
350361

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;
355366

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+
});
362374
});
363375
}
364376

0 commit comments

Comments
 (0)