From 1716c4cdc3123f26c26c8d060035982ea1036e01 Mon Sep 17 00:00:00 2001 From: Vishal Shingala Date: Tue, 17 Aug 2021 17:11:00 +0530 Subject: [PATCH] Added support for stackLimit option to customize schema resolution level --- lib/deref.js | 6 +++--- lib/options.js | 13 +++++++++++-- lib/utils.js | 9 +++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/deref.js b/lib/deref.js index 13a739a..d38e535 100644 --- a/lib/deref.js +++ b/lib/deref.js @@ -218,7 +218,7 @@ module.exports = { this._currentRefStack.push(refKey); let refResolvedSchema = this.resolveRefs(resolvedSchema, parameterSourceOption, - components, schemaResolutionCache, resolveFor, resolveTo, stack, _.cloneDeep(seenRef)); + components, schemaResolutionCache, resolveFor, resolveTo, stack, _.cloneDeep(seenRef), stackLimit); // remove current ref that's being resolved from stack as soon as resolved _.isArray(this._currentRefStack) && (this._currentRefStack.pop()); @@ -258,7 +258,7 @@ module.exports = { } /* eslint-enable */ tempSchema.properties[prop] = this.resolveRefs(property, parameterSourceOption, components, - schemaResolutionCache, resolveFor, resolveTo, stack, _.cloneDeep(seenRef)); + schemaResolutionCache, resolveFor, resolveTo, stack, _.cloneDeep(seenRef), stackLimit); } } return tempSchema; @@ -296,7 +296,7 @@ module.exports = { // without this, schemas with circular references aren't faked correctly let tempSchema = _.omit(schema, 'items'); tempSchema.items = this.resolveRefs(schema.items, parameterSourceOption, - components, schemaResolutionCache, resolveFor, resolveTo, stack, _.cloneDeep(seenRef)); + components, schemaResolutionCache, resolveFor, resolveTo, stack, _.cloneDeep(seenRef), stackLimit); return tempSchema; } else if (!schema.hasOwnProperty('default')) { diff --git a/lib/options.js b/lib/options.js index c0cc1b6..f5a4925 100644 --- a/lib/options.js +++ b/lib/options.js @@ -116,6 +116,15 @@ module.exports = { external: false, usage: ['CONVERSION'] }, + { + name: 'Schema resolution nesting limit', + id: 'stackLimit', + type: 'integer', + default: 10, + description: 'Number of nesting limit till which schema resolution will happen.', + external: false, + usage: ['CONVERSION'] + }, { name: 'Include auth info in example requests', id: 'includeAuthInfoInExample', @@ -246,8 +255,8 @@ module.exports = { // options to be used as documentation return _.filter(optsArray, (opt) => { - // only return options that are externally controllable - return opt.external === true; + // only return options that are not disabled + return opt.disabled !== true; }); } }; diff --git a/lib/utils.js b/lib/utils.js index 9ead6e4..8b23c88 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -66,6 +66,15 @@ module.exports = { } break; + case 'integer': + if (_.isSafeInteger(userOptions[id])) { + retVal[id] = userOptions[id]; + } + else { + retVal[id] = defaultOptions[id].default; + } + break; + default: retVal[id] = defaultOptions[id].default; }