Skip to content

Commit ac4094e

Browse files
committed
Code review improvements
Code review improvements
1 parent 50b7b06 commit ac4094e

File tree

2 files changed

+33
-47
lines changed

2 files changed

+33
-47
lines changed

lib/schemaUtils.js

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function hash(input) {
133133
*/
134134
function verifyDeprecatedProperties(resolvedSchema, includeDeprecated) {
135135
traverseUtility(resolvedSchema.properties).forEach(function (property) {
136-
if (property) {
136+
if (property && typeof property === 'object') {
137137
if (property.deprecated === true && includeDeprecated === false) {
138138
this.delete();
139139
}
@@ -151,20 +151,21 @@ function verifyDeprecatedProperties(resolvedSchema, includeDeprecated) {
151151
* @param {string} parameterSourceOption Specifies whether the schema being faked is from a request or response.
152152
* @param {*} components list of predefined components (with schemas)
153153
* @param {string} schemaFormat default or xml
154-
* @param {string} indentCharacter char for 1 unit of indentation
155-
* @param {object} schemaCache - object storing schemaFaker and schmeResolution caches
156-
* @param {object} stackLimit - The depth to which schema resolution will happen for nested refs
157-
* @param {boolean} includeDeprecated - Whether to include the deprecated properties
154+
* @param {object} schemaCache - object storing schemaFaker and schemaResolution caches
155+
* @param {object} options - a standard list of options that's globally passed around. Check options.js for more.
158156
* @returns {object} fakedObject
159157
*/
160158
function safeSchemaFaker(oldSchema, resolveTo, resolveFor, parameterSourceOption, components,
161-
schemaFormat, indentCharacter, schemaCache, stackLimit, includeDeprecated) {
159+
schemaFormat, schemaCache, options) {
162160
var prop, key, resolvedSchema, fakedSchema,
163161
schemaResolutionCache = _.get(schemaCache, 'schemaResolutionCache', {}),
164162
schemaFakerCache = _.get(schemaCache, 'schemaFakerCache', {});
165163
let concreteUtils = components && components.hasOwnProperty('concreteUtils') ?
166164
components.concreteUtils :
167165
DEFAULT_SCHEMA_UTILS;
166+
const indentCharacter = _.get(options, 'indentCharacter', ' '),
167+
stackLimit = _.get(options, 'stackLimit', 10),
168+
includeDeprecated = _.get(options, 'includeDeprecated', true);
168169

169170
resolvedSchema = deref.resolveRefs(oldSchema, parameterSourceOption, components, schemaResolutionCache,
170171
resolveFor, resolveTo, 0, {}, stackLimit);
@@ -237,8 +238,11 @@ function safeSchemaFaker(oldSchema, resolveTo, resolveFor, parameterSourceOption
237238
* @returns {boolean} whether to add or not the deprecated operation
238239
*/
239240
function shouldAddDeprecatedOperation (operation, options) {
240-
return !operation.deprecated ||
241-
(operation.deprecated === true && options.includeDeprecated === true);
241+
if (typeof operation === 'object') {
242+
return !operation.deprecated ||
243+
(operation.deprecated === true && options.includeDeprecated === true);
244+
}
245+
return false;
242246
}
243247

244248

@@ -979,8 +983,7 @@ module.exports = {
979983

980984
fakedData = options.schemaFaker ?
981985
safeSchemaFaker(variable.schema || {}, options.requestParametersResolution, PROCESSING_TYPE.CONVERSION,
982-
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache,
983-
options.stackLimit, options.includeDeprecated) : '';
986+
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, schemaCache, options) : '';
984987

985988
convertedPathVar = this.convertParamsWithStyle(variable, fakedData, PARAMETER_SOURCE.REQUEST,
986989
components, schemaCache, options);
@@ -1438,25 +1441,20 @@ module.exports = {
14381441
header: [],
14391442
path: []
14401443
},
1441-
includeDeprecated = options.includeDeprecated !== undefined ?
1442-
options.includeDeprecated : true;
1444+
includeDeprecated = options.includeDeprecated !== false;
14431445

14441446
_.forEach(localParams, (param) => {
14451447
tempParam = param;
1446-
let shouldAddDeprecated = (includeDeprecated ||
1448+
let verifyAddDeprecated = (includeDeprecated ||
14471449
includeDeprecated === false && tempParam.deprecated !== true);
1448-
if (tempParam.in === 'query') {
1449-
if (shouldAddDeprecated) {
1450+
if (verifyAddDeprecated) {
1451+
if (tempParam.in === 'query') {
14501452
params.query.push(tempParam);
14511453
}
1452-
}
1453-
else if (tempParam.in === 'header') {
1454-
if (shouldAddDeprecated) {
1454+
else if (tempParam.in === 'header') {
14551455
params.header.push(tempParam);
14561456
}
1457-
}
1458-
else if (tempParam.in === 'path') {
1459-
if (shouldAddDeprecated) {
1457+
else if (tempParam.in === 'path') {
14601458
params.path.push(tempParam);
14611459
}
14621460
}
@@ -1584,8 +1582,7 @@ module.exports = {
15841582
}
15851583
// Do not fake the bodyData if the complexity is 10.
15861584
bodyData = safeSchemaFaker(bodyObj.schema || {}, resolveTo, PROCESSING_TYPE.CONVERSION, parameterSourceOption,
1587-
components, schemaFormat, indentCharacter, schemaCache, options.stackLimit,
1588-
options.includeDeprecated);
1585+
components, schemaFormat, schemaCache, options);
15891586
}
15901587
else {
15911588
// do not fake if the option is false
@@ -1651,9 +1648,7 @@ module.exports = {
16511648
// fake data generated
16521649
paramValue = options.schemaFaker ?
16531650
safeSchemaFaker(param.schema, resolveTo, PROCESSING_TYPE.CONVERSION, PARAMETER_SOURCE.REQUEST,
1654-
components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache, options.stackLimit,
1655-
options.includeDeprecated) : '';
1656-
// paramType = param.schema.type;
1651+
components, SCHEMA_FORMATS.DEFAULT, schemaCache, options) : '';
16571652

16581653
if (typeof paramValue === 'number' || typeof paramValue === 'boolean') {
16591654
// the SDK will keep the number-ness,
@@ -1678,7 +1673,7 @@ module.exports = {
16781673
},
16791674

16801675
/**
1681-
* Recursicely extracts key-value pair from deep objects.
1676+
* Recursively extracts key-value pair from deep objects.
16821677
*
16831678
* @param {*} deepObject - Deep object
16841679
* @param {*} objectKey - key associated with deep object
@@ -1829,8 +1824,7 @@ module.exports = {
18291824
this.assignParameterExamples(header);
18301825

18311826
fakeData = safeSchemaFaker(header.schema || {}, resolveTo, PROCESSING_TYPE.CONVERSION, parameterSource,
1832-
components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache, options.stackLimit,
1833-
options.includeDeprecated);
1827+
components, SCHEMA_FORMATS.DEFAULT, schemaCache, options);
18341828
}
18351829
}
18361830
else {
@@ -2426,8 +2420,7 @@ module.exports = {
24262420
if (!variableStore[element.name]) {
24272421
let fakedData = options.schemaFaker ?
24282422
safeSchemaFaker(element.schema || {}, options.requestParametersResolution, PROCESSING_TYPE.CONVERSION,
2429-
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache,
2430-
options.stackLimit, options.includeDeprecated) : '',
2423+
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, schemaCache, options) : '',
24312424
convertedPathVar = _.get(this.convertParamsWithStyle(element, fakedData, PARAMETER_SOURCE.REQUEST,
24322425
components, schemaCache, options), '[0]', {});
24332426

@@ -3337,8 +3330,7 @@ module.exports = {
33373330
key: txnParamName,
33383331
actualValue: valueToUse,
33393332
suggestedValue: safeSchemaFaker(openApiSchemaObj || {}, 'example', PROCESSING_TYPE.VALIDATION,
3340-
parameterSourceOption, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache,
3341-
options.stackLimit, options.includeDeprecated)
3333+
parameterSourceOption, components, SCHEMA_FORMATS.DEFAULT, schemaCache, options.includeDeprecated)
33423334
};
33433335
}
33443336

@@ -3354,8 +3346,7 @@ module.exports = {
33543346
let mismatchObj,
33553347
suggestedValue,
33563348
fakedValue = safeSchemaFaker(openApiSchemaObj || {}, 'example', PROCESSING_TYPE.VALIDATION,
3357-
parameterSourceOption, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache,
3358-
options.stackLimit, options.includeDeprecated);
3349+
parameterSourceOption, components, SCHEMA_FORMATS.DEFAULT, schemaCache, options);
33593350

33603351
// Show detailed validation mismatches for only request/response body
33613352
if (options.detailedBlobValidation && needJsonMatching) {
@@ -3634,8 +3625,7 @@ module.exports = {
36343625
suggestedValue: {
36353626
key: pathVar.name,
36363627
value: safeSchemaFaker(pathVar.schema || {}, 'example', PROCESSING_TYPE.VALIDATION,
3637-
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache,
3638-
options.stackLimit, options.includeDeprecated),
3628+
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, schemaCache, options),
36393629
description: this.getParameterDescription(pathVar)
36403630
}
36413631
};
@@ -3928,8 +3918,7 @@ module.exports = {
39283918
suggestedValue: {
39293919
key: qp.name,
39303920
value: safeSchemaFaker(qp.schema || {}, 'example', PROCESSING_TYPE.VALIDATION,
3931-
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache,
3932-
options.stackLimit, options.includeDeprecated),
3921+
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, schemaCache, options),
39333922
description: this.getParameterDescription(qp)
39343923
}
39353924
};
@@ -4185,8 +4174,7 @@ module.exports = {
41854174
suggestedValue: {
41864175
key: header.name,
41874176
value: safeSchemaFaker(header.schema || {}, 'example', PROCESSING_TYPE.VALIDATION,
4188-
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache,
4189-
options.stackLimit, options.includeDeprecated),
4177+
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, schemaCache, options),
41904178
description: this.getParameterDescription(header)
41914179
}
41924180
};
@@ -4293,8 +4281,7 @@ module.exports = {
42934281
suggestedValue: {
42944282
key: header.name,
42954283
value: safeSchemaFaker(header.schema || {}, 'example', PROCESSING_TYPE.VALIDATION,
4296-
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache,
4297-
options.stackLimit, options.includeDeprecated),
4284+
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, schemaCache, options),
42984285
description: this.getParameterDescription(header)
42994286
}
43004287
};
@@ -4506,8 +4493,7 @@ module.exports = {
45064493
suggestedValue: {
45074494
key: uParam.name,
45084495
value: safeSchemaFaker(uParam.schema || {}, 'example', PROCESSING_TYPE.VALIDATION,
4509-
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache,
4510-
options.stackLimit, options.includeDeprecated),
4496+
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, schemaCache, options),
45114497
description: this.getParameterDescription(uParam)
45124498
}
45134499
};

test/unit/util.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
189189
},
190190
key = hash('resolveToSchema ' + JSON.stringify(resolvedSchema)),
191191
fakedSchema = SchemaUtils.safeSchemaFaker(schema, resolveTo, resolveFor, parameterSource,
192-
{ components, concreteUtils }, 'default', ' ', schemaCache);
192+
{ components, concreteUtils }, 'default', schemaCache);
193193

194194
expect(schemaCache.schemaFakerCache).to.have.property(key);
195195
expect(schemaCache.schemaFakerCache[key]).to.equal(fakedSchema);
@@ -232,7 +232,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
232232
),
233233
key = hash('resolveToExample ' + JSON.stringify(resolvedSchema)),
234234
fakedSchema = SchemaUtils.safeSchemaFaker(schema, resolveTo, resolveFor, parameterSource,
235-
{ components, concreteUtils }, 'default', ' ', schemaCache);
235+
{ components, concreteUtils }, 'default', schemaCache);
236236

237237
expect(schemaCache.schemaFakerCache).to.have.property(key);
238238
expect(schemaCache.schemaFakerCache[key]).to.equal(fakedSchema);

0 commit comments

Comments
 (0)