From 1bde5d603a1686ba8f6a74aec1ab8b3cfb51e8ff Mon Sep 17 00:00:00 2001 From: Ed Clements Date: Fri, 18 Sep 2020 15:10:59 +0100 Subject: [PATCH] Disable optional parameters Adds a new option disableOptionalParameters to mark optional parameters (required is false) as disabled. --- lib/options.js | 9 +++++++++ lib/schemaUtils.js | 28 ++++++++++++++++++---------- test/unit/base.test.js | 22 +++++++++++++++++++++- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/lib/options.js b/lib/options.js index b5b98a8..3c0cba6 100644 --- a/lib/options.js +++ b/lib/options.js @@ -201,6 +201,15 @@ module.exports = { 'include any matches where the URL path segments don\'t match exactly.', external: true, usage: ['VALIDATION'] + }, + { + name: 'Disable optional parameters', + id: 'disableOptionalParameters', + type: 'boolean', + default: false, + description: 'Whether to set optional parameters (not required) as disabled', + external: true, + usage: ['CONVERSION'] } ]; diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index b5e909e..77be9a1 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -892,7 +892,7 @@ module.exports = { PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache, options.stackLimit) : '', convertedPathVar = this.convertParamsWithStyle(variable, fakedData, PARAMETER_SOURCE.REQUEST, - components, schemaCache); + components, schemaCache, options); variables = _.concat(variables, convertedPathVar); }); @@ -1425,7 +1425,7 @@ module.exports = { // https://github.com/postmanlabs/postman-app-support/issues/6500 paramValue = paramValue.toString(); } - return this.convertParamsWithStyle(param, paramValue, PARAMETER_SOURCE.REQUEST, components, schemaCache); + return this.convertParamsWithStyle(param, paramValue, PARAMETER_SOURCE.REQUEST, components, schemaCache, options); } let description = this.getParameterDescription(param); @@ -1456,11 +1456,12 @@ module.exports = { * The styles are documented at * https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#style-values */ - convertParamsWithStyle: function(param, paramValue, parameterSource, components, schemaCache) { + convertParamsWithStyle: function(param, paramValue, parameterSource, components, schemaCache, options) { var paramName = _.get(param, 'name'), pmParams = [], serialisedValue = '', - description = this.getParameterDescription(param); + description = this.getParameterDescription(param), + disabled = false; // for invalid param object return null if (!_.isObject(param)) { @@ -1470,6 +1471,10 @@ module.exports = { let { style, explode, startValue, propSeparator, keyValueSeparator, isExplodable } = this.getParamSerialisationInfo(param, parameterSource, components, schemaCache); + if (options && options.disableOptionalParameters) { + disabled = !param.required; + } + // decide explodable params, starting value and separators between key-value and properties for serialisation switch (style) { case 'form': @@ -1478,7 +1483,8 @@ module.exports = { pmParams.push({ key: _.isArray(paramValue) ? paramName : key, value: (value === undefined ? '' : value), - description + description, + disabled }); }); return pmParams; @@ -1489,7 +1495,8 @@ module.exports = { pmParams.push({ key: param.name + '[' + key + ']', value: (value === undefined ? '' : value), - description + description, + disabled }); }); return pmParams; @@ -1518,7 +1525,8 @@ module.exports = { pmParams.push({ key: paramName, value: serialisedValue, - description + description, + disabled }); return pmParams; @@ -1558,7 +1566,7 @@ module.exports = { } convertedHeader = _.get(this.convertParamsWithStyle(header, fakeData, parameterSource, - components, schemaCache), '[0]'); + components, schemaCache, options), '[0]'); reqHeader = new sdk.Header(convertedHeader); reqHeader.description = this.getParameterDescription(header); @@ -1632,7 +1640,7 @@ module.exports = { }; encoding[key].description = description; params = this.convertParamsWithStyle(encoding[key], value, PARAMETER_SOURCE.REQUEST, components, - schemaCache); + schemaCache, options); // TODO: Show warning for incorrect schema if !params params && params.forEach((element) => { // Collection v2.1 schema allows urlencoded param value to be only string @@ -2079,7 +2087,7 @@ module.exports = { PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache, options.stackLimit) : '', convertedPathVar = _.get(this.convertParamsWithStyle(element, fakedData, PARAMETER_SOURCE.REQUEST, - components, schemaCache), '[0]', {}); + components, schemaCache, options), '[0]', {}); variableStore[element.name] = _.assign(convertedPathVar, { id: element.name, type: 'collection' }); } diff --git a/test/unit/base.test.js b/test/unit/base.test.js index f3762be..e04ebc7 100644 --- a/test/unit/base.test.js +++ b/test/unit/base.test.js @@ -725,7 +725,8 @@ describe('CONVERT FUNCTION TESTS ', function() { { key: 'access_token', value: 'X-access-token', - description: 'Access token' + description: 'Access token', + disabled: false } ]); }); @@ -905,6 +906,25 @@ describe('CONVERT FUNCTION TESTS ', function() { done(); }); }); + + it('[Github #31] - should set optional params as disabled', function(done) { + Converter.convert({ type: 'file', data: requiredInParams }, { schemaFaker: true, disableOptionalParameters: true }, (err, conversionResult) => { + expect(err).to.be.null; + let requests = conversionResult.output[0].data.item[0].item, + request; + + // GET /pets + // query1 required, query2 optional + // header1 required, header2 optional + request = requests[0].request; + expect(request.url.query[0].disabled).to.be.false; + expect(request.url.query[1].disabled).to.be.true; + expect(request.header[0].disabled).to.be.false; + expect(request.header[1].disabled).to.be.true; + + done(); + }); + }); }); describe('requestNameSource option', function() {