Skip to content

Commit

Permalink
First try to fix whitlockjc#80
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego de Estrada committed Sep 12, 2016
1 parent 5ad1f63 commit 7114e95
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 13 deletions.
24 changes: 13 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 = ['#'];
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand All @@ -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
};
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
36 changes: 36 additions & 0 deletions test/browser/documents/nested-refs/a.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions test/browser/documents/nested-refs/b.yaml
Original file line number Diff line number Diff line change
@@ -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"
39 changes: 39 additions & 0 deletions test/test-issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

0 comments on commit 7114e95

Please sign in to comment.