Skip to content

Commit 1522423

Browse files
The $Refs.paths() method now decodes URL-encoded characters in local file paths
1 parent 9320776 commit 1522423

6 files changed

Lines changed: 48 additions & 37 deletions

File tree

dist/ref-parser.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,9 @@ function read$RefFile($ref, options) {
610610

611611
$ref.type = 'fs';
612612
return new Promise(function(resolve, reject) {
613+
var file;
613614
try {
614-
var file = decodeURI($ref.path);
615+
file = decodeURI($ref.path);
615616
}
616617
catch (err) {
617618
reject(ono.uri(err, 'Malformed URI: %s', $ref.path));
@@ -1048,14 +1049,17 @@ function $Refs() {
10481049
$Refs.prototype.paths = function(types) {
10491050
var $refs = this._$refs;
10501051
var keys = Object.keys($refs);
1051-
10521052
types = _flatten(arguments);
1053-
if (types.length === 0) {
1054-
return keys;
1053+
1054+
if (types.length > 0) {
1055+
keys = keys.filter(function(key) {
1056+
return types.indexOf($refs[key].type) !== -1;
1057+
});
10551058
}
10561059

1057-
return keys.filter(function(key) {
1058-
return types.indexOf($refs[key].type) !== -1;
1060+
return keys.map(function(key) {
1061+
// Decode URL-encoded characters for local file paths
1062+
return $refs[key].type === 'fs' ? decodeURI(key) : key;
10591063
});
10601064
};
10611065

@@ -1067,13 +1071,10 @@ $Refs.prototype.paths = function(types) {
10671071
*/
10681072
$Refs.prototype.values = function(types) {
10691073
var $refs = this._$refs;
1070-
var keys = Object.keys($refs);
1071-
types = _flatten(arguments);
1074+
var keys = this.paths(types);
10721075

10731076
return keys.reduce(function(obj, key) {
1074-
if (types.length === 0 || types.indexOf($refs[key].type) !== -1) {
1075-
obj[key] = $refs[key].value;
1076-
}
1077+
obj[key] = $refs[key].value;
10771078
}, {});
10781079
};
10791080

@@ -1347,7 +1348,7 @@ exports.debug = debug('json-schema-ref-parser');
13471348
*/
13481349
exports.cwd = function cwd() {
13491350
return process.browser ? location.href : process.cwd() + '/';
1350-
}
1351+
};
13511352

13521353
/**
13531354
* Determines whether the given path is a URL.
@@ -1357,7 +1358,7 @@ exports.cwd = function cwd() {
13571358
*/
13581359
exports.isUrl = function isUrl(path) {
13591360
return protocolPattern.test(path);
1360-
}
1361+
};
13611362

13621363
/**
13631364
* Returns the hash (URL fragment), if any, of the given path.
@@ -1371,7 +1372,7 @@ exports.getHash = function getHash(path) {
13711372
return path.substr(hashIndex);
13721373
}
13731374
return '';
1374-
}
1375+
};
13751376

13761377
/**
13771378
* Removes the hash (URL fragment), if any, from the given path.
@@ -1385,7 +1386,7 @@ exports.stripHash = function stripHash(path) {
13851386
path = path.substr(0, hashIndex);
13861387
}
13871388
return path;
1388-
}
1389+
};
13891390

13901391
/**
13911392
* Returns the file extension of the given path.
@@ -1399,7 +1400,7 @@ exports.extname = function extname(path) {
13991400
return path.substr(lastDot).toLowerCase();
14001401
}
14011402
return '';
1402-
}
1403+
};
14031404

14041405
/**
14051406
* Asynchronously invokes the given callback function with the given parameters.
@@ -1424,7 +1425,7 @@ exports.doCallback = function doCallback(callback, err, params) {
14241425
function invokeCallback() {
14251426
callback.apply(null, args);
14261427
}
1427-
}
1428+
};
14281429

14291430
}).call(this,require('_process'))
14301431

dist/ref-parser.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/refs.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ function $Refs() {
3030
$Refs.prototype.paths = function(types) {
3131
var $refs = this._$refs;
3232
var keys = Object.keys($refs);
33-
3433
types = _flatten(arguments);
35-
if (types.length === 0) {
36-
return keys;
34+
35+
if (types.length > 0) {
36+
keys = keys.filter(function(key) {
37+
return types.indexOf($refs[key].type) !== -1;
38+
});
3739
}
3840

39-
return keys.filter(function(key) {
40-
return types.indexOf($refs[key].type) !== -1;
41+
return keys.map(function(key) {
42+
// Decode URL-encoded characters for local file paths
43+
return $refs[key].type === 'fs' ? decodeURI(key) : key;
4144
});
4245
};
4346

@@ -49,13 +52,10 @@ $Refs.prototype.paths = function(types) {
4952
*/
5053
$Refs.prototype.values = function(types) {
5154
var $refs = this._$refs;
52-
var keys = Object.keys($refs);
53-
types = _flatten(arguments);
55+
var keys = this.paths(types);
5456

5557
return keys.reduce(function(obj, key) {
56-
if (types.length === 0 || types.indexOf($refs[key].type) !== -1) {
57-
obj[key] = $refs[key].value;
58-
}
58+
obj[key] = $refs[key].value;
5959
}, {});
6060
};
6161

tests/specs/dereference.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ describe('Dereferencing', function() {
99
expect(schema).to.be.an('object').and.not.empty;
1010
expect(schema).to.equal(parser.schema);
1111
expect(parser.$refs).to.be.an('object');
12+
13+
var all$Refs = parser.$refs.paths();
14+
expect(all$Refs).to.satisfy(arrayOfStrings);
15+
if (userAgent.isBrowser) {
16+
expect(parser.$refs.paths('http', 'https')).to.deep.equal(all$Refs);
17+
}
18+
else {
19+
expect(parser.$refs.paths('fs')).to.deep.equal(all$Refs);
20+
}
21+
1222
done();
1323
})
1424
.catch(done);

0 commit comments

Comments
 (0)