react-jsonschema-form icon indicating copy to clipboard operation
react-jsonschema-form copied to clipboard

Array defaults don't work at the root of the schema.

Open abdalla-rko opened this issue 1 year ago • 1 comments

Prerequisites

What theme are you using?

core

Version

5.21.2

Current Behavior

Array defaults don't work at the root of the schema.

Expected Behavior

Array defaults should work at the root of the schema and not only when it's nested.

Steps To Reproduce

  1. Click here open the demo with array at the root of the schema
  2. You'll notice that defaults are not applied.
  3. Click here to open the demo with nested array.
  4. You'll notice that defaults are applied when the array is nested and not at the root of the schema.

Environment

- OS: macOS
- Node: 22.1.0
- npm: 10.7.0

Anything else?

No response

abdalla-rko avatar Oct 12 '24 10:10 abdalla-rko

@abdalla-rko Another interesting bug with our defaulting code. Are you able to fix this for us?

heath-freenome avatar Oct 18 '24 19:10 heath-freenome

This is not an issue. It was not applying the defaults because the formData has an empty array and the array mergeExtraDefaults was not enabled. But there is an issue when you enable the mergeExtraDefaults and you have formData as an empty array, the default on the root of the array schema doesn't get applied and only applies the default from the items property (see demo). If formData is undefined or null, it will apply the default from the root schema and items.default and the root default will take precedence over item default.

abdalla-rko avatar Oct 31 '24 20:10 abdalla-rko