@@ -19,18 +19,21 @@ const findSchemaType = (schema) => {
19
19
return "primitive" ;
20
20
} ;
21
21
22
+ const nullableExtras = ( schema , value ) => {
23
+ const { nullable, type } = schema || { } ;
24
+ return nullable || type === "null" ? `${ value } | null` : value ;
25
+ } ;
26
+
22
27
const getPrimitiveType = ( property ) => {
23
- const { type, nullable } = property || { } ;
28
+ const { type } = property || { } ;
24
29
const primitiveType = typeAliases [ type ] || type ;
25
- return primitiveType
26
- ? ( nullable && `${ primitiveType } | null` ) || primitiveType
27
- : DEFAULT_PRIMITIVE_TYPE ;
30
+ return primitiveType ? nullableExtras ( property , primitiveType ) : DEFAULT_PRIMITIVE_TYPE ;
28
31
} ;
29
32
30
33
const specificObjectTypes = {
31
- array : ( { items } ) => {
34
+ array : ( { items, ... schemaPart } ) => {
32
35
const { content, type } = parseSchema ( items , null , inlineExtraFormatters ) ;
33
- return type === "primitive" ? `${ content } []` : `Array<${ content } >` ;
36
+ return nullableExtras ( schemaPart , type === "primitive" ? `${ content } []` : `Array<${ content } >` ) ;
34
37
} ,
35
38
} ;
36
39
@@ -48,14 +51,17 @@ const getType = (property) => {
48
51
if ( ! property ) return DEFAULT_PRIMITIVE_TYPE ;
49
52
50
53
const anotherTypeGetter = specificObjectTypes [ property . type ] || getPrimitiveType ;
51
- return getRefTypeName ( property ) || anotherTypeGetter ( property ) ;
54
+ const refType = getRefTypeName ( property ) ;
55
+ return refType ? nullableExtras ( property , refType ) : anotherTypeGetter ( property ) ;
52
56
} ;
53
57
54
58
const getObjectTypeContent = ( properties ) => {
55
59
return _ . map ( properties , ( property , name ) => {
56
- // TODO: probably nullable should'n be use as required/no-required conditions
57
- const isRequired =
58
- typeof property . nullable === "undefined" ? property . required : ! property . nullable ;
60
+ const isRequired = config . convertedFromSwagger2
61
+ ? typeof property . nullable === "undefined"
62
+ ? property . required
63
+ : ! property . nullable
64
+ : ! ! property . required ;
59
65
return {
60
66
description : property . description ,
61
67
isRequired,
@@ -72,16 +78,21 @@ const complexSchemaParsers = {
72
78
oneOf : ( schema ) => {
73
79
// T1 | T2
74
80
const combined = _ . map ( schema . oneOf , complexTypeGetter ) ;
75
- return combined . join ( " | " ) ;
81
+ return nullableExtras ( schema , combined . join ( " | " ) ) ;
76
82
} ,
77
83
allOf : ( schema ) => {
78
84
// T1 & T2
79
- return _ . map ( schema . allOf , complexTypeGetter ) . join ( " & " ) ;
85
+ return nullableExtras ( schema , _ . map ( schema . allOf , complexTypeGetter ) . join ( " & " ) ) ;
80
86
} ,
81
87
anyOf : ( schema ) => {
82
88
// T1 | T2 | (T1 & T2)
83
89
const combined = _ . map ( schema . anyOf , complexTypeGetter ) ;
84
- return `${ combined . join ( " | " ) } ` + ( combined . length > 1 ? ` | (${ combined . join ( " & " ) } )` : "" ) ;
90
+ const nonEmptyTypesCombined = combined . filter ( ( type ) => ! jsEmptyTypes . includes ( type ) ) ;
91
+ return nullableExtras (
92
+ schema ,
93
+ `${ combined . join ( " | " ) } ` +
94
+ ( nonEmptyTypesCombined . length > 1 ? ` | (${ nonEmptyTypesCombined . join ( " & " ) } )` : "" ) ,
95
+ ) ;
85
96
} ,
86
97
// TODO
87
98
not : ( schema ) => {
@@ -170,7 +181,8 @@ const schemaParsers = {
170
181
typeIdentifier : "type" ,
171
182
name : typeName ,
172
183
description : formatDescription ( description ) ,
173
- content : contentType || getType ( schema ) ,
184
+ // TODO: probably it should be refactored. `type === 'null'` is not flexible
185
+ content : type === "null" ? type : contentType || getType ( schema ) ,
174
186
} ;
175
187
} ,
176
188
} ;
@@ -191,12 +203,10 @@ const parseSchema = (rawSchema, typeName, formattersMap) => {
191
203
let parsedSchema = null ;
192
204
193
205
if ( typeof rawSchema === "string" ) {
194
- console . log ( "WOW THERE IS STRING" , rawSchema ) ;
195
206
return rawSchema ;
196
207
}
197
208
198
209
if ( rawSchema . $parsedSchema ) {
199
- console . log ( "IT IS ALREADY PARSED SCHEMA" , rawSchema ) ;
200
210
schemaType = rawSchema . schemaType ;
201
211
parsedSchema = rawSchema ;
202
212
} else {
0 commit comments