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

fix: recursively resolve and dereference extended refs #45

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 1 addition & 8 deletions lib/bundle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/resolve-external.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ function crawl (obj, path, $refs, options, external, seen) {
}
obj.$ref = withoutHash + obj.$ref;
}
}

for (let key of Object.keys(obj)) {
let keyPath = Pointer.join(path, key);
let value = obj[key];
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, external, seen)
);
}
promises = promises.concat(
crawl(value, keyPath, $refs, options, external, seen)
);
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/specs/nested-extended/dereferenced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";

module.exports = {
const: "root",
objectchild: {
const: "object-child",
},
arraychild: [
{
const: "array-child",
},
],
local: {
const: "local",
}
};
51 changes: 51 additions & 0 deletions test/specs/nested-extended/nested-extended.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"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);
});

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);
});
});
10 changes: 10 additions & 0 deletions test/specs/nested-extended/nested-extended.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$ref: "referenced-root.yaml"
objectchild:
$ref: "referenced-object-child.yaml"
arraychild:
- $ref: "referenced-array-child.yaml"
local:
$ref: "#/$defs/local"
$defs:
local:
const: "local"
35 changes: 35 additions & 0 deletions test/specs/nested-extended/parsed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use strict";

module.exports = {
schema: {
$ref: "referenced-root.yaml",
objectchild: {
$ref: "referenced-object-child.yaml",
},
arraychild: [
{
$ref: "referenced-array-child.yaml",
},
],
local: {
$ref: "#/$defs/local",
},
$defs: {
local: {
const: "local",
},
},
},

root: {
const: "root",
},

objectchild: {
const: "object-child",
},

arraychild: {
const: "array-child",
},
};
1 change: 1 addition & 0 deletions test/specs/nested-extended/referenced-array-child.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const: "array-child"
1 change: 1 addition & 0 deletions test/specs/nested-extended/referenced-object-child.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const: "object-child"
1 change: 1 addition & 0 deletions test/specs/nested-extended/referenced-root.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const: "root"