Skip to content

Commit

Permalink
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 fde6b19
Show file tree
Hide file tree
Showing 9 changed files with 2,244 additions and 2,000 deletions.
4 changes: 2 additions & 2 deletions browser/json-refs-min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions browser/json-refs-standalone-min.js

Large diffs are not rendered by default.

2,589 changes: 1,333 additions & 1,256 deletions browser/json-refs-standalone.js

Large diffs are not rendered by default.

1,533 changes: 804 additions & 729 deletions browser/json-refs.js

Large diffs are not rendered by default.

24 changes: 15 additions & 9 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,19 +190,21 @@ 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 = ['#'];

isRootFile = typeof isRootFile !== 'undefined' ? isRootFile : true;

parents.forEach(function (parent, index) {
if (parent.charAt(0) !== '#') {
ancestorPtrs.push(parentPtrs[index]);
Expand Down Expand Up @@ -275,7 +277,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 +299,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 +496,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,9 +834,11 @@ 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 = {};

isRootFile = typeof isRootFile !== 'undefined' ? isRootFile : true;

// Validate the provided document
if (!isType(obj, 'Array') && !isType(obj, 'Object')) {
throw new TypeError('obj must be an Array or an Object');
Expand Down Expand Up @@ -862,7 +868,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
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 fde6b19

Please sign in to comment.