Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/schemaUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
38 changes: 11 additions & 27 deletions test/unit/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down