Skip to content

Commit 5c22baf

Browse files
committed
Added support for usage of interface version in CLI conversions with v2 as default
1 parent 58d2e27 commit 5c22baf

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

bin/openapi2postmanv2.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var _ = require('lodash'),
1212
definedOptions,
1313
testFlag,
1414
swaggerInput,
15+
interfaceVersion,
1516
swaggerData;
1617

1718
/**
@@ -41,6 +42,14 @@ function parseOptions (value) {
4142
console.warn('\x1b[33m%s\x1b[0m', 'Warning: Invalid option supplied ', option[0]);
4243
}
4344
});
45+
46+
/**
47+
* As v2 interface uses parametersResolution instead of previous requestParametersResolution option,
48+
* override value of parametersResolution if it's not defined and requestParametersResolution is defined
49+
*/
50+
if (_.has(parsedOptions, 'requestParametersResolution') && !_.has(parsedOptions, 'parametersResolution')) {
51+
parsedOptions.parametersResolution = parsedOptions.requestParametersResolution;
52+
}
4453
return parsedOptions;
4554
}
4655

@@ -50,6 +59,7 @@ program
5059
.option('-o, --output <output>', 'Write the collection to an output file')
5160
.option('-t, --test', 'Test the OPENAPI converter')
5261
.option('-p, --pretty', 'Pretty print the JSON file')
62+
.option('-i, --interface-version <interfaceVersion>', 'Interface version of convert() to be used')
5363
.option('-c, --options-config <optionsConfig>', 'JSON file containing Converter options')
5464
.option('-O, --options <options>', 'comma separated list of options', parseOptions);
5565

@@ -76,6 +86,7 @@ inputFile = program.spec;
7686
outputFile = program.output || false;
7787
testFlag = program.test || false;
7888
prettyPrintFlag = program.pretty || false;
89+
interfaceVersion = program.interfaceVersion || 'v2';
7990
configFile = program.optionsConfig || false;
8091
definedOptions = (!(program.options instanceof Array) ? program.options : {});
8192
swaggerInput;
@@ -112,7 +123,8 @@ function writetoFile(prettyPrintFlag, file, collection) {
112123
* @returns {void}
113124
*/
114125
function convert(swaggerData) {
115-
let options = {};
126+
let options = {},
127+
convertFn = interfaceVersion === 'v1' ? 'convert' : 'convertV2';
116128

117129
// apply options from config file if present
118130
if (configFile) {
@@ -126,7 +138,7 @@ function convert(swaggerData) {
126138
options = definedOptions;
127139
}
128140

129-
Converter.convert({
141+
Converter[convertFn]({
130142
type: 'string',
131143
data: swaggerData
132144
}, options, (err, status) => {

libV2/schemaUtils.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,19 @@ let QUERYPARAM = 'query',
236236
return { collectionVariables, pathVariables, baseUrl };
237237
},
238238

239+
/**
240+
* Provides ref stack limit for current instance
241+
* @param {*} stackLimit - Defined stackLimit in options
242+
*
243+
* @returns {Number} Returns the stackLimit to be used
244+
*/
245+
getRefStackLimit = (stackLimit) => {
246+
if (typeof stackLimit === 'number' && stackLimit > REF_STACK_LIMIT) {
247+
return stackLimit;
248+
}
249+
return REF_STACK_LIMIT;
250+
},
251+
239252
/**
240253
* Resolve a given ref from the schema
241254
* @param {Object} context - Global context object
@@ -246,9 +259,10 @@ let QUERYPARAM = 'query',
246259
* @returns {Object} Returns the object that staisfies the schema
247260
*/
248261
resolveRefFromSchema = (context, $ref, stackDepth = 0, seenRef = {}) => {
249-
const { specComponents } = context;
262+
const { specComponents } = context,
263+
{ stackLimit } = context.computedOptions;
250264

251-
if (stackDepth >= REF_STACK_LIMIT) {
265+
if (stackDepth >= getRefStackLimit(stackLimit)) {
252266
return { value: ERR_TOO_MANY_LEVELS };
253267
}
254268

@@ -315,9 +329,10 @@ let QUERYPARAM = 'query',
315329
* @returns {Object} Returns the object that staisfies the schema
316330
*/
317331
resolveRefForExamples = (context, $ref, stackDepth = 0, seenRef = {}) => {
318-
const { specComponents } = context;
332+
const { specComponents } = context,
333+
{ stackLimit } = context.computedOptions;
319334

320-
if (stackDepth >= REF_STACK_LIMIT) {
335+
if (stackDepth >= getRefStackLimit(stackLimit)) {
321336
return { value: ERR_TOO_MANY_LEVELS };
322337
}
323338

@@ -467,12 +482,15 @@ let QUERYPARAM = 'query',
467482
return new Error('Schema is empty');
468483
}
469484

470-
if (stack >= REF_STACK_LIMIT) {
485+
const { stackLimit } = context.computedOptions;
486+
487+
if (stack >= getRefStackLimit(stackLimit)) {
471488
return { value: ERR_TOO_MANY_LEVELS };
472489
}
473490

474491
stack++;
475492

493+
// eslint-disable-next-line one-var
476494
const compositeKeyword = schema.anyOf ? 'anyOf' : 'oneOf',
477495
{ concreteUtils } = context;
478496

0 commit comments

Comments
 (0)