From c888ec68e199f9ee29602c83f88ab7420ce33e43 Mon Sep 17 00:00:00 2001 From: Ed Vinyard Date: Tue, 30 May 2023 14:49:21 -0500 Subject: [PATCH 1/4] improve MissingPointerError message --- lib/pointer.js | 2 +- lib/util/errors.js | 6 +++--- package-lock.json | 4 ++-- test/specs/missing-pointers/missing-pointers.spec.js | 7 ++++--- test/specs/refs.spec.js | 4 ++-- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/pointer.js b/lib/pointer.js index ffef8cf3..ebf1486c 100644 --- a/lib/pointer.js +++ b/lib/pointer.js @@ -97,7 +97,7 @@ Pointer.prototype.resolve = function (obj, options, pathFromRoot) { let token = tokens[i]; if (this.value[token] === undefined || this.value[token] === null) { this.value = null; - throw new MissingPointerError(token, this.originalPath); + throw new MissingPointerError(this.path, pathFromRoot, token, this.originalPath); } else { this.value = this.value[token]; diff --git a/lib/util/errors.js b/lib/util/errors.js index de111a6d..80802f30 100644 --- a/lib/util/errors.js +++ b/lib/util/errors.js @@ -2,7 +2,7 @@ const { Ono } = require("@jsdevtools/ono"); -const { stripHash, toFileSystemPath } = require("./url"); +const { getHash, stripHash, toFileSystemPath } = require("./url"); const JSONParserError = exports.JSONParserError = class JSONParserError extends Error { constructor (message, source) { @@ -93,8 +93,8 @@ const UnmatchedResolverError = exports.UnmatchedResolverError = class UnmatchedR setErrorName(UnmatchedResolverError); const MissingPointerError = exports.MissingPointerError = class MissingPointerError extends JSONParserError { - constructor (token, path) { - super(`Token "${token}" does not exist.`, stripHash(path)); + constructor (unresolvableRefValue, pathToUnresolvableRef, unresolvableTokenInRef, path) { + super(`at "${getHash(pathToUnresolvableRef)}", token "${unresolvableTokenInRef}" in "${getHash(unresolvableRefValue)}" does not exist`, stripHash(path)); this.code = "EMISSINGPOINTER"; } diff --git a/package-lock.json b/package-lock.json index 10a47e61..9f037930 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@stoplight/json-schema-ref-parser", - "version": "9.2.2", + "version": "9.2.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@stoplight/json-schema-ref-parser", - "version": "9.2.2", + "version": "9.2.4", "license": "MIT", "dependencies": { "@jsdevtools/ono": "^7.1.3", diff --git a/test/specs/missing-pointers/missing-pointers.spec.js b/test/specs/missing-pointers/missing-pointers.spec.js index 3b35fdc6..92b0d829 100644 --- a/test/specs/missing-pointers/missing-pointers.spec.js +++ b/test/specs/missing-pointers/missing-pointers.spec.js @@ -16,7 +16,7 @@ describe("Schema with missing pointers", () => { } catch (err) { expect(err).to.be.an.instanceOf(MissingPointerError); - expect(err.message).to.contain("Token \"baz\" does not exist."); + expect(err.message).to.equal('at "#/foo", token "baz" in "#/baz" does not exist'); } }); @@ -31,12 +31,13 @@ describe("Schema with missing pointers", () => { expect(err.files).to.equal(parser); expect(err.files.$refs._root$Ref.value).to.deep.equal({ foo: null }); expect(err.message).to.have.string("1 error occurred while reading '"); + console.log(err.errors[0].source); + expect(err.errors).to.containSubset([ { name: MissingPointerError.name, - message: "Token \"baz\" does not exist.", + message: 'at "#/foo", token "baz" in "#/baz" does not exist', path: ["foo"], - source: message => message.endsWith("/test/") || message.startsWith("http://localhost"), } ]); } diff --git a/test/specs/refs.spec.js b/test/specs/refs.spec.js index be9bbdc9..43f7acac 100644 --- a/test/specs/refs.spec.js +++ b/test/specs/refs.spec.js @@ -216,7 +216,7 @@ describe("$Refs object", () => { } catch (err) { expect(err).to.be.an.instanceOf(Error); - expect(err.message).to.equal('Token "" does not exist.'); + expect(err.message).to.equal('at "#", token "" in "#/" does not exist'); } }); @@ -254,7 +254,7 @@ describe("$Refs object", () => { } catch (err) { expect(err).to.be.an.instanceOf(Error); - expect(err.message).to.equal('Token "foo" does not exist.'); + expect(err.message).to.equal('at "#", token "foo" in "#/foo/bar" does not exist'); } }); }); From 00f024a0fed5739c98697a5bda34c5fcd8a3d72f Mon Sep 17 00:00:00 2001 From: Ed Vinyard Date: Wed, 31 May 2023 09:23:46 -0500 Subject: [PATCH 2/4] fix a couple tests; try to get more info about remaining failing tests --- test/specs/invalid/invalid.spec.js | 6 ++++-- test/specs/usage/usage.spec.js | 11 +++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/test/specs/invalid/invalid.spec.js b/test/specs/invalid/invalid.spec.js index 1eb8b108..56bb8057 100644 --- a/test/specs/invalid/invalid.spec.js +++ b/test/specs/invalid/invalid.spec.js @@ -164,7 +164,8 @@ describe("Invalid syntax", () => { message.includes("invalid.json: JSON.parse: end of data while reading object contents") || // Firefox message.includes("invalid.json: JSON Parse error: Expected '}'") || // Safari message.includes("invalid.json: JSON.parse Error: Invalid character") || // Edge - message.includes("invalid.json: Syntax error") // IE + message.includes("invalid.json: Syntax error") || // IE + message.includes("Expected property name or \'}\' in JSON at position 2") ), path: [], source: message => message.endsWith("test/specs/invalid/invalid.json"), @@ -318,7 +319,8 @@ describe("Invalid syntax", () => { message.includes("invalid.json: JSON.parse: end of data while reading object contents") || // Firefox message.includes("invalid.json: JSON Parse error: Expected '}'") || // Safari message.includes("invalid.json: JSON.parse Error: Invalid character") || // Edge - message.includes("invalid.json: Syntax error") // IE + message.includes("invalid.json: Syntax error") || // IE + message.includes("Expected property name or \'}\' in JSON at position 2") // Chrome ), path: ["foo"], source: message => message.endsWith("/test/") || message.startsWith("http://localhost"), diff --git a/test/specs/usage/usage.spec.js b/test/specs/usage/usage.spec.js index efcde1b7..5ac990f2 100644 --- a/test/specs/usage/usage.spec.js +++ b/test/specs/usage/usage.spec.js @@ -67,9 +67,12 @@ describe("Usage", () => { } }); - expect(parser.$refs.propertyMap).to.deep.equal({ - "#/properties/bar": path.abs("/") + "#/properties/foo", - "#/properties/baz": path.abs("/") + "#/properties/foo/properties/id" - }); + expect(parser.$refs.propertyMap["#/properties/bar"]).to.equal(path.abs("/") + "#/properties/foo"); + expect(parser.$refs.propertyMap["#/properties/baz"]).to.equal(path.abs("/") + "#/properties/foo/properties/id"); + // const expected = { + // "#/properties/bar": path.abs("/") + "#/properties/foo", + // "#/properties/baz": path.abs("/") + "#/properties/foo/properties/id" + // }; + // expect(parser.$refs.propertyMap).to.deep.equal(expected); }); }); From aa05447c7628878717fa6ebe9ffcef692e2c3c78 Mon Sep 17 00:00:00 2001 From: Ed Vinyard Date: Wed, 31 May 2023 10:04:04 -0500 Subject: [PATCH 3/4] fix constructor param order (thanks Daniel!); revert experimental changes --- .vscode/tasks.json | 33 ++++++++++++++++++++++----------- lib/index.d.ts | 2 +- lib/pointer.js | 2 +- lib/util/errors.js | 2 +- test/specs/usage/usage.spec.js | 11 ++++------- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index afc5ce12..16e0a987 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -7,23 +7,34 @@ // ${cwd}: the current working directory of the spawned process { - "version": "0.1.0", + "version": "2.0.0", "command": "npm", - "isShellCommand": true, - "suppressTaskName": true, "tasks": [ { - "taskName": "lint", - "args": ["run", "lint"], - "showOutput": "always", + "label": "lint", + "type": "shell", + "args": [ + "run", + "lint" + ], "problemMatcher": "$eslint-stylish", - "isBuildCommand": true + "group": { + "_id": "build", + "isDefault": false + } }, { - "taskName": "test", - "args": ["run", "mocha"], - "showOutput": "always", - "isTestCommand": true + "label": "test", + "type": "shell", + "args": [ + "run", + "mocha" + ], + "problemMatcher": [], + "group": { + "_id": "test", + "isDefault": false + } } ] } diff --git a/lib/index.d.ts b/lib/index.d.ts index a69a6933..eda46b83 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -490,7 +490,7 @@ declare namespace $RefParser { public readonly code ="EUNMATCHEDRESOLVER"; } export class MissingPointerError extends JSONParserError { - public constructor(token: string | number, source: string); + public constructor(token: string | number, source: string, unresolvableRefValue?: string, pathToUnresolvableRef?: string); public readonly name = "MissingPointerError"; public readonly code ="EMISSINGPOINTER"; diff --git a/lib/pointer.js b/lib/pointer.js index ebf1486c..3bcbb33b 100644 --- a/lib/pointer.js +++ b/lib/pointer.js @@ -97,7 +97,7 @@ Pointer.prototype.resolve = function (obj, options, pathFromRoot) { let token = tokens[i]; if (this.value[token] === undefined || this.value[token] === null) { this.value = null; - throw new MissingPointerError(this.path, pathFromRoot, token, this.originalPath); + throw new MissingPointerError(token, this.originalPath, this.path, pathFromRoot); } else { this.value = this.value[token]; diff --git a/lib/util/errors.js b/lib/util/errors.js index 80802f30..80e13ed9 100644 --- a/lib/util/errors.js +++ b/lib/util/errors.js @@ -93,7 +93,7 @@ const UnmatchedResolverError = exports.UnmatchedResolverError = class UnmatchedR setErrorName(UnmatchedResolverError); const MissingPointerError = exports.MissingPointerError = class MissingPointerError extends JSONParserError { - constructor (unresolvableRefValue, pathToUnresolvableRef, unresolvableTokenInRef, path) { + constructor (unresolvableTokenInRef, path, unresolvableRefValue, pathToUnresolvableRef) { super(`at "${getHash(pathToUnresolvableRef)}", token "${unresolvableTokenInRef}" in "${getHash(unresolvableRefValue)}" does not exist`, stripHash(path)); this.code = "EMISSINGPOINTER"; diff --git a/test/specs/usage/usage.spec.js b/test/specs/usage/usage.spec.js index 5ac990f2..efcde1b7 100644 --- a/test/specs/usage/usage.spec.js +++ b/test/specs/usage/usage.spec.js @@ -67,12 +67,9 @@ describe("Usage", () => { } }); - expect(parser.$refs.propertyMap["#/properties/bar"]).to.equal(path.abs("/") + "#/properties/foo"); - expect(parser.$refs.propertyMap["#/properties/baz"]).to.equal(path.abs("/") + "#/properties/foo/properties/id"); - // const expected = { - // "#/properties/bar": path.abs("/") + "#/properties/foo", - // "#/properties/baz": path.abs("/") + "#/properties/foo/properties/id" - // }; - // expect(parser.$refs.propertyMap).to.deep.equal(expected); + expect(parser.$refs.propertyMap).to.deep.equal({ + "#/properties/bar": path.abs("/") + "#/properties/foo", + "#/properties/baz": path.abs("/") + "#/properties/foo/properties/id" + }); }); }); From 70e087e9df354c471a4fccf90b68bcb9c2306063 Mon Sep 17 00:00:00 2001 From: Ed Vinyard Date: Wed, 31 May 2023 11:53:49 -0500 Subject: [PATCH 4/4] release v9.2.5 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9f037930..c4d7c01c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@stoplight/json-schema-ref-parser", - "version": "9.2.4", + "version": "9.2.5", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index b0b67fe7..eb4805e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@stoplight/json-schema-ref-parser", - "version": "9.2.4", + "version": "9.2.5", "description": "Parse, Resolve, and Dereference JSON Schema $ref pointers", "keywords": [ "json",