Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cyclic ref problem when baseDoc is specified #1038

Merged
merged 2 commits into from
May 2, 2017
Merged
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
4 changes: 3 additions & 1 deletion src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export default function resolve({http, fetch, spec, url, baseDoc, mode, allowMet
return doResolve(spec)

function doResolve(_spec) {
plugins.refs.docCache[baseDoc] = _spec
if (baseDoc) {
plugins.refs.docCache[baseDoc] = _spec
}

// Build a json-fetcher ( ie: give it a URL and get json out )
plugins.refs.fetchJSON = makeFetchJSON(http)
Expand Down
41 changes: 32 additions & 9 deletions src/specmap/lib/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,20 @@ const plugin = {
}
}
else {
promOrVal = extractFromDoc(basePath, pointer).catch((e) => {
throw wrapError(e, {
pointer,
$ref: ref,
baseDoc,
fullPath,
promOrVal = extractFromDoc(basePath, pointer)
if (promOrVal.__value != null) {
promOrVal = promOrVal.__value
}
else {
promOrVal = promOrVal.catch((e) => {
throw wrapError(e, {
pointer,
$ref: ref,
baseDoc,
fullPath,
})
})
})
}
}

if (promOrVal instanceof Error) {
Expand Down Expand Up @@ -178,7 +184,24 @@ function split(ref) {
* @api public
*/
function extractFromDoc(docPath, pointer) {
return getDoc(docPath).then(doc => extract(pointer, doc))
const doc = docCache[docPath]
if (doc && !lib.isPromise(doc)) {
// If doc is already available, return __value together with the promise.
// __value is for special handling in cycle check:
// pointerAlreadyInPath() won't work if patch.value is a promise,
// thus when that promise is finally resolved, cycle might happen (because
// `spec` and `docCache[basePath]` refer to the exact same object).
// See test "should resolve a cyclic spec when baseDoc is specified".
try {
const v = extract(pointer, doc)
return Object.assign(Promise.resolve(v), {__value: v})
}
catch (e) {
return Promise.reject(e)
}
}

return getDoc(docPath).then(_doc => extract(pointer, _doc))
}

/**
Expand Down Expand Up @@ -331,7 +354,7 @@ function pointerAlreadyInPath(pointer, basePath, parent, specmap) {
// Detect by checking that the parent path doesn't start with 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)) {
if (basePath == rootDoc && pointerIsAParent(parentPointer, pointer)) { // eslint-disable-line
return true
}

Expand Down
32 changes: 32 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,38 @@ describe('constructor', () => {
})
})

it('should resolve a cyclic spec when baseDoc is specified', function (cb) {
const spec = {
paths: {
post: {
parameters: [
{
$ref: '#/definitions/list',
}
]
}
},
definitions: {
item: {
items: {
$ref: '#/definitions/item'
}
},
list: {
items: {
$ref: '#/definitions/item'
}
}
}
}

Swagger.resolve({spec, baseDoc: 'http://whatever/'}).then((swag) => {
expect(swag.errors).toEqual([])
cb()
})
})


it('should keep resolve errors in #errors', function () {
// Given
const spec = {
Expand Down