@@ -170,7 +170,25 @@ export const createDefaultValue = (
170
170
schema : JsonSchema ,
171
171
rootSchema : JsonSchema
172
172
) => {
173
- const resolvedSchema = Resolve . schema ( schema , schema . $ref , rootSchema ) ;
173
+ const defaultValue = doCreateDefaultValue ( schema , rootSchema ) ;
174
+
175
+ // preserve the backward compatibility where it is returning an empty object if we can't determine the default value
176
+ return defaultValue === undefined ? { } : defaultValue ;
177
+ } ;
178
+
179
+ /**
180
+ * Create a default value based on the given schema.
181
+ * @param schema the schema for which to create a default value.
182
+ * @returns the default value to use, undefined if none was found
183
+ */
184
+ export const doCreateDefaultValue = (
185
+ schema : JsonSchema ,
186
+ rootSchema : JsonSchema
187
+ ) => {
188
+ const resolvedSchema =
189
+ typeof schema . $ref === 'string'
190
+ ? Resolve . schema ( rootSchema , schema . $ref , rootSchema )
191
+ : schema ;
174
192
if ( resolvedSchema . default !== undefined ) {
175
193
return extractDefaults ( resolvedSchema , rootSchema ) ;
176
194
}
@@ -183,22 +201,56 @@ export const createDefaultValue = (
183
201
return convertDateToString ( new Date ( ) , resolvedSchema . format ) ;
184
202
}
185
203
return '' ;
186
- } else if (
187
- hasType ( resolvedSchema , 'integer' ) ||
188
- hasType ( resolvedSchema , 'number' )
189
- ) {
204
+ }
205
+ if ( hasType ( resolvedSchema , 'integer' ) || hasType ( resolvedSchema , 'number' ) ) {
190
206
return 0 ;
191
- } else if ( hasType ( resolvedSchema , 'boolean' ) ) {
207
+ }
208
+ if ( hasType ( resolvedSchema , 'boolean' ) ) {
192
209
return false ;
193
- } else if ( hasType ( resolvedSchema , 'array' ) ) {
210
+ }
211
+ if ( hasType ( resolvedSchema , 'array' ) ) {
194
212
return [ ] ;
195
- } else if ( hasType ( resolvedSchema , 'object' ) ) {
213
+ }
214
+ if ( hasType ( resolvedSchema , 'object' ) ) {
196
215
return extractDefaults ( resolvedSchema , rootSchema ) ;
197
- } else if ( hasType ( resolvedSchema , 'null' ) ) {
216
+ }
217
+ if ( hasType ( resolvedSchema , 'null' ) ) {
198
218
return null ;
199
- } else {
200
- return { } ;
201
219
}
220
+
221
+ const combinators : CombinatorKeyword [ ] = [ 'oneOf' , 'anyOf' , 'allOf' ] ;
222
+ for ( const combinator of combinators ) {
223
+ if ( schema [ combinator ] && Array . isArray ( schema [ combinator ] ) ) {
224
+ const combinatorDefault = createDefaultValueForCombinatorSchema (
225
+ schema [ combinator ] ,
226
+ rootSchema
227
+ ) ;
228
+ if ( combinatorDefault !== undefined ) {
229
+ return combinatorDefault ;
230
+ }
231
+ }
232
+ }
233
+
234
+ // no default value found
235
+ return undefined ;
236
+ } ;
237
+
238
+ const createDefaultValueForCombinatorSchema = (
239
+ combinatorSchemas : JsonSchema [ ] ,
240
+ rootSchema : JsonSchema
241
+ ) : any => {
242
+ if ( combinatorSchemas . length > 0 ) {
243
+ for ( const combinatorSchema of combinatorSchemas ) {
244
+ const result = doCreateDefaultValue ( combinatorSchema , rootSchema ) ;
245
+ if ( result !== undefined ) {
246
+ // return the first one with type information
247
+ return result ;
248
+ }
249
+ }
250
+ }
251
+
252
+ // no default value found
253
+ return undefined ;
202
254
} ;
203
255
204
256
/**
@@ -214,10 +266,26 @@ export const extractDefaults = (schema: JsonSchema, rootSchema: JsonSchema) => {
214
266
const resolvedProperty = property . $ref
215
267
? Resolve . schema ( rootSchema , property . $ref , rootSchema )
216
268
: property ;
217
- if ( resolvedProperty . default !== undefined ) {
269
+ if ( resolvedProperty && resolvedProperty . default !== undefined ) {
218
270
result [ key ] = cloneDeep ( resolvedProperty . default ) ;
219
271
}
220
272
}
273
+ // there could be more properties in allOf schemas
274
+ if ( schema . allOf && Array . isArray ( schema . allOf ) ) {
275
+ schema . allOf . forEach ( ( allOfSchema ) => {
276
+ if ( allOfSchema && allOfSchema . properties ) {
277
+ for ( const key in allOfSchema . properties ) {
278
+ const property = allOfSchema . properties [ key ] ;
279
+ const resolvedProperty = property . $ref
280
+ ? Resolve . schema ( rootSchema , property . $ref , rootSchema )
281
+ : property ;
282
+ if ( resolvedProperty && resolvedProperty . default !== undefined ) {
283
+ result [ key ] = cloneDeep ( resolvedProperty . default ) ;
284
+ }
285
+ }
286
+ }
287
+ } ) ;
288
+ }
221
289
return result ;
222
290
}
223
291
return cloneDeep ( schema . default ) ;
0 commit comments