From d9580c0618da393754f0ce4a79bbfa751b164e2b Mon Sep 17 00:00:00 2001 From: Sky Rose Date: Wed, 19 Jul 2023 10:50:40 -0400 Subject: [PATCH 1/4] new test for resolving nested refs inside extended refs --- test/specs/nested-extended/dereferenced.js | 13 ++++++ .../nested-extended/nested-extended.spec.js | 42 +++++++++++++++++++ .../nested-extended/nested-extended.yaml | 5 +++ test/specs/nested-extended/parsed.js | 27 ++++++++++++ .../referenced-array-child.yaml | 1 + .../referenced-object-child.yaml | 1 + .../nested-extended/referenced-root.yaml | 1 + 7 files changed, 90 insertions(+) create mode 100644 test/specs/nested-extended/dereferenced.js create mode 100644 test/specs/nested-extended/nested-extended.spec.js create mode 100644 test/specs/nested-extended/nested-extended.yaml create mode 100644 test/specs/nested-extended/parsed.js create mode 100644 test/specs/nested-extended/referenced-array-child.yaml create mode 100644 test/specs/nested-extended/referenced-object-child.yaml create mode 100644 test/specs/nested-extended/referenced-root.yaml diff --git a/test/specs/nested-extended/dereferenced.js b/test/specs/nested-extended/dereferenced.js new file mode 100644 index 00000000..94bb8080 --- /dev/null +++ b/test/specs/nested-extended/dereferenced.js @@ -0,0 +1,13 @@ +"use strict"; + +module.exports = { + const: "root", + objectchild: { + const: "object-child", + }, + arraychild: [ + { + const: "array-child", + }, + ], +}; diff --git a/test/specs/nested-extended/nested-extended.spec.js b/test/specs/nested-extended/nested-extended.spec.js new file mode 100644 index 00000000..f6ad1fcd --- /dev/null +++ b/test/specs/nested-extended/nested-extended.spec.js @@ -0,0 +1,42 @@ +"use strict"; + +const chai = require("chai"); +const chaiSubset = require("chai-subset"); +chai.use(chaiSubset); +const { expect } = chai; +const $RefParser = require("../../../lib"); +const helper = require("../../utils/helper"); +const path = require("../../utils/path"); +const parsedSchema = require("./parsed"); +const dereferencedSchema = require("./dereferenced"); + +describe("Schema with a $ref nested inside an extended $ref", () => { + it("should parse successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.parse( + path.rel("specs/nested-extended/nested-extended.yaml") + ); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(parsedSchema.schema); + expect(parser.$refs.paths()).to.deep.equal([ + path.abs("specs/nested-extended/nested-extended.yaml"), + ]); + }); + + it("should resolve successfully", helper.testResolve( + path.rel("specs/nested-extended/nested-extended.yaml"), + path.abs("specs/nested-extended/nested-extended.yaml"), parsedSchema.schema, + path.abs("specs/nested-extended/referenced-root.yaml"), parsedSchema.root, + path.abs("specs/nested-extended/referenced-object-child.yaml"), parsedSchema.objectchild, + path.abs("specs/nested-extended/referenced-array-child.yaml"), parsedSchema.arraychild + )); + + it("should dereference successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.dereference( + path.rel("specs/nested-extended/nested-extended.yaml") + ); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(dereferencedSchema); + }); +}); diff --git a/test/specs/nested-extended/nested-extended.yaml b/test/specs/nested-extended/nested-extended.yaml new file mode 100644 index 00000000..b044c6e8 --- /dev/null +++ b/test/specs/nested-extended/nested-extended.yaml @@ -0,0 +1,5 @@ +$ref: "referenced-root.yaml" +objectchild: + $ref: "referenced-object-child.yaml" +arraychild: + - $ref: "referenced-array-child.yaml" diff --git a/test/specs/nested-extended/parsed.js b/test/specs/nested-extended/parsed.js new file mode 100644 index 00000000..2e17839c --- /dev/null +++ b/test/specs/nested-extended/parsed.js @@ -0,0 +1,27 @@ +"use strict"; + +module.exports = { + schema: { + $ref: "referenced-root.yaml", + objectchild: { + $ref: "referenced-object-child.yaml", + }, + arraychild: [ + { + $ref: "referenced-array-child.yaml", + }, + ], + }, + + root: { + const: "root", + }, + + objectchild: { + const: "object-child", + }, + + arraychild: { + const: "array-child", + }, +}; diff --git a/test/specs/nested-extended/referenced-array-child.yaml b/test/specs/nested-extended/referenced-array-child.yaml new file mode 100644 index 00000000..cc5fa40a --- /dev/null +++ b/test/specs/nested-extended/referenced-array-child.yaml @@ -0,0 +1 @@ +const: "array-child" diff --git a/test/specs/nested-extended/referenced-object-child.yaml b/test/specs/nested-extended/referenced-object-child.yaml new file mode 100644 index 00000000..cb781445 --- /dev/null +++ b/test/specs/nested-extended/referenced-object-child.yaml @@ -0,0 +1 @@ +const: "object-child" diff --git a/test/specs/nested-extended/referenced-root.yaml b/test/specs/nested-extended/referenced-root.yaml new file mode 100644 index 00000000..692562b0 --- /dev/null +++ b/test/specs/nested-extended/referenced-root.yaml @@ -0,0 +1 @@ +const: "root" From 1dccb6b2008bcd121c4c9b616f66b23a2cf6351f Mon Sep 17 00:00:00 2001 From: Sky Rose Date: Wed, 19 Jul 2023 14:30:30 -0400 Subject: [PATCH 2/4] fix: recursively resolve and dereference in extended refs --- lib/bundle/index.js | 9 +-------- lib/resolve-external.js | 16 ++++------------ 2 files changed, 5 insertions(+), 20 deletions(-) diff --git a/lib/bundle/index.js b/lib/bundle/index.js index 4de03408..39cc2c9a 100644 --- a/lib/bundle/index.js +++ b/lib/bundle/index.js @@ -78,14 +78,7 @@ function crawl (parent, key, path, pathFromRoot, indirections, inventory, $refs, for (let key of keys) { let keyPath = Pointer.join(path, key); let keyPathFromRoot = Pointer.join(pathFromRoot, key); - let value = obj[key]; - - if ($Ref.isAllowed$Ref(value)) { - inventory$Ref(obj, key, path, keyPathFromRoot, indirections, inventory, $refs, options, customRoots); - } - else { - crawl(obj, key, keyPath, keyPathFromRoot, indirections, inventory, $refs, options, customRoots); - } + crawl(obj, key, keyPath, keyPathFromRoot, indirections, inventory, $refs, options, customRoots); } } } diff --git a/lib/resolve-external.js b/lib/resolve-external.js index c7238fbd..d923d6a3 100644 --- a/lib/resolve-external.js +++ b/lib/resolve-external.js @@ -58,18 +58,10 @@ function crawl (obj, path, $refs, options) { if ($Ref.isExternal$Ref(obj)) { promises.push(resolve$Ref(obj, path, $refs, options)); } - else { - for (let key of Object.keys(obj)) { - let keyPath = Pointer.join(path, key); - let value = obj[key]; - - if ($Ref.isExternal$Ref(value)) { - promises.push(resolve$Ref(value, keyPath, $refs, options)); - } - else { - promises = promises.concat(crawl(value, keyPath, $refs, options)); - } - } + for (let key of Object.keys(obj)) { + let keyPath = Pointer.join(path, key); + let value = obj[key]; + promises = promises.concat(crawl(value, keyPath, $refs, options)); } } From f545b65f049b90c596339f13aa9d9ce8ed665733 Mon Sep 17 00:00:00 2001 From: Sky Rose Date: Wed, 19 Jul 2023 16:23:16 -0400 Subject: [PATCH 3/4] test: failing test for local dereference in nested-extended --- test/specs/nested-extended/dereferenced.js | 3 +++ test/specs/nested-extended/nested-extended.yaml | 5 +++++ test/specs/nested-extended/parsed.js | 8 ++++++++ 3 files changed, 16 insertions(+) diff --git a/test/specs/nested-extended/dereferenced.js b/test/specs/nested-extended/dereferenced.js index 94bb8080..2b0fb181 100644 --- a/test/specs/nested-extended/dereferenced.js +++ b/test/specs/nested-extended/dereferenced.js @@ -10,4 +10,7 @@ module.exports = { const: "array-child", }, ], + local: { + const: "local", + } }; diff --git a/test/specs/nested-extended/nested-extended.yaml b/test/specs/nested-extended/nested-extended.yaml index b044c6e8..55a545d1 100644 --- a/test/specs/nested-extended/nested-extended.yaml +++ b/test/specs/nested-extended/nested-extended.yaml @@ -3,3 +3,8 @@ objectchild: $ref: "referenced-object-child.yaml" arraychild: - $ref: "referenced-array-child.yaml" +local: + $ref: "#/$defs/local" +$defs: + local: + const: "local" diff --git a/test/specs/nested-extended/parsed.js b/test/specs/nested-extended/parsed.js index 2e17839c..73f4a6b6 100644 --- a/test/specs/nested-extended/parsed.js +++ b/test/specs/nested-extended/parsed.js @@ -11,6 +11,14 @@ module.exports = { $ref: "referenced-array-child.yaml", }, ], + local: { + $ref: "#/$defs/local", + }, + $defs: { + local: { + const: "local", + }, + }, }, root: { From e432e6c344bca370d3ecf002ad7e90173bd7c4fd Mon Sep 17 00:00:00 2001 From: Sky Rose Date: Wed, 19 Jul 2023 16:33:36 -0400 Subject: [PATCH 4/4] test: failing test for nested-extended bundle --- test/specs/nested-extended/nested-extended.spec.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/specs/nested-extended/nested-extended.spec.js b/test/specs/nested-extended/nested-extended.spec.js index f6ad1fcd..cfe4038c 100644 --- a/test/specs/nested-extended/nested-extended.spec.js +++ b/test/specs/nested-extended/nested-extended.spec.js @@ -39,4 +39,13 @@ describe("Schema with a $ref nested inside an extended $ref", () => { expect(schema).to.equal(parser.schema); expect(schema).to.deep.equal(dereferencedSchema); }); + + it("should bundle successfully", async () => { + let parser = new $RefParser(); + const schema = await parser.bundle( + path.rel("specs/nested-extended/nested-extended.yaml") + ); + expect(schema).to.equal(parser.schema); + expect(schema).to.deep.equal(dereferencedSchema); + }); });