nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: mozilla-services/react-jsonschema-form
Pull request #871 (2018-03-23 03:10:05)

Fix corner case of empty items array
### Reasons for making this change

When upgrading from an old version I found that our code was creating array schemas in the shape shown in the test case, which was resulting in the `getDefaultFormState` function creating form data that had an array of `undefined`s, instead of using `additionalItems` as the item schema. This meant that required fields in the array item schema weren't triggering validation errors.

### Checklist

* [x] **I'm adding or updating code**
  - [x] I've added and/or updated tests
  - [x] I've updated docs if needed
  - [x] I've run `npm run cs-format` on my branch to conform my code to [prettier](https://github.com/prettier/prettier) coding style


edi9999: To me this looks good, I suggest to merge this soon.
epicfaace: @glasserc Can you merge this?

Patch:
diff --git a/src/utils.js b/src/utils.js
index 47856324d..7deaa3edf 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -157,9 +157,12 @@ function computeDefaults(schema, parentDefaults, definitions = {}) {
           if (schema.minItems > defaultsLength) {
             const defaultEntries = defaults || [];
             // populate the array with the defaults
+            const fillerSchema = Array.isArray(schema.items)
+              ? schema.additionalItems
+              : schema.items;
             const fillerEntries = fill(
               new Array(schema.minItems - defaultsLength),
-              computeDefaults(schema.items, schema.items.defaults, definitions)
+              computeDefaults(fillerSchema, fillerSchema.defaults, definitions)
             );
             // then fill up the rest with either the item default or empty, up to minItems
 


Test patch:
diff --git a/test/utils_test.js b/test/utils_test.js
index 0d97f0551..7ba1be760 100644
--- a/test/utils_test.js
+++ b/test/utils_test.js
@@ -292,6 +292,26 @@ describe("utils", () => {
           foo: 42,
         });
       });
+
+      it("should fill array with additional items schema when items is empty", () => {
+        const schema = {
+          type: "object",
+          properties: {
+            array: {
+              type: "array",
+              minItems: 1,
+              additionalItems: {
+                type: "string",
+                default: "foo",
+              },
+              items: [],
+            },
+          },
+        };
+        expect(getDefaultFormState(schema, {})).eql({
+          array: ["foo"],
+        });
+      });
     });
   });