@@ -63,12 +63,7 @@ const getTypeAlias = (rawSchema) => {
63
63
} ;
64
64
65
65
const getEnumNames = ( schema ) => {
66
- return (
67
- schema [ "x-enumNames" ] ||
68
- schema [ "xEnumNames" ] ||
69
- schema [ "x-enumnames" ] ||
70
- schema [ "x-enum-varnames" ]
71
- ) ;
66
+ return schema [ "x-enumNames" ] || schema [ "xEnumNames" ] || schema [ "x-enumnames" ] || schema [ "x-enum-varnames" ] ;
72
67
} ;
73
68
74
69
const getInternalSchemaType = ( schema ) => {
@@ -151,9 +146,7 @@ const getObjectTypeContent = (schema) => {
151
146
! _ . isUndefined ( property . maximum ) && `@max ${ property . maximum } ` ,
152
147
! _ . isUndefined ( property . pattern ) && `@pattern ${ property . pattern } ` ,
153
148
! _ . isUndefined ( property . example ) &&
154
- `@example ${
155
- _ . isObject ( property . example ) ? JSON . stringify ( property . example ) : property . example
156
- } `,
149
+ `@example ${ _ . isObject ( property . example ) ? JSON . stringify ( property . example ) : property . example } ` ,
157
150
] ) . join ( "\n" ) ,
158
151
isRequired : required ,
159
152
isNullable : nullable ,
@@ -178,44 +171,69 @@ const getObjectTypeContent = (schema) => {
178
171
const complexTypeGetter = ( schema ) => getInlineParseContent ( schema ) ;
179
172
const filterContents = ( contents , types ) => _ . filter ( contents , ( type ) => ! _ . includes ( types , type ) ) ;
180
173
174
+ const makeAddRequiredToChildSchema = ( parentSchema ) => ( childSchema ) => {
175
+ let required = childSchema . required || [ ] ;
176
+ let properties = childSchema . properties || { } ;
177
+
178
+ // Inherit all the required fields from the parent schema that are defined
179
+ // either on the parent schema or on the child schema
180
+ // TODO: any that are defined at grandparents or higher are ignored
181
+ required = required . concat (
182
+ ( parentSchema . required || [ ] ) . filter (
183
+ ( key ) =>
184
+ ! required . includes ( key ) && ( _ . keys ( properties ) . includes ( key ) || _ . keys ( parentSchema . properties ) . includes ( key ) ) ,
185
+ ) ,
186
+ ) ;
187
+
188
+ // Identify properties that are required in the child schema, but
189
+ // defined only in the parent schema (TODO: this only works one level deep)
190
+ const parentPropertiesRequiredByChild = required . filter (
191
+ ( key ) => ! _ . keys ( childSchema . properties ) . includes ( key ) && _ . keys ( parentSchema . properties ) . includes ( key ) ,
192
+ ) ;
193
+
194
+ // Add such properties to the child so that they can be overriden and made required
195
+ properties = {
196
+ ...properties ,
197
+ ...parentPropertiesRequiredByChild . reduce (
198
+ ( additionalProperties , key ) => ( {
199
+ ...additionalProperties ,
200
+ [ key ] : ( parentSchema . properties || { } ) [ key ] ,
201
+ } ) ,
202
+ { } ,
203
+ ) ,
204
+ } ;
205
+
206
+ return _ . merge (
207
+ {
208
+ required : required ,
209
+ properties : properties ,
210
+ } ,
211
+ childSchema ,
212
+ ) ;
213
+ } ;
214
+
181
215
const complexSchemaParsers = {
182
216
[ SCHEMA_TYPES . COMPLEX_ONE_OF ] : ( schema ) => {
183
217
// T1 | T2
184
- const combined = _ . map (
185
- schema . oneOf . map ( ( s ) => _ . merge ( { required : schema . required } , s ) ) ,
186
- complexTypeGetter ,
187
- ) ;
218
+ const combined = _ . map ( schema . oneOf . map ( makeAddRequiredToChildSchema ( schema ) ) , complexTypeGetter ) ;
188
219
189
220
return checkAndAddNull ( schema , filterContents ( combined , [ TS_KEYWORDS . ANY ] ) . join ( " | " ) ) ;
190
221
} ,
191
222
[ SCHEMA_TYPES . COMPLEX_ALL_OF ] : ( schema ) => {
192
223
// T1 & T2
193
- const combined = _ . map (
194
- schema . allOf . map ( ( s ) => _ . merge ( { required : schema . required } , s ) ) ,
195
- complexTypeGetter ,
196
- ) ;
224
+ const combined = _ . map ( schema . allOf . map ( makeAddRequiredToChildSchema ( schema ) ) , complexTypeGetter ) ;
197
225
return checkAndAddNull (
198
226
schema ,
199
- filterContents ( combined , [ ...JS_EMPTY_TYPES , ...JS_PRIMITIVE_TYPES , TS_KEYWORDS . ANY ] ) . join (
200
- " & " ,
201
- ) ,
227
+ filterContents ( combined , [ ...JS_EMPTY_TYPES , ...JS_PRIMITIVE_TYPES , TS_KEYWORDS . ANY ] ) . join ( " & " ) ,
202
228
) ;
203
229
} ,
204
230
[ SCHEMA_TYPES . COMPLEX_ANY_OF ] : ( schema ) => {
205
231
// T1 | T2 | (T1 & T2)
206
- const combined = _ . map (
207
- schema . anyOf . map ( ( s ) => _ . merge ( { required : schema . required } , s ) ) ,
208
- complexTypeGetter ,
209
- ) ;
210
- const nonEmptyTypesCombined = filterContents ( combined , [
211
- ...JS_EMPTY_TYPES ,
212
- ...JS_PRIMITIVE_TYPES ,
213
- TS_KEYWORDS . ANY ,
214
- ] ) ;
232
+ const combined = _ . map ( schema . anyOf . map ( makeAddRequiredToChildSchema ( schema ) ) , complexTypeGetter ) ;
233
+ const nonEmptyTypesCombined = filterContents ( combined , [ ...JS_EMPTY_TYPES , ...JS_PRIMITIVE_TYPES , TS_KEYWORDS . ANY ] ) ;
215
234
return checkAndAddNull (
216
235
schema ,
217
- `${ combined . join ( " | " ) } ` +
218
- ( nonEmptyTypesCombined . length > 1 ? ` | (${ nonEmptyTypesCombined . join ( " & " ) } )` : "" ) ,
236
+ `${ combined . join ( " | " ) } ` + ( nonEmptyTypesCombined . length > 1 ? ` | (${ nonEmptyTypesCombined . join ( " & " ) } )` : "" ) ,
219
237
) ;
220
238
} ,
221
239
// TODO
@@ -236,8 +254,7 @@ const getComplexType = (schema) => {
236
254
} ;
237
255
238
256
const attachParsedRef = ( originalSchema , parsedSchema ) => {
239
- const parsedSchemaAfterHook =
240
- config . hooks . onParseSchema ( originalSchema , parsedSchema ) || parsedSchema ;
257
+ const parsedSchemaAfterHook = config . hooks . onParseSchema ( originalSchema , parsedSchema ) || parsedSchema ;
241
258
242
259
if ( originalSchema ) {
243
260
originalSchema . $parsed = parsedSchemaAfterHook ;
@@ -272,12 +289,7 @@ const schemaParsers = {
272
289
return {
273
290
key : formattedKey ,
274
291
type : keyType ,
275
- value :
276
- enumValue === null
277
- ? enumValue
278
- : isIntegerOrBooleanEnum
279
- ? `${ enumValue } `
280
- : `"${ enumValue } "` ,
292
+ value : enumValue === null ? enumValue : isIntegerOrBooleanEnum ? `${ enumValue } ` : `"${ enumValue } "` ,
281
293
} ;
282
294
} ) ;
283
295
} else {
@@ -299,9 +311,7 @@ const schemaParsers = {
299
311
type : SCHEMA_TYPES . ENUM ,
300
312
keyType : keyType ,
301
313
typeIdentifier :
302
- config . generateUnionEnums || ( ! enumNames && isIntegerOrBooleanEnum )
303
- ? TS_KEYWORDS . TYPE
304
- : TS_KEYWORDS . ENUM ,
314
+ config . generateUnionEnums || ( ! enumNames && isIntegerOrBooleanEnum ) ? TS_KEYWORDS . TYPE : TS_KEYWORDS . ENUM ,
305
315
name : typeName ,
306
316
description : formatDescription ( schema . description ) ,
307
317
content,
@@ -340,8 +350,7 @@ const schemaParsers = {
340
350
content :
341
351
_ . compact ( [
342
352
complexSchemaContent && `(${ complexSchemaContent } )` ,
343
- getInternalSchemaType ( simpleSchema ) === TS_KEYWORDS . OBJECT &&
344
- getInlineParseContent ( simpleSchema ) ,
353
+ getInternalSchemaType ( simpleSchema ) === TS_KEYWORDS . OBJECT && getInlineParseContent ( simpleSchema ) ,
345
354
] ) . join ( " & " ) || TS_KEYWORDS . ANY ,
346
355
} ) ;
347
356
} ,
@@ -409,10 +418,7 @@ const parseSchema = (rawSchema, typeName, formattersMap) => {
409
418
parsedSchema = schemaParsers [ schemaType ] ( fixedRawSchema , typeName ) ;
410
419
}
411
420
412
- return (
413
- ( formattersMap && formattersMap [ schemaType ] && formattersMap [ schemaType ] ( parsedSchema ) ) ||
414
- parsedSchema
415
- ) ;
421
+ return ( formattersMap && formattersMap [ schemaType ] && formattersMap [ schemaType ] ( parsedSchema ) ) || parsedSchema ;
416
422
} ;
417
423
418
424
const parseSchemas = ( components ) =>
@@ -421,8 +427,7 @@ const parseSchemas = (components) =>
421
427
const getInlineParseContent = ( rawTypeData , typeName = null ) =>
422
428
parseSchema ( rawTypeData , typeName , inlineExtraFormatters ) . content ;
423
429
424
- const getParseContent = ( rawTypeData , typeName = null ) =>
425
- parseSchema ( rawTypeData , typeName ) . content ;
430
+ const getParseContent = ( rawTypeData , typeName = null ) => parseSchema ( rawTypeData , typeName ) . content ;
426
431
427
432
module . exports = {
428
433
types,
0 commit comments