Switching from a null option back to an object option restores the field's default
Detect
- Your schema has a
oneOf/anyOfwith atype: 'null'option beside an object option, and the field or an ancestor declares adefaultfor it. - Tests that select the
nulloption, then the object option, and assert the data.
Change
A field that held nothing, as after a null option, and switches to an option whose defaults are an object gets the defaults of the field with that option alone, so a default the field declares comes back with the option's own (#5368). Those defaults hold no key set to undefined, so a default an ancestor declares for the field fills the keys they leave, as the form fills any other.
No code has to change. Update tests; "before" is what 6.11.0 gives:
- With
{ type: 'object', oneOf: [{ title: 'Obj', type: 'object', required: ['kind'], properties: { kind: { const: 'obj' }, note: { type: 'string', default: 'from-option' }, extra: { type: 'string' } } }, { title: 'None', type: 'null' }], default: { kind: 'obj', extra: 'from-root' } }, selectNone, thenObj. Before:{ note: 'from-option' }. After:{ kind: 'obj', note: 'from-option', extra: 'from-root' }, the data the form mounted with. - With the same
oneOfasproperties.w.properties.pand thedefaultdeclared on the root as{ w: { p: { kind: 'obj', extra: 'from-root' } } }, the round trip gave{ w: { p: { note: 'from-option' } } }and now gives{ w: { p: { kind: 'obj', note: 'from-option', extra: 'from-root' } } }. The form mounted with{ w: { p: { kind: 'obj', extra: 'from-root' } } }and now mounts with the data the round trip gives, since the ancestor'sdefaultpicks the option.
Verify
- In the nested example, the form mounts with
{ w: { p: { kind: 'obj', note: 'from-option', extra: 'from-root' } } }, the data the round trip gives. - An object property of the option still gets the empty object a switch from another object option gives it: with
meta: { type: 'object', properties: { a: { type: 'string' } } }inObj, the switch fromNonewritesmeta: {}, as before.
Also read: