Skip to content

Commit

Permalink
#556 - handle recursive references to deep schema definitions (#1142)
Browse files Browse the repository at this point in the history
* #556 - handle recursive references to deep schema definitions

* test: add tests for recursive references

* fix: handle multiple recursive references to deep schema definitions
  • Loading branch information
epicfaace authored Jan 21, 2019
1 parent 55c0261 commit cf46342
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
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

0 comments on commit cf46342

Please sign in to comment.