Skip to content

Commit a97d6f8

Browse files
authored
Merge pull request #701 from postmanlabs/feature/cli-v2-usage
Added support for usage of interface version in CLI conversions with v2 as default.
2 parents e65aa1b + 952cf91 commit a97d6f8

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,31 @@ The converter can be used as a CLI tool as well. The following [command line opt
5656

5757
### Options
5858

59-
- `-s <source>`, `--spec <source>`
59+
- `-s <source>`, `--spec <source>`
6060
Used to specify the OpenAPI specification (file path) which is to be converted
6161

62-
- `-o <destination>`, `--output <destination>`
62+
- `-o <destination>`, `--output <destination>`
6363
Used to specify the destination file in which the collection is to be written
6464

65-
- `-p`, `--pretty`
65+
- `-p`, `--pretty`
6666
Used to pretty print the collection object while writing to a file
6767

68-
- `-O`, `--options`
68+
- `-i`, `--interface-version`
69+
Specifies the interface version of the converter to be used. Value can be 'v2' or 'v1'. Default is 'v2'.
70+
71+
- `-O`, `--options`
6972
Used to supply options to the converter, for complete options details see [here](/OPTIONS.md)
7073

71-
- `-c`, `--options-config`
74+
- `-c`, `--options-config`
7275
Used to supply options to the converter through config file, for complete options details see [here](/OPTIONS.md)
7376

74-
- `-t`, `--test`
77+
- `-t`, `--test`
7578
Used to test the collection with an in-built sample specification
7679

77-
- `-v`, `--version`
80+
- `-v`, `--version`
7881
Specifies the version of the converter
7982

80-
- `-h`, `--help`
83+
- `-h`, `--help`
8184
Specifies all the options along with a few usage examples on the terminal
8285

8386

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)