Skip to content

Commit

Permalink
Merge pull request #1031 from buunguyen/fix-cyclic-more
Browse files Browse the repository at this point in the history
Fix issue circular ref not resolved if baseDoc is provided
  • Loading branch information
shockey authored Apr 28, 2017
2 parents 6d20222 + 4fac138 commit 3d1deca
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/specmap/lib/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,13 @@ function pointerAlreadyInPath(pointer, basePath, parent, specmap) {

// Case 1: direct cycle, e.g. a.b.c.$ref: '/a.b'
// Detect by checking that the parent path doesn't start with pointer.
// This only applies if the pointer is purely internal.
if (basePath == null && pointerIsAParent(parentPointer, pointer)) {
// This only applies if the pointer is internal, i.e. basePath === rootPath (could be null)
const rootDoc = specmap.contextTree.get([]).baseDoc
if (basePath === rootDoc && pointerIsAParent(parentPointer, pointer)) {
return true
}


// Case 2: indirect cycle
// ex1: a.$ref: '/b' & b.c.$ref: '/b/c'
// ex2: a.$ref: '/b/c' & b.c.$ref: '/b'
Expand Down
37 changes: 37 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,43 @@ describe('constructor', () => {
expect(apis.me.getMe).toBeA(Function)
})
})

it('should handle circular $refs when a baseDoc is provided', () => {
// Given
const spec = {
swagger: '2.0',
definitions: {
node: {
required: ['id', 'nodes'],
type: 'object',
properties: {
id: {
type: 'string'
},
nodes: {
type: 'array',
items: {
$ref: '#/definitions/node'
}
}
}
}
}
}

// When
return Swagger.resolve({
spec,
allowMetaPatches: false,
baseDoc: 'http://example.com/swagger.json'
}).then(handleResponse)

// Then
function handleResponse(obj) {
expect(obj.errors).toEqual([])
expect(obj.spec).toEqual(spec)
}
})
})

describe('#http', function () {
Expand Down

0 comments on commit 3d1deca

Please sign in to comment.