From 234ecd61f18c686eb1c7fb0d310f96d8959dd718 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Thu, 23 Sep 2021 12:50:17 +0200 Subject: [PATCH 1/4] Added test to confirm problem --- test/specs/root-internal/bundled.js | 19 +++++++++ test/specs/root-internal/dereferenced.js | 19 +++++++++ test/specs/root-internal/parsed.js | 26 ++++++++++++ .../specs/root-internal/root-internal.spec.js | 42 +++++++++++++++++++ test/specs/root-internal/root-internal.yaml | 13 ++++++ 5 files changed, 119 insertions(+) create mode 100644 test/specs/root-internal/bundled.js create mode 100644 test/specs/root-internal/dereferenced.js create mode 100644 test/specs/root-internal/parsed.js create mode 100644 test/specs/root-internal/root-internal.spec.js create mode 100644 test/specs/root-internal/root-internal.yaml diff --git a/test/specs/root-internal/bundled.js b/test/specs/root-internal/bundled.js new file mode 100644 index 00000000..2f6677ed --- /dev/null +++ b/test/specs/root-internal/bundled.js @@ -0,0 +1,19 @@ +"use strict"; + +module.exports = +{ + title: "name", + required: [ + "first", + "last" + ], + type: "object", + properties: { + last: { + type: "string" + }, + first: { + type: "string" + } + }, +}; diff --git a/test/specs/root-internal/dereferenced.js b/test/specs/root-internal/dereferenced.js new file mode 100644 index 00000000..2f6677ed --- /dev/null +++ b/test/specs/root-internal/dereferenced.js @@ -0,0 +1,19 @@ +"use strict"; + +module.exports = +{ + title: "name", + required: [ + "first", + "last" + ], + type: "object", + properties: { + last: { + type: "string" + }, + first: { + type: "string" + } + }, +}; diff --git a/test/specs/root-internal/parsed.js b/test/specs/root-internal/parsed.js new file mode 100644 index 00000000..71e197a7 --- /dev/null +++ b/test/specs/root-internal/parsed.js @@ -0,0 +1,26 @@ +"use strict"; + +module.exports = +{ + schema: { + definitions: { + name: { + title: "name", + required: [ + "first", + "last" + ], + type: "object", + properties: { + last: { + $ref: "#/definitions/name/properties/first" + }, + first: { + type: "string" + } + }, + } + }, + $ref: "#/definitions/name" + } +}; diff --git a/test/specs/root-internal/root-internal.spec.js b/test/specs/root-internal/root-internal.spec.js new file mode 100644 index 00000000..a3ce9888 --- /dev/null +++ b/test/specs/root-internal/root-internal.spec.js @@ -0,0 +1,42 @@ +"use strict"; + +const { expect } = require("chai"); +const $RefParser = require("../../.."); +const helper = require("../../utils/helper"); +const path = require("../../utils/path"); +const parsedSchema = require("./parsed"); +const dereferencedSchema = require("./dereferenced"); +const bundledSchema = require("./bundled"); + +describe("Schema with a top-level internal (root) $ref", () => { + it("should parse successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.parse(path.rel("specs/root-internal/root-internal.yaml")); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(parsedSchema.schema); + expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/root-internal/root-internal.yaml")]); + }); + + it("should resolve successfully", helper.testResolve( + path.rel("specs/root-internal/root-internal.yaml"), + path.abs("specs/root-internal/root-internal.yaml"), parsedSchema.schema + )); + + it("should dereference successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.dereference(path.rel("specs/root-internal/root-internal.yaml")); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(dereferencedSchema); + // Reference equality + expect(schema.properties.first).to.equal(schema.properties.last); + // The "circular" flag should NOT be set + expect(parser.$refs.circular).to.equal(false); + }); + + it("should bundle successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.bundle(path.rel("specs/root-internal/root-internal.yaml")); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(bundledSchema); + }); +}); diff --git a/test/specs/root-internal/root-internal.yaml b/test/specs/root-internal/root-internal.yaml new file mode 100644 index 00000000..b654e9e2 --- /dev/null +++ b/test/specs/root-internal/root-internal.yaml @@ -0,0 +1,13 @@ +definitions: + name: + title: name + type: object + required: + - first + - last + properties: + first: + type: string + last: + $ref: "#/definitions/name/properties/first" +$ref: "#/definitions/name" \ No newline at end of file From 2ddeb9798ef8d262b691c3c7eebc098a2a6d8385 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Thu, 23 Sep 2021 18:44:54 +0200 Subject: [PATCH 2/4] Fixed initial check --- lib/dereference.js | 1 - lib/pointer.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/dereference.js b/lib/dereference.js index 0a4c919a..4d0b6ce7 100644 --- a/lib/dereference.js +++ b/lib/dereference.js @@ -129,7 +129,6 @@ function dereference$Ref ($ref, path, pathFromRoot, parents, processedObjects, d return cache; } - let pointer = $refs._resolve($refPath, path, options); if (pointer === null) { diff --git a/lib/pointer.js b/lib/pointer.js index 43c6e072..1265f088 100644 --- a/lib/pointer.js +++ b/lib/pointer.js @@ -84,7 +84,7 @@ Pointer.prototype.resolve = function (obj, options, pathFromRoot) { this.path = Pointer.join(this.path, tokens.slice(i)); } - if (typeof this.value === "object" && this.value !== null && "$ref" in this.value) { + if (typeof this.value === "object" && this.value !== null && "$ref" in this.value && this.value.$ref === "#") { return this; } From dbe964c29b615a043f29a408cbe8ba81d6412852 Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Thu, 23 Sep 2021 18:47:08 +0200 Subject: [PATCH 3/4] Fixed expected result --- test/specs/root-internal/bundled.js | 18 ++++++++++++++++++ test/specs/root-internal/dereferenced.js | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/test/specs/root-internal/bundled.js b/test/specs/root-internal/bundled.js index 2f6677ed..c39f781f 100644 --- a/test/specs/root-internal/bundled.js +++ b/test/specs/root-internal/bundled.js @@ -2,6 +2,24 @@ module.exports = { + definitions: { + name: { + title: "name", + required: [ + "first", + "last" + ], + type: "object", + properties: { + last: { + type: "string" + }, + first: { + type: "string" + } + }, + } + }, title: "name", required: [ "first", diff --git a/test/specs/root-internal/dereferenced.js b/test/specs/root-internal/dereferenced.js index 2f6677ed..c39f781f 100644 --- a/test/specs/root-internal/dereferenced.js +++ b/test/specs/root-internal/dereferenced.js @@ -2,6 +2,24 @@ module.exports = { + definitions: { + name: { + title: "name", + required: [ + "first", + "last" + ], + type: "object", + properties: { + last: { + type: "string" + }, + first: { + type: "string" + } + }, + } + }, title: "name", required: [ "first", From e4750910528be2a999c6d9f32a0c397d4fd547ce Mon Sep 17 00:00:00 2001 From: Jonas Lagoni Date: Mon, 4 Oct 2021 20:45:41 +0200 Subject: [PATCH 4/4] Removed unnecessary check --- lib/pointer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pointer.js b/lib/pointer.js index 1265f088..43c6e072 100644 --- a/lib/pointer.js +++ b/lib/pointer.js @@ -84,7 +84,7 @@ Pointer.prototype.resolve = function (obj, options, pathFromRoot) { this.path = Pointer.join(this.path, tokens.slice(i)); } - if (typeof this.value === "object" && this.value !== null && "$ref" in this.value && this.value.$ref === "#") { + if (typeof this.value === "object" && this.value !== null && "$ref" in this.value) { return this; }