Skip to content

Commit 4fac138

Browse files
committed
Fix issue circular ref not resolved if baseDoc is provided
1 parent 6d20222 commit 4fac138

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/specmap/lib/refs.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,13 @@ function pointerAlreadyInPath(pointer, basePath, parent, specmap) {
329329

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

338+
337339
// Case 2: indirect cycle
338340
// ex1: a.$ref: '/b' & b.c.$ref: '/b/c'
339341
// ex2: a.$ref: '/b/c' & b.c.$ref: '/b'

test/index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,43 @@ describe('constructor', () => {
9191
expect(apis.me.getMe).toBeA(Function)
9292
})
9393
})
94+
95+
it('should handle circular $refs when a baseDoc is provided', () => {
96+
// Given
97+
const spec = {
98+
swagger: '2.0',
99+
definitions: {
100+
node: {
101+
required: ['id', 'nodes'],
102+
type: 'object',
103+
properties: {
104+
id: {
105+
type: 'string'
106+
},
107+
nodes: {
108+
type: 'array',
109+
items: {
110+
$ref: '#/definitions/node'
111+
}
112+
}
113+
}
114+
}
115+
}
116+
}
117+
118+
// When
119+
return Swagger.resolve({
120+
spec,
121+
allowMetaPatches: false,
122+
baseDoc: 'http://example.com/swagger.json'
123+
}).then(handleResponse)
124+
125+
// Then
126+
function handleResponse(obj) {
127+
expect(obj.errors).toEqual([])
128+
expect(obj.spec).toEqual(spec)
129+
}
130+
})
94131
})
95132

96133
describe('#http', function () {

0 commit comments

Comments
 (0)