1
1
import get from 'lodash/get' ;
2
2
import isEmpty from 'lodash/isEmpty' ;
3
3
4
- import { ANY_OF_KEY , DEFAULT_KEY , DEPENDENCIES_KEY , PROPERTIES_KEY , ONE_OF_KEY , REF_KEY } from '../constants' ;
4
+ import {
5
+ ANY_OF_KEY ,
6
+ DEFAULT_KEY ,
7
+ DEPENDENCIES_KEY ,
8
+ PROPERTIES_KEY ,
9
+ ONE_OF_KEY ,
10
+ REF_KEY ,
11
+ ALL_OF_KEY ,
12
+ } from '../constants' ;
5
13
import findSchemaDefinition from '../findSchemaDefinition' ;
6
14
import getClosestMatchingOption from './getClosestMatchingOption' ;
7
15
import getDiscriminatorFieldFromSchema from '../getDiscriminatorFieldFromSchema' ;
@@ -255,43 +263,52 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
255
263
switch ( getSchemaType < S > ( schema ) ) {
256
264
// We need to recurse for object schema inner default values.
257
265
case 'object' : {
258
- const objectDefaults = Object . keys ( schema . properties || { } ) . reduce ( ( acc : GenericObjectType , key : string ) => {
259
- // Compute the defaults for this node, with the parent defaults we might
260
- // have from a previous run: defaults[key].
261
- const computedDefault = computeDefaults < T , S , F > ( validator , get ( schema , [ PROPERTIES_KEY , key ] ) , {
262
- rootSchema,
263
- _recurseList,
264
- experimental_defaultFormStateBehavior,
265
- includeUndefinedValues : includeUndefinedValues === true ,
266
- parentDefaults : get ( defaults , [ key ] ) ,
267
- rawFormData : get ( formData , [ key ] ) ,
268
- required : schema . required ?. includes ( key ) ,
269
- } ) ;
270
- maybeAddDefaultToObject < T > (
271
- acc ,
272
- key ,
273
- computedDefault ,
274
- includeUndefinedValues ,
275
- required ,
276
- schema . required ,
277
- experimental_defaultFormStateBehavior
278
- ) ;
279
- return acc ;
280
- } , { } ) as T ;
281
- if ( schema . additionalProperties ) {
266
+ // This is a custom addition that fixes this issue:
267
+ // https://github.com/rjsf-team/react-jsonschema-form/issues/3832
268
+ const retrievedSchema =
269
+ ALL_OF_KEY in schema ? retrieveSchema < T , S , F > ( validator , schema , rootSchema , formData ) : schema ;
270
+ const objectDefaults = Object . keys ( retrievedSchema . properties || { } ) . reduce (
271
+ ( acc : GenericObjectType , key : string ) => {
272
+ // Compute the defaults for this node, with the parent defaults we might
273
+ // have from a previous run: defaults[key].
274
+ const computedDefault = computeDefaults < T , S , F > ( validator , get ( retrievedSchema , [ PROPERTIES_KEY , key ] ) , {
275
+ rootSchema,
276
+ _recurseList,
277
+ experimental_defaultFormStateBehavior,
278
+ includeUndefinedValues : includeUndefinedValues === true ,
279
+ parentDefaults : get ( defaults , [ key ] ) ,
280
+ rawFormData : get ( formData , [ key ] ) ,
281
+ required : retrievedSchema . required ?. includes ( key ) ,
282
+ } ) ;
283
+ maybeAddDefaultToObject < T > (
284
+ acc ,
285
+ key ,
286
+ computedDefault ,
287
+ includeUndefinedValues ,
288
+ required ,
289
+ retrievedSchema . required ,
290
+ experimental_defaultFormStateBehavior
291
+ ) ;
292
+ return acc ;
293
+ } ,
294
+ { }
295
+ ) as T ;
296
+ if ( retrievedSchema . additionalProperties ) {
282
297
// as per spec additionalProperties may be either schema or boolean
283
- const additionalPropertiesSchema = isObject ( schema . additionalProperties ) ? schema . additionalProperties : { } ;
298
+ const additionalPropertiesSchema = isObject ( retrievedSchema . additionalProperties )
299
+ ? retrievedSchema . additionalProperties
300
+ : { } ;
284
301
const keys = new Set < string > ( ) ;
285
302
if ( isObject ( defaults ) ) {
286
303
Object . keys ( defaults as GenericObjectType )
287
- . filter ( ( key ) => ! schema . properties || ! schema . properties [ key ] )
304
+ . filter ( ( key ) => ! retrievedSchema . properties || ! retrievedSchema . properties [ key ] )
288
305
. forEach ( ( key ) => keys . add ( key ) ) ;
289
306
}
290
307
let formDataRequired : string [ ] ;
291
308
if ( isObject ( formData ) ) {
292
309
formDataRequired = [ ] ;
293
310
Object . keys ( formData as GenericObjectType )
294
- . filter ( ( key ) => ! schema . properties || ! schema . properties [ key ] )
311
+ . filter ( ( key ) => ! retrievedSchema . properties || ! retrievedSchema . properties [ key ] )
295
312
. forEach ( ( key ) => {
296
313
keys . add ( key ) ;
297
314
formDataRequired . push ( key ) ;
@@ -305,7 +322,7 @@ export function computeDefaults<T = any, S extends StrictRJSFSchema = RJSFSchema
305
322
includeUndefinedValues : includeUndefinedValues === true ,
306
323
parentDefaults : get ( defaults , [ key ] ) ,
307
324
rawFormData : get ( formData , [ key ] ) ,
308
- required : schema . required ?. includes ( key ) ,
325
+ required : retrievedSchema . required ?. includes ( key ) ,
309
326
} ) ;
310
327
// Since these are additional properties we don’t need to add the `experimental_defaultFormStateBehavior` prop
311
328
maybeAddDefaultToObject < T > (
0 commit comments