Skip to content

Commit 3013b32

Browse files
Added support for paths with spaces and/or special characters
1 parent 5011800 commit 3013b32

7 files changed

Lines changed: 41 additions & 35 deletions

File tree

dist/ref-parser.js

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

611611
$ref.type = 'fs';
612612
return new Promise(function(resolve, reject) {
613-
util.debug('Opening file: %s', $ref.path);
613+
try {
614+
var file = decodeURI($ref.path);
615+
}
616+
catch (err) {
617+
reject(ono.uri(err, 'Malformed URI: %s', $ref.path));
618+
}
619+
620+
util.debug('Opening file: %s', file);
614621

615622
try {
616-
fs.readFile($ref.path, function(err, data) {
623+
fs.readFile(file, function(err, data) {
617624
if (err) {
618625
reject(ono(err, 'Error opening file "%s"', $ref.path));
619626
}
@@ -623,7 +630,7 @@ function read$RefFile($ref, options) {
623630
});
624631
}
625632
catch (err) {
626-
reject(ono(err, 'Error opening file "%s"', $ref.path));
633+
reject(ono(err, 'Error opening file "%s"', file));
627634
}
628635
});
629636
}
@@ -1326,27 +1333,19 @@ var debug = require('debug'),
13261333
_isFunction = require('lodash/lang/isFunction'),
13271334
protocolPattern = /^[a-z0-9.+-]+:\/\//i;
13281335

1329-
module.exports = {
1330-
/**
1331-
* Writes messages to stdout.
1332-
* Log messages are suppressed by default, but can be enabled by setting the DEBUG variable.
1333-
* @type {function}
1334-
*/
1335-
debug: debug('json-schema-ref-parser'),
1336-
cwd: cwd,
1337-
isUrl: isUrl,
1338-
getHash: getHash,
1339-
stripHash: stripHash,
1340-
extname: extname,
1341-
doCallback: doCallback
1342-
};
1336+
/**
1337+
* Writes messages to stdout.
1338+
* Log messages are suppressed by default, but can be enabled by setting the DEBUG variable.
1339+
* @type {function}
1340+
*/
1341+
exports.debug = debug('json-schema-ref-parser');
13431342

13441343
/**
13451344
* Returns the current working directory (in Node) or the current page URL (in browsers).
13461345
*
13471346
* @returns {string}
13481347
*/
1349-
function cwd() {
1348+
exports.cwd = function cwd() {
13501349
return process.browser ? location.href : process.cwd() + '/';
13511350
}
13521351

@@ -1356,7 +1355,7 @@ function cwd() {
13561355
* @param {string} path
13571356
* @returns {boolean}
13581357
*/
1359-
function isUrl(path) {
1358+
exports.isUrl = function isUrl(path) {
13601359
return protocolPattern.test(path);
13611360
}
13621361

@@ -1366,7 +1365,7 @@ function isUrl(path) {
13661365
* @param {string} path
13671366
* @returns {string}
13681367
*/
1369-
function getHash(path) {
1368+
exports.getHash = function getHash(path) {
13701369
var hashIndex = path.indexOf('#');
13711370
if (hashIndex >= 0) {
13721371
return path.substr(hashIndex);
@@ -1380,7 +1379,7 @@ function getHash(path) {
13801379
* @param {string} path
13811380
* @returns {string}
13821381
*/
1383-
function stripHash(path) {
1382+
exports.stripHash = function stripHash(path) {
13841383
var hashIndex = path.indexOf('#');
13851384
if (hashIndex >= 0) {
13861385
path = path.substr(0, hashIndex);
@@ -1394,7 +1393,7 @@ function stripHash(path) {
13941393
* @param {string} path
13951394
* @returns {string}
13961395
*/
1397-
function extname(path) {
1396+
exports.extname = function extname(path) {
13981397
var lastDot = path.lastIndexOf('.');
13991398
if (lastDot >= 0) {
14001399
return path.substr(lastDot).toLowerCase();
@@ -1409,7 +1408,7 @@ function extname(path) {
14091408
* @param {*} [err]
14101409
* @param {...*} [params]
14111410
*/
1412-
function doCallback(callback, err, params) {
1411+
exports.doCallback = function doCallback(callback, err, params) {
14131412
if (_isFunction(callback)) {
14141413
var args = Array.prototype.slice.call(arguments, 1);
14151414

dist/ref-parser.js.map

Lines changed: 3 additions & 3 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: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/read.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,17 @@ function read$RefFile($ref, options) {
102102

103103
$ref.type = 'fs';
104104
return new Promise(function(resolve, reject) {
105-
util.debug('Opening file: %s', $ref.path);
105+
try {
106+
var file = decodeURI($ref.path);
107+
}
108+
catch (err) {
109+
reject(ono.uri(err, 'Malformed URI: %s', $ref.path));
110+
}
111+
112+
util.debug('Opening file: %s', file);
106113

107114
try {
108-
fs.readFile($ref.path, function(err, data) {
115+
fs.readFile(file, function(err, data) {
109116
if (err) {
110117
reject(ono(err, 'Error opening file "%s"', $ref.path));
111118
}
@@ -115,7 +122,7 @@ function read$RefFile($ref, options) {
115122
});
116123
}
117124
catch (err) {
118-
reject(ono(err, 'Error opening file "%s"', $ref.path));
125+
reject(ono(err, 'Error opening file "%s"', file));
119126
}
120127
});
121128
}

tests/specs/dereference.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ describe('Dereferencing', function() {
44
it('should parse',
55
function(done) {
66
var parser = new $RefParser();
7-
parser.parse(helper.relPath('no-refs.yaml'))
7+
parser.parse(helper.relPath('no refs.yaml'))
88
.then(function(schema) {
99
expect(schema).to.be.an('object').and.not.empty;
1010
expect(schema).to.equal(parser.schema);

0 commit comments

Comments
 (0)