Skip to content

Commit 790cd5c

Browse files
committed
Used safer lodash object functions to avoid type errors
1 parent 0d9af9f commit 790cd5c

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

lib/schemaUtils.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,9 @@ module.exports = {
288288
// The complexity is 10.
289289
if (size < 8) {
290290
// Finds the number of requests that would be generated from this spec
291-
if (spec.paths) {
291+
if (_.isObject(spec.paths)) {
292292
Object.values(spec.paths).forEach((value) => {
293-
Object.keys(value).forEach((key) => {
293+
_.keys(value).forEach((key) => {
294294
if (METHODS.includes(key)) {
295295
numberOfRequests++;
296296
}
@@ -645,7 +645,7 @@ module.exports = {
645645
pathLength = currentPath.length;
646646

647647
// get method names available for this path
648-
pathMethods = getPathMethods(Object.keys(currentPathObject));
648+
pathMethods = getPathMethods(_.keys(currentPathObject));
649649

650650
// the number of requests under this node
651651
currentPathRequestCount = pathMethods.length;
@@ -724,7 +724,7 @@ module.exports = {
724724
variableStore = {},
725725
webhooksVariables = [];
726726

727-
if (Object.keys(webhooksTree.root.children).length === 0) {
727+
if (_.keys(webhooksTree.root.children).length === 0) {
728728
return;
729729
}
730730

@@ -1059,7 +1059,7 @@ module.exports = {
10591059
// with parent folder.
10601060
/* eslint-disable max-depth */
10611061
if (resource.childCount === 1 && options.collapseFolders) {
1062-
let subChild = Object.keys(resource.children)[0],
1062+
let subChild = _.keys(resource.children)[0],
10631063
resourceSubChild = resource.children[subChild];
10641064

10651065
resourceSubChild.name = resource.name + '/' + resourceSubChild.name;
@@ -1140,7 +1140,7 @@ module.exports = {
11401140
};
11411141
return false;
11421142
}
1143-
securityDef = _.get(openapi, ['securityDefs', Object.keys(security)[0]]);
1143+
securityDef = _.get(openapi, ['securityDefs', _.keys(security)[0]]);
11441144
if (!_.isObject(securityDef)) {
11451145
return;
11461146
}
@@ -1189,7 +1189,7 @@ module.exports = {
11891189
oauth2: []
11901190
};
11911191

1192-
if (_.isObject(securityDef.flows) && FLOW_TYPE[Object.keys(securityDef.flows)[0]]) {
1192+
if (_.isObject(securityDef.flows) && FLOW_TYPE[_.keys(securityDef.flows)[0]]) {
11931193
/*
11941194
11951195
//===================[]========================\\
@@ -1209,8 +1209,8 @@ module.exports = {
12091209
• "authorization_code_with_pkce"
12101210
12111211
*/
1212-
currentFlowType = FLOW_TYPE[Object.keys(securityDef.flows)[0]];
1213-
flowObj = _.get(securityDef, `flows.${Object.keys(securityDef.flows)[0]}`);
1212+
currentFlowType = FLOW_TYPE[_.keys(securityDef.flows)[0]];
1213+
flowObj = _.get(securityDef, `flows.${_.keys(securityDef.flows)[0]}`);
12141214
}
12151215

12161216
if (currentFlowType) { // Means the flow is of supported type
@@ -1219,7 +1219,7 @@ module.exports = {
12191219
if (!_.isEmpty(flowObj.scopes)) {
12201220
helper.oauth2.push({
12211221
key: 'scope',
1222-
value: Object.keys(flowObj.scopes).join(' ')
1222+
value: _.keys(flowObj.scopes).join(' ')
12231223
});
12241224
}
12251225

@@ -1408,7 +1408,7 @@ module.exports = {
14081408
responseBody: ''
14091409
};
14101410
}
1411-
let headers = Object.keys(contentObj);
1411+
let headers = _.keys(contentObj);
14121412

14131413
for (let i = 0; i < headers.length; i++) {
14141414
let headerFamily = this.getHeaderFamily(headers[i]);
@@ -1423,7 +1423,7 @@ module.exports = {
14231423

14241424
// if no JSON or XML, take whatever we have
14251425
if (!hasComputedType) {
1426-
cTypes = Object.keys(contentObj);
1426+
cTypes = _.keys(contentObj);
14271427
if (cTypes.length > 0) {
14281428
cTypeHeader = cTypes[0];
14291429
hasComputedType = true;
@@ -1521,7 +1521,7 @@ module.exports = {
15211521
return '';
15221522
}
15231523

1524-
exampleKey = Object.keys(exampleObj)[0];
1524+
exampleKey = _.keys(exampleObj)[0];
15251525
example = exampleObj[exampleKey];
15261526
// return example value if present else example is returned
15271527

@@ -1722,7 +1722,7 @@ module.exports = {
17221722
extractDeepObjectParams: function (deepObject, objectKey) {
17231723
let extractedParams = [];
17241724

1725-
Object.keys(deepObject).forEach((key) => {
1725+
_.keys(deepObject).forEach((key) => {
17261726
let value = deepObject[key];
17271727
if (value && typeof value === 'object') {
17281728
extractedParams = _.concat(extractedParams, this.extractDeepObjectParams(value, objectKey + '[' + key + ']'));
@@ -2250,12 +2250,12 @@ module.exports = {
22502250
previewLanguage = PREVIEW_LANGUAGE.JSON;
22512251
}
22522252
}
2253-
else if (response.content && Object.keys(response.content).length > 0) {
2254-
responseHeaders.push({ key: 'Content-Type', value: Object.keys(response.content)[0] });
2255-
if (this.getHeaderFamily(Object.keys(response.content)[0]) === HEADER_TYPE.JSON) {
2253+
else if (response.content && _.keys(response.content).length > 0) {
2254+
responseHeaders.push({ key: 'Content-Type', value: _.keys(response.content)[0] });
2255+
if (this.getHeaderFamily(_.keys(response.content)[0]) === HEADER_TYPE.JSON) {
22562256
previewLanguage = PREVIEW_LANGUAGE.JSON;
22572257
}
2258-
else if (this.getHeaderFamily(Object.keys(response.content)[0]) === HEADER_TYPE.XML) {
2258+
else if (this.getHeaderFamily(_.keys(response.content)[0]) === HEADER_TYPE.XML) {
22592259
previewLanguage = PREVIEW_LANGUAGE.XML;
22602260
}
22612261
else if (responseBodyWrapper.isJsonLike) {
@@ -5142,9 +5142,10 @@ module.exports = {
51425142
bundleOutput;
51435143

51445144
if (isSwagger(version)) {
5145-
Object.entries(contentAndComponents.components).forEach(([key, value]) => {
5146-
bundledFile[key] = value;
5147-
});
5145+
_.isObject(contentAndComponents.components) &&
5146+
Object.entries(contentAndComponents.components).forEach(([key, value]) => {
5147+
bundledFile[key] = value;
5148+
});
51485149
}
51495150
else if (!_.isEmpty(contentAndComponents.components)) {
51505151
bundledFile.components = contentAndComponents.components;

libV2/schemaUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ let QUERYPARAM = 'query',
896896
extractDeepObjectParams = (deepObject, objectKey) => {
897897
let extractedParams = [];
898898

899-
Object.keys(deepObject).forEach((key) => {
899+
_.keys(deepObject).forEach((key) => {
900900
let value = deepObject[key];
901901
if (value && typeof value === 'object') {
902902
extractedParams = _.concat(extractedParams, extractDeepObjectParams(value, objectKey + '[' + key + ']'));

0 commit comments

Comments
 (0)