Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When the parser is unable to resolve a path, give the user supplied path in the error. #75

Merged
merged 1 commit into from
Mar 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/pointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var $Ref = require('./ref'),
* @param {string} path
* @constructor
*/
function Pointer ($ref, path) {
function Pointer ($ref, path, originalPath) {
/**
* The {@link $Ref} object that contains this {@link Pointer} object.
* @type {$Ref}
Expand All @@ -31,6 +31,12 @@ function Pointer ($ref, path) {
*/
this.path = path;

/**
* The original path or URL, used for error messages.
* @type {string}
*/
this.originalPath = originalPath || path;

/**
* The value of the JSON pointer.
* Can be any JSON type, not just objects. Unknown file types are represented as Buffers (byte arrays).
Expand Down Expand Up @@ -77,7 +83,7 @@ Pointer.prototype.resolve = function (obj, options) {

var token = tokens[i];
if (this.value[token] === undefined) {
throw ono.syntax('Error resolving $ref pointer "%s". \nToken "%s" does not exist.', this.path, token);
throw ono.syntax('Error resolving $ref pointer "%s". \nToken "%s" does not exist.', this.originalPath, token);
}
else {
this.value = this.value[token];
Expand Down
5 changes: 3 additions & 2 deletions lib/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ $Ref.prototype.get = function (path, options) {
*
* @param {string} path - The full path being resolved, optionally with a JSON pointer in the hash
* @param {$RefParserOptions} options
* @param {string} [originalPath] - The original, unmodified path being resolved
* @returns {Pointer}
*/
$Ref.prototype.resolve = function (path, options) {
var pointer = new Pointer(this, path);
$Ref.prototype.resolve = function (path, options, originalPath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see anywhere where this method is called with the new parameter. Am I missing something? Or is the parameter always undefined? If the latter, then we can remove the parameter from here and from the Pointer constructor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. Not sure how I missed that

var pointer = new Pointer(this, path, originalPath);
return pointer.resolve(this.value, options);
};

Expand Down
12 changes: 6 additions & 6 deletions lib/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ $Refs.prototype.get = function (path, options) {
* @param {*} value - The value to assign
*/
$Refs.prototype.set = function (path, value) {
path = url.resolve(this._root$Ref.path, path);
var withoutHash = url.stripHash(path);
var absPath = url.resolve(this._root$Ref.path, path);
var withoutHash = url.stripHash(absPath);
var $ref = this._$refs[withoutHash];

if (!$ref) {
throw ono('Error resolving $ref pointer "%s". \n"%s" not found.', path, withoutHash);
}

$ref.set(path, value);
$ref.set(absPath, value);
};

/**
Expand Down Expand Up @@ -146,15 +146,15 @@ $Refs.prototype._add = function (path, value) {
* @protected
*/
$Refs.prototype._resolve = function (path, options) {
path = url.resolve(this._root$Ref.path, path);
var withoutHash = url.stripHash(path);
var absPath = url.resolve(this._root$Ref.path, path);
var withoutHash = url.stripHash(absPath);
var $ref = this._$refs[withoutHash];

if (!$ref) {
throw ono('Error resolving $ref pointer "%s". \n"%s" not found.', path, withoutHash);
}

return $ref.resolve(path, options);
return $ref.resolve(absPath, options, path);
};

/**
Expand Down
8 changes: 4 additions & 4 deletions test/specs/refs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ describe('$Refs object', function () {
.catch(function (err) {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal(
'Error resolving $ref pointer "' + encodeURI(path.abs('specs/external/definitions/name.yaml')) + '#/". ' +
'Error resolving $ref pointer "definitions/name.yaml#/". ' +
'\nToken "" does not exist.'
);
});
Expand Down Expand Up @@ -279,7 +279,7 @@ describe('$Refs object', function () {
.catch(function (err) {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal(
'Error resolving $ref pointer "' + encodeURI(path.abs('specs/external/foo-bar.yaml')) + '#/some/value". ' +
'Error resolving $ref pointer "foo-bar.yaml#/some/value". ' +
'\n"' + encodeURI(path.abs('specs/external/foo-bar.yaml')) + '" not found.'
);
});
Expand All @@ -294,7 +294,7 @@ describe('$Refs object', function () {
.catch(function (err) {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal(
'Error resolving $ref pointer "' + encodeURI(path.abs('specs/external/external.yaml')) + '#/foo/bar". ' +
'Error resolving $ref pointer "external.yaml#/foo/bar". ' +
'\nToken "foo" does not exist.'
);
});
Expand Down Expand Up @@ -339,7 +339,7 @@ describe('$Refs object', function () {
.catch(function (err) {
expect(err).to.be.an.instanceOf(Error);
expect(err.message).to.equal(
'Error resolving $ref pointer "' + encodeURI(path.abs('specs/external/foo-bar.yaml')) + '#/some/path". ' +
'Error resolving $ref pointer "foo-bar.yaml#/some/path". ' +
'\n"' + encodeURI(path.abs('specs/external/foo-bar.yaml')) + '" not found.'
);
});
Expand Down