From 7114e95b8ce26d5e02b296bcd69bca7af553a662 Mon Sep 17 00:00:00 2001 From: Diego de Estrada Date: Fri, 9 Sep 2016 14:01:08 -0300 Subject: [PATCH] First try to fix #80 --- index.js | 24 +++++++------- package.json | 4 +-- test/browser/documents/nested-refs/a.yaml | 36 +++++++++++++++++++++ test/browser/documents/nested-refs/b.yaml | 11 +++++++ test/test-issues.js | 39 +++++++++++++++++++++++ 5 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 test/browser/documents/nested-refs/a.yaml create mode 100644 test/browser/documents/nested-refs/b.yaml diff --git a/index.js b/index.js index b20d3ba..777d034 100644 --- a/index.js +++ b/index.js @@ -150,7 +150,7 @@ function findAncestors (obj, path) { return ancestors; } -function processSubDocument (mode, doc, subDocPath, refDetails, options, parents, parentPtrs, allRefs, indirect) { +function processSubDocument (mode, doc, subDocPath, refDetails, options, parents, parentPtrs, allRefs, indirect, isRootFile) { var refValue; var rOptions; @@ -190,15 +190,15 @@ function processSubDocument (mode, doc, subDocPath, refDetails, options, parents } } - return findRefsRecursive(doc, rOptions, parents, parentPtrs, allRefs, indirect); + return findRefsRecursive(doc, rOptions, parents, parentPtrs, allRefs, indirect, isRootFile); } } // Should this be its own exported API? -function findRefsRecursive (obj, options, parents, parentPtrs, allRefs, indirect) { +function findRefsRecursive (obj, options, parents, parentPtrs, allRefs, indirect, isRootFile) { var allTasks = Promise.resolve(); var parentPath = parentPtrs.length ? pathFromPtr(parentPtrs[parentPtrs.length - 1]) : []; - var refs = findRefs(obj, options); + var refs = findRefs(obj, options, isRootFile); var subDocPath = options.subDocPath || []; var subDocPtr = pathToPtr(subDocPath); var ancestorPtrs = ['#']; @@ -275,7 +275,8 @@ function findRefsRecursive (obj, options, parents, parentPtrs, allRefs, indirect parents.concat(location), parentPtrs.concat(refFullPtr), allRefs, - indirect); + indirect, + false); }) .catch(function (err) { refDetails.error = err.message; @@ -296,7 +297,8 @@ function findRefsRecursive (obj, options, parents, parentPtrs, allRefs, indirect parents.concat(location), parentPtrs.concat(refFullPtr), allRefs, - indirect || (location.indexOf(subDocPtr + '/') === -1 && location !== subDocPtr)); + indirect || (location.indexOf(subDocPtr + '/') === -1 && location !== subDocPtr), + isRootFile); }); } } else { @@ -492,7 +494,7 @@ function makeRefFilter (options) { if (isType(options.filter, 'Array') || isType(options.filter, 'String')) { validTypes = isType(options.filter, 'String') ? [options.filter] : options.filter; - refFilter = function (refDetails) { + refFilter = function (refDetails, path) { // Check the exact type or for invalid URIs, check its original type return validTypes.indexOf(refDetails.type) > -1 || validTypes.indexOf(getRefType(refDetails)) > -1; }; @@ -830,7 +832,7 @@ function encodePath (path) { * // Finding all invalid references * var invalidRefs = JsonRefs.findRefs(obj, {filter: 'invalid', includeInvalid: true}); */ -function findRefs (obj, options) { +function findRefs (obj, options, isRootFile) { var refs = {}; // Validate the provided document @@ -862,7 +864,7 @@ function findRefs (obj, options) { refDetails = options.refPostProcessor(refDetails, path); } - if (options.filter(refDetails, path)) { + if (!isRootFile || options.filter(refDetails, path)) { refs[pathToPtr(path)] = refDetails; } @@ -939,7 +941,7 @@ function findRefsAt (location, options) { cOptions.includeInvalid = true; - remoteCache[location].refs = findRefs(res, cOptions); + remoteCache[location].refs = findRefs(res, cOptions, true); } // Add the filter options back @@ -955,7 +957,7 @@ function findRefsAt (location, options) { // This will use the cache so don't worry about calling it twice return { - refs: findRefs(res, cOptions), + refs: findRefs(res, cOptions, true), value: res }; }); diff --git a/package.json b/package.json index 71404e0..24c45b6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "json-refs", - "version": "2.1.6", + "name": "@m/json-refs", + "version": "2.1.7", "description": "Various utilities for JSON References (http://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03).", "main": "index.js", "scripts": { diff --git a/test/browser/documents/nested-refs/a.yaml b/test/browser/documents/nested-refs/a.yaml new file mode 100644 index 0000000..836a37e --- /dev/null +++ b/test/browser/documents/nested-refs/a.yaml @@ -0,0 +1,36 @@ +--- +swagger: "2.0" +info: + title: test + version: '0.0.0' +responses: + 500: + description: "error" + schema: + $ref: "#/definitions/e" +paths: {} +definitions: + y: + type: object + properties: + children: + type: array + items: + $ref: "#/definitions/z" + z: + type: object + properties: + name: + type: string + t: + type: string + enum: + - unauthorized + - forbidden + e: + type: object + properties: + error_type: + $ref: "#/definitions/t" + message: + type: string diff --git a/test/browser/documents/nested-refs/b.yaml b/test/browser/documents/nested-refs/b.yaml new file mode 100644 index 0000000..b249825 --- /dev/null +++ b/test/browser/documents/nested-refs/b.yaml @@ -0,0 +1,11 @@ +--- +swagger: "2.0" +info: + title: test + version: '0.0.0' +responses: + $ref: "./a.yaml#/responses" +paths: {} +definitions: + x: + $ref: "./a.yaml#/definitions/y" diff --git a/test/test-issues.js b/test/test-issues.js index 2208f77..6053fd5 100644 --- a/test/test-issues.js +++ b/test/test-issues.js @@ -376,4 +376,43 @@ describe('json-refs Issues', function () { .then(done, done); }); }); + + describe('Issue #77', function () { + it('combined URI\'s should handle windows pathing correctly', function (done) { + JsonRefs.resolveRefsAt('../documents/test-document.yaml', { + loaderOptions: { + processContent: function (res, callback) { + callback(undefined, YAML.safeLoad(res.text)); + } + }, + relativeBase: relativeBase + }) + .then(function (results) { + assert.notEqual(results.refs['#/remote/relative'].missing, true); + }) + .then(done, done); + }); + }); + + describe('Issue #80', function () { + it('should resolve nested references correctly', function (done) { + JsonRefs.resolveRefsAt('nested-refs/b.yaml', { + filter: 'relative', + loaderOptions: { + processContent: function (res, callback) { + callback(undefined, YAML.safeLoad(res.text)); + } + }, + relativeBase: relativeBase + }) + .then(function (results) { + assert.ok(!_.has(results, 'resolved.definitions.x.properties.children.items.$ref')); + assert.equal(results.resolved.definitions.x.properties.children.items.type, 'object'); + + assert.ok(!_.has(results, 'resolved.responses.500.schema.properties.error_type.$ref')); + assert.equal(results.resolved.responses['500'].schema.properties.error_type.type, 'string'); + }) + .then(done, done); + }); + }); });