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

#556 - handle recursive references to deep schema definitions #1142

Merged
merged 3 commits into from
Jan 21, 2019
Merged
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
3 changes: 3 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,9 @@ function findSchemaDefinition($ref, definitions = {}) {
let current = definitions;
for (let part of parts) {
part = part.replace(/~1/g, "/").replace(/~0/g, "~");
while (current.hasOwnProperty("$ref")) {
current = findSchemaDefinition(current.$ref, definitions);
}
if (current.hasOwnProperty(part)) {
current = current[part];
} else {
Expand Down
86 changes: 86 additions & 0 deletions test/Form_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,92 @@ describe("Form", () => {
expect(node.querySelector("#root_children_0_name")).to.exist;
});

it("should follow recursive references", () => {
const schema = {
definitions: {
bar: { $ref: "#/definitions/qux" },
qux: { type: "string" },
},
type: "object",
required: ["foo"],
properties: {
foo: { $ref: "#/definitions/bar" },
},
};
const { node } = createFormComponent({ schema });

expect(node.querySelectorAll("input[type=text]")).to.have.length.of(1);
});

it("should follow multiple recursive references", () => {
const schema = {
definitions: {
bar: { $ref: "#/definitions/bar2" },
bar2: { $ref: "#/definitions/qux" },
qux: { type: "string" },
},
type: "object",
required: ["foo"],
properties: {
foo: { $ref: "#/definitions/bar" },
},
};
const { node } = createFormComponent({ schema });

expect(node.querySelectorAll("input[type=text]")).to.have.length.of(1);
});

it("should handle recursive references to deep schema definitions", () => {
const schema = {
definitions: {
testdef: {
$ref: "#/definitions/testdefref",
},
testdefref: {
type: "object",
properties: {
bar: { type: "string" },
},
},
},
type: "object",
properties: {
foo: { $ref: "#/definitions/testdef/properties/bar" },
},
};

const { node } = createFormComponent({ schema });

expect(node.querySelectorAll("input[type=text]")).to.have.length.of(1);
});

it("should handle multiple recursive references to deep schema definitions", () => {
const schema = {
definitions: {
testdef: {
$ref: "#/definitions/testdefref1",
},
testdefref1: {
$ref: "#/definitions/testdefref2",
},
testdefref2: {
type: "object",
properties: {
bar: { type: "string" },
},
},
},
type: "object",
properties: {
foo: { $ref: "#/definitions/testdef/properties/bar" },
},
};

const { node } = createFormComponent({ schema });

expect(node.querySelectorAll("input[type=text]")).to.have.length.of(1);
});

it("should priorize definition over schema type property", () => {
// Refs bug #140
const schema = {
Expand Down