Skip to content

Commit 396cf51

Browse files
authored
Merge pull request #303 from edclements/feature/disable-optional-parameters
Disable optional parameters
2 parents bc1a09c + a5a495b commit 396cf51

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

OPTIONS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ suggestAvailableFixes|boolean|-|false|Whether to provide fixes for patching corr
1616
validateMetadata|boolean|-|false|Whether to show mismatches for incorrect name and description of request|VALIDATION
1717
ignoreUnresolvedVariables|boolean|-|false|Whether to ignore mismatches resulting from unresolved variables in the Postman request|VALIDATION
1818
strictRequestMatching|boolean|-|false|Whether requests should be strictly matched with schema operations. Setting to true will not include any matches where the URL path segments don't match exactly.|VALIDATION
19+
disableOptionalParameters|boolean|-|false|Whether to set optional parameters (not required) as disabled|CONVERSION

lib/options.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,15 @@ module.exports = {
201201
'include any matches where the URL path segments don\'t match exactly.',
202202
external: true,
203203
usage: ['VALIDATION']
204+
},
205+
{
206+
name: 'Disable optional parameters',
207+
id: 'disableOptionalParameters',
208+
type: 'boolean',
209+
default: false,
210+
description: 'Whether to set optional parameters (not required) as disabled',
211+
external: true,
212+
usage: ['CONVERSION']
204213
}
205214
];
206215

lib/schemaUtils.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ module.exports = {
920920
options.stackLimit) : '';
921921

922922
convertedPathVar = this.convertParamsWithStyle(variable, fakedData, PARAMETER_SOURCE.REQUEST,
923-
components, schemaCache);
923+
components, schemaCache, options);
924924

925925
variables = _.concat(variables, convertedPathVar);
926926
});
@@ -1455,7 +1455,7 @@ module.exports = {
14551455
// https://github.com/postmanlabs/postman-app-support/issues/6500
14561456
paramValue = paramValue.toString();
14571457
}
1458-
return this.convertParamsWithStyle(param, paramValue, PARAMETER_SOURCE.REQUEST, components, schemaCache);
1458+
return this.convertParamsWithStyle(param, paramValue, PARAMETER_SOURCE.REQUEST, components, schemaCache, options);
14591459
}
14601460

14611461
let description = this.getParameterDescription(param);
@@ -1486,11 +1486,12 @@ module.exports = {
14861486
* The styles are documented at
14871487
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#style-values
14881488
*/
1489-
convertParamsWithStyle: function(param, paramValue, parameterSource, components, schemaCache) {
1489+
convertParamsWithStyle: function(param, paramValue, parameterSource, components, schemaCache, options) {
14901490
var paramName = _.get(param, 'name'),
14911491
pmParams = [],
14921492
serialisedValue = '',
1493-
description = this.getParameterDescription(param);
1493+
description = this.getParameterDescription(param),
1494+
disabled = false;
14941495

14951496
// for invalid param object return null
14961497
if (!_.isObject(param)) {
@@ -1500,6 +1501,10 @@ module.exports = {
15001501
let { style, explode, startValue, propSeparator, keyValueSeparator, isExplodable } =
15011502
this.getParamSerialisationInfo(param, parameterSource, components, schemaCache);
15021503

1504+
if (options && options.disableOptionalParameters) {
1505+
disabled = !param.required;
1506+
}
1507+
15031508
// decide explodable params, starting value and separators between key-value and properties for serialisation
15041509
switch (style) {
15051510
case 'form':
@@ -1508,7 +1513,8 @@ module.exports = {
15081513
pmParams.push({
15091514
key: _.isArray(paramValue) ? paramName : key,
15101515
value: (value === undefined ? '' : value),
1511-
description
1516+
description,
1517+
disabled
15121518
});
15131519
});
15141520
return pmParams;
@@ -1525,7 +1531,8 @@ module.exports = {
15251531
pmParams.push({
15261532
key: param.name + '[' + key + ']',
15271533
value: (value === undefined ? '' : value),
1528-
description
1534+
description,
1535+
disabled
15291536
});
15301537
});
15311538
}
@@ -1555,7 +1562,8 @@ module.exports = {
15551562
pmParams.push({
15561563
key: paramName,
15571564
value: serialisedValue,
1558-
description
1565+
description,
1566+
disabled
15591567
});
15601568

15611569
return pmParams;
@@ -1597,7 +1605,7 @@ module.exports = {
15971605
}
15981606

15991607
convertedHeader = _.get(this.convertParamsWithStyle(header, fakeData, parameterSource,
1600-
components, schemaCache), '[0]');
1608+
components, schemaCache, options), '[0]');
16011609

16021610
reqHeader = new sdk.Header(convertedHeader);
16031611
reqHeader.description = this.getParameterDescription(header);
@@ -1671,7 +1679,7 @@ module.exports = {
16711679
};
16721680
encoding[key].description = description;
16731681
params = this.convertParamsWithStyle(encoding[key], value, PARAMETER_SOURCE.REQUEST, components,
1674-
schemaCache);
1682+
schemaCache, options);
16751683
// TODO: Show warning for incorrect schema if !params
16761684
params && params.forEach((element) => {
16771685
// Collection v2.1 schema allows urlencoded param value to be only string
@@ -2118,7 +2126,7 @@ module.exports = {
21182126
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache,
21192127
options.stackLimit) : '',
21202128
convertedPathVar = _.get(this.convertParamsWithStyle(element, fakedData, PARAMETER_SOURCE.REQUEST,
2121-
components, schemaCache), '[0]', {});
2129+
components, schemaCache, options), '[0]', {});
21222130

21232131
variableStore[element.name] = _.assign(convertedPathVar, { id: element.name, type: 'collection' });
21242132
}

test/system/structure.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ const optionIds = [
1919
'validateMetadata',
2020
'ignoreUnresolvedVariables',
2121
'optimizeConversion',
22-
'strictRequestMatching'
22+
'strictRequestMatching',
23+
'disableOptionalParameters'
2324
],
2425
expectedOptions = {
2526
collapseFolders: {

test/unit/base.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,8 @@ describe('CONVERT FUNCTION TESTS ', function() {
726726
{
727727
key: 'access_token',
728728
value: 'X-access-token',
729-
description: 'Access token'
729+
description: 'Access token',
730+
disabled: false
730731
}
731732
]);
732733
});
@@ -907,6 +908,26 @@ describe('CONVERT FUNCTION TESTS ', function() {
907908
});
908909
});
909910

911+
it('[Github #31] - should set optional params as disabled', function(done) {
912+
let options = { schemaFaker: true, disableOptionalParameters: true };
913+
Converter.convert({ type: 'file', data: requiredInParams }, options, (err, conversionResult) => {
914+
expect(err).to.be.null;
915+
let requests = conversionResult.output[0].data.item[0].item,
916+
request;
917+
918+
// GET /pets
919+
// query1 required, query2 optional
920+
// header1 required, header2 optional
921+
request = requests[0].request;
922+
expect(request.url.query[0].disabled).to.be.false;
923+
expect(request.url.query[1].disabled).to.be.true;
924+
expect(request.header[0].disabled).to.be.false;
925+
expect(request.header[1].disabled).to.be.true;
926+
927+
done();
928+
});
929+
});
930+
910931
it('Should prefer and use example from parameter object over schema example while faking schema', function(done) {
911932
Converter.convert({ type: 'file', data: parameterExamples },
912933
{ schemaFaker: true, requestParametersResolution: 'example' },

0 commit comments

Comments
 (0)