Skip to content

Commit 63636b7

Browse files
authored
Don't crash when non-object formData is passed in to a schema item with additionalProperties (rjsf-team#2595)
* handle null formData when stubbing existing additional properties * added Form test * improved non-object formData handling and added corresponding test * fixed formatting
1 parent 3daabab commit 63636b7

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

packages/core/src/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,9 @@ export function stubExistingAdditionalProperties(
634634
properties: { ...schema.properties },
635635
};
636636

637+
// make sure formData is an object
638+
formData = isObject(formData) ? formData : {};
639+
637640
Object.keys(formData).forEach(key => {
638641
if (schema.properties.hasOwnProperty(key)) {
639642
// No need to stub, our schema already has the property

packages/core/test/Form_test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,59 @@ describeRepeated("Form common", createFormComponent => {
506506
expect(node.querySelectorAll("input[type=text]")).to.have.length.of(1);
507507
});
508508

509+
it("should not crash with null values for property with additionalProperties", () => {
510+
const schema = {
511+
type: "object",
512+
properties: {
513+
data: {
514+
additionalProperties: {
515+
type: "string",
516+
},
517+
type: "object",
518+
},
519+
},
520+
};
521+
522+
const { node } = createFormComponent({
523+
schema,
524+
formData: {
525+
data: null,
526+
},
527+
});
528+
529+
expect(node).to.not.be.null;
530+
});
531+
532+
it("should not crash with non-object values for property with additionalProperties", () => {
533+
const schema = {
534+
type: "object",
535+
properties: {
536+
data1: {
537+
additionalProperties: {
538+
type: "string",
539+
},
540+
type: "object",
541+
},
542+
data2: {
543+
additionalProperties: {
544+
type: "string",
545+
},
546+
type: "object",
547+
},
548+
},
549+
};
550+
551+
const { node } = createFormComponent({
552+
schema,
553+
formData: {
554+
data1: 123,
555+
data2: ["one", "two", "three"],
556+
},
557+
});
558+
559+
expect(node).to.not.be.null;
560+
});
561+
509562
it("should raise for non-existent definitions referenced", () => {
510563
const schema = {
511564
type: "object",

packages/core/test/utils_test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,21 @@ describe("utils", () => {
17701770
});
17711771
});
17721772

1773+
it("should handle null formData for schema which contains additionalProperties", () => {
1774+
const schema = {
1775+
additionalProperties: {
1776+
type: "string",
1777+
},
1778+
type: "object",
1779+
};
1780+
1781+
const formData = null;
1782+
expect(retrieveSchema(schema, {}, formData)).eql({
1783+
...schema,
1784+
properties: {},
1785+
});
1786+
});
1787+
17731788
it("should priorize local definitions over foreign ones", () => {
17741789
const schema = {
17751790
$ref: "#/definitions/address",

0 commit comments

Comments
 (0)