Skip to content

Tests for nested root references #223

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

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 9 additions & 11 deletions lib/resolve-external.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,15 @@ 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];

if ($Ref.isExternal$Ref(value)) {
promises.push(resolve$Ref(value, keyPath, $refs, options));
}
else {
promises = promises.concat(crawl(value, keyPath, $refs, options));
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/specs/external-root/bundled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

module.exports = {
external2: {
$ref: "definitions-other.yaml#/external2"
},
thing: {
type: "string"
}
};
3 changes: 3 additions & 0 deletions test/specs/external-root/definitions-other.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
external2:
thing:
type: string
3 changes: 3 additions & 0 deletions test/specs/external-root/definitions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
external1:
thing:
type: string
13 changes: 13 additions & 0 deletions test/specs/external-root/dereferenced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

module.exports =
{
thing: {
type: "string"
},
external2: {
thing: {
type: "string"
}
}
};
62 changes: 62 additions & 0 deletions test/specs/external-root/external-root.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";

const { expect } = require("chai");
const $RefParser = require("../../../lib");
const helper = require("../../utils/helper");
const path = require("../../utils/path");
const parsedSchema = require("./parsed");
const dereferencedSchema = require("./dereferenced");
const bundledSchema = require("./bundled");

/**
* This test is from PR #
* https://github.com/APIDevTools/json-schema-ref-parser/pull/62
*/
describe("Schema with external root refs", () => {
it("should parse successfully from an absolute path", async () => {
let parser = new $RefParser();
const schema = await parser.parse(path.abs("specs/external-root/external-root.yaml"));
expect(schema).to.equal(parser.schema);
expect(schema).to.deep.equal(parsedSchema.schema);
expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external-root/external-root.yaml")]);
});

it("should parse successfully from a relative path", async () => {
let parser = new $RefParser();
const schema = await parser.parse(path.rel("specs/external-root/external-root.yaml"));
expect(schema).to.equal(parser.schema);
expect(schema).to.deep.equal(parsedSchema.schema);
expect(parser.$refs.paths()).to.deep.equal([path.abs("specs/external-root/external-root.yaml")]);
});

it("should parse successfully from a url", async () => {
let parser = new $RefParser();
const schema = await parser.parse(path.url("specs/external-root/external-root.yaml"));
expect(schema).to.equal(parser.schema);
expect(schema).to.deep.equal(parsedSchema.schema);
expect(parser.$refs.paths()).to.deep.equal([path.url("specs/external-root/external-root.yaml")]);
});

it("should resolve successfully", helper.testResolve(
path.abs("specs/external-root/external-root.yaml"),
path.abs("specs/external-root/external-root.yaml"), parsedSchema.schema,
path.abs("specs/external-root/definitions.yaml"), parsedSchema.definitions,
path.abs("specs/external-root/definitions-other.yaml"), parsedSchema.definitionsOther
));

it("should dereference successfully", async () => {
let parser = new $RefParser();
const schema = await parser.dereference(path.rel("specs/external-root/external-root.yaml"));
expect(schema).to.equal(parser.schema);
expect(schema).to.deep.equal(dereferencedSchema);
// 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/external-root/external-root.yaml"));
expect(schema).to.equal(parser.schema);
expect(schema).to.deep.equal(bundledSchema);
});
});
3 changes: 3 additions & 0 deletions test/specs/external-root/external-root.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$ref: 'definitions.yaml#/external1'
external2:
$ref: 'definitions-other.yaml#/external2'
27 changes: 27 additions & 0 deletions test/specs/external-root/parsed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";

module.exports =
{
schema: {
$ref: "definitions.yaml#/external1",
external2: {
$ref: "definitions-other.yaml#/external2"
},
},

definitions: {
external1: {
thing: {
type: "string"
}
}
},

definitionsOther: {
external2: {
thing: {
type: "string"
}
}
}
};