Skip to content

Commit 4560c34

Browse files
authored
Merge pull request #709 from postmanlabs/feature/enable-optional-params
Added support for enableOptionalParameters option in v2 interface.
2 parents 2be5cfb + 6f217f7 commit 4560c34

File tree

9 files changed

+730
-16
lines changed

9 files changed

+730
-16
lines changed

OPTIONS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ validateMetadata|boolean|-|false|Whether to show mismatches for incorrect name a
1818
ignoreUnresolvedVariables|boolean|-|false|Whether to ignore mismatches resulting from unresolved variables in the Postman request|VALIDATION|v2, v1
1919
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|v2, v1
2020
allowUrlPathVarMatching|boolean|-|false|Whether to allow matching path variables that are available as part of URL itself in the collection request|VALIDATION|v2, v1
21-
enableOptionalParameters|boolean|-|true|Optional parameters aren't selected in the collection. Once enabled they will be selected in the collection and request as well.|CONVERSION|v1
21+
enableOptionalParameters|boolean|-|true|Optional parameters aren't selected in the collection. Once enabled they will be selected in the collection and request as well.|CONVERSION|v2, v1
2222
keepImplicitHeaders|boolean|-|false|Whether to keep implicit headers from the OpenAPI specification, which are removed by default.|CONVERSION|v2, v1
2323
includeDeprecated|boolean|-|true|Select whether to include deprecated operations, parameters, and properties in generated collection or not|CONVERSION, VALIDATION|v2, v1

lib/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ module.exports = {
328328
external: true,
329329
usage: ['CONVERSION'],
330330
supportedIn: [VERSION20, VERSION30, VERSION31],
331-
supportedModuleVersion: [MODULE_VERSION.V1]
331+
supportedModuleVersion: [MODULE_VERSION.V2, MODULE_VERSION.V1]
332332
},
333333
{
334334
name: 'Keep implicit headers',

libV2/schemaUtils.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -926,11 +926,13 @@ let QUERYPARAM = 'query',
926926

927927
serialiseParamsBasedOnStyle = (context, param, paramValue) => {
928928
const { style, explode, startValue, propSeparator, keyValueSeparator, isExplodable } =
929-
getParamSerialisationInfo(context, param);
929+
getParamSerialisationInfo(context, param),
930+
{ enableOptionalParameters } = context.computedOptions;
930931

931932
let serialisedValue = '',
932933
description = getParameterDescription(param),
933934
paramName = _.get(param, 'name'),
935+
disabled = !enableOptionalParameters && _.get(param, 'required') !== true,
934936
pmParams = [],
935937
isNotSerializable = false;
936938

@@ -945,7 +947,8 @@ let QUERYPARAM = 'query',
945947
pmParams.push({
946948
key: isArrayValue ? paramName : key,
947949
value: (value === undefined ? '' : _.toString(value)),
948-
description
950+
description,
951+
disabled
949952
});
950953
});
951954

@@ -961,7 +964,8 @@ let QUERYPARAM = 'query',
961964
pmParams.push({
962965
key: extractedParam.key,
963966
value: _.toString(extractedParam.value) || '',
964-
description
967+
description,
968+
disabled
965969
});
966970
});
967971

@@ -971,7 +975,8 @@ let QUERYPARAM = 'query',
971975
isNotSerializable = true;
972976
pmParams.push({
973977
key: paramName,
974-
value: '<Error: Not supported in OAS>'
978+
value: '<Error: Not supported in OAS>',
979+
disabled
975980
});
976981
}
977982

@@ -1004,7 +1009,8 @@ let QUERYPARAM = 'query',
10041009
pmParams.push({
10051010
key: paramName,
10061011
value: _.toString(serialisedValue),
1007-
description
1012+
description,
1013+
disabled
10081014
});
10091015

10101016
return pmParams;

libV2/validationUtils.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ function checkQueryParams (context, queryParams, transactionPathPrefix, schemaPa
17351735
requestQueryParams = [],
17361736
resolvedSchemaParams = [],
17371737
mismatchProperty = 'QUERYPARAM',
1738-
{ includeDeprecated } = context.computedOptions;
1738+
{ includeDeprecated, enableOptionalParameters } = context.computedOptions;
17391739

17401740
if (options.validationPropertiesToIgnore.includes(mismatchProperty)) {
17411741
return callback(null, []);
@@ -1837,6 +1837,11 @@ function checkQueryParams (context, queryParams, transactionPathPrefix, schemaPa
18371837
return;
18381838
}
18391839

1840+
// If optional parameters are disabled, do not report them as missing
1841+
if (!enableOptionalParameters && qp.required !== true) {
1842+
return;
1843+
}
1844+
18401845
if (!_.find(requestQueryParams, (param) => {
18411846
return param.key === qp.name;
18421847
})) {
@@ -1888,7 +1893,7 @@ function checkRequestHeaders (context, headers, transactionPathPrefix, schemaPat
18881893
return !_.includes(IMPLICIT_HEADERS, _.toLower(_.get(header, 'key')));
18891894
}),
18901895
mismatchProperty = 'HEADER',
1891-
{ includeDeprecated } = context.computedOptions;
1896+
{ includeDeprecated, enableOptionalParameters } = context.computedOptions;
18921897

18931898
if (options.validationPropertiesToIgnore.includes(mismatchProperty)) {
18941899
return callback(null, []);
@@ -1974,6 +1979,11 @@ function checkRequestHeaders (context, headers, transactionPathPrefix, schemaPat
19741979
return;
19751980
}
19761981

1982+
// If optional parameters are disabled, do not report them as missing
1983+
if (!enableOptionalParameters && header.required !== true) {
1984+
return;
1985+
}
1986+
19771987
// assign parameter example(s) as schema examples;
19781988
assignParameterExamples(header);
19791989

@@ -2019,7 +2029,7 @@ function checkResponseHeaders (context, schemaResponse, headers, transactionPath
20192029
return !_.includes(IMPLICIT_HEADERS, _.toLower(_.get(header, 'key')));
20202030
}),
20212031
mismatchProperty = 'RESPONSE_HEADER',
2022-
{ includeDeprecated } = context.computedOptions;
2032+
{ includeDeprecated, enableOptionalParameters } = context.computedOptions;
20232033

20242034
if (options.validationPropertiesToIgnore.includes(mismatchProperty)) {
20252035
return callback(null, []);
@@ -2098,6 +2108,11 @@ function checkResponseHeaders (context, schemaResponse, headers, transactionPath
20982108
return;
20992109
}
21002110

2111+
// If optional parameters are disabled, do not report them as missing
2112+
if (!enableOptionalParameters && header.required !== true) {
2113+
return;
2114+
}
2115+
21012116
if (!_.find(resHeaders, (param) => { return param.key === header.name; })) {
21022117

21032118
// assign parameter example(s) as schema examples;
@@ -2139,7 +2154,7 @@ function checkRequestBody (context, requestBody, transactionPathPrefix, schemaPa
21392154
let jsonSchemaBody,
21402155
jsonContentType,
21412156
mismatchProperty = 'BODY',
2142-
{ includeDeprecated } = context.computedOptions;
2157+
{ includeDeprecated, enableOptionalParameters } = context.computedOptions;
21432158

21442159
if (options.validationPropertiesToIgnore.includes(mismatchProperty)) {
21452160
return callback(null, []);
@@ -2278,6 +2293,11 @@ function checkRequestBody (context, requestBody, transactionPathPrefix, schemaPa
22782293
return;
22792294
}
22802295

2296+
// If optional parameters are disabled, do not report them as missing
2297+
if (!enableOptionalParameters && uParam.required !== true) {
2298+
return;
2299+
}
2300+
22812301
// report mismatches only for required properties
22822302
if (!_.find(filteredUrlEncodedBody, (param) => { return param.key === uParam.name; })) {
22832303
mismatchObj = {

0 commit comments

Comments
 (0)