diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index e203f1d35..e2f52664f 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -693,6 +693,13 @@ module.exports = { key, variableStore = {}; + // Resolving $ref, if present in any of the Path Objects + for (const path in spec.paths) { + if (spec.paths[path].hasOwnProperty('$ref')) { + spec.paths[path] = this.getRefObject(spec.paths[path].$ref, spec, options); + } + } + /** We need a trie because the decision of whether or not a node is a folder or request can only be made once the whole trie is generated @@ -1953,16 +1960,11 @@ module.exports = { }); // at this stage, savedSchema is [components, part1, parts] // must have min. 2 segments after "#/components" - if (savedSchema.length < 3) { + if (savedSchema.length < 2) { console.warn(`ref ${$ref} not found.`); return { value: `reference ${$ref} not found in the given specification` }; } - if (savedSchema[0] !== 'components' && savedSchema[0] !== 'paths') { - console.warn(`Error reading ${$ref}. Can only use references from components and paths`); - return { value: `Error reading ${$ref}. Can only use references from components and paths` }; - } - // at this point, savedSchema is similar to ['components', 'schemas','Address'] // components is actually components and paths (an object with components + paths as 1st-level-props) refObj = _.get(components, savedSchema); diff --git a/test/unit/util.test.js b/test/unit/util.test.js index 7c11546b2..e2427899b 100644 --- a/test/unit/util.test.js +++ b/test/unit/util.test.js @@ -300,39 +300,23 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { expect(retVal).to.eql([]); }); - it('should not throw error if a parameter with invalid $ref is passed', function () { + it('should resolve ref outside components and paths', function () { var operationParam = [ - { name: 'limit', in: 'query', $ref: '#/invalid/ref/1' } + { name: 'limit', in: 'query', $ref: '#/valid/ref' } ], - pathParam = [ - { $ref: '#/components/schemas/searchParam' } - ], - componentsAndPaths = { - components: { - schemas: { - searchParam: { - name: 'search', - in: 'query', - schema: { - type: 'string' - } + spec = { + valid: { + ref: { + name: 'valid', + in: 'query', + schema: { + type: 'string' } } } }, - retVal = SchemaUtils.getRequestParams(operationParam, pathParam, componentsAndPaths); - expect(retVal).to.deep.equal([ - { - value: 'Error reading #/invalid/ref/1. Can only use references from components and paths' - }, - { - name: 'search', - in: 'query', - schema: { - type: 'string' - } - } - ]); + retVal = SchemaUtils.getRequestParams(operationParam, null, spec); + expect(retVal).to.eql(operationParam); }); });