@@ -19,18 +19,21 @@ const findSchemaType = (schema) => {
1919 return "primitive" ;
2020} ;
2121
22+ const nullableExtras = ( schema , value ) => {
23+ const { nullable, type } = schema || { } ;
24+ return nullable || type === "null" ? `${ value } | null` : value ;
25+ } ;
26+
2227const getPrimitiveType = ( property ) => {
23- const { type, nullable } = property || { } ;
28+ const { type } = property || { } ;
2429 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 ;
2831} ;
2932
3033const specificObjectTypes = {
31- array : ( { items } ) => {
34+ array : ( { items, ... schemaPart } ) => {
3235 const { content, type } = parseSchema ( items , null , inlineExtraFormatters ) ;
33- return type === "primitive" ? `${ content } []` : `Array<${ content } >` ;
36+ return nullableExtras ( schemaPart , type === "primitive" ? `${ content } []` : `Array<${ content } >` ) ;
3437 } ,
3538} ;
3639
@@ -48,14 +51,17 @@ const getType = (property) => {
4851 if ( ! property ) return DEFAULT_PRIMITIVE_TYPE ;
4952
5053 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 ) ;
5256} ;
5357
5458const getObjectTypeContent = ( properties ) => {
5559 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 ;
5965 return {
6066 description : property . description ,
6167 isRequired,
@@ -72,16 +78,21 @@ const complexSchemaParsers = {
7278 oneOf : ( schema ) => {
7379 // T1 | T2
7480 const combined = _ . map ( schema . oneOf , complexTypeGetter ) ;
75- return combined . join ( " | " ) ;
81+ return nullableExtras ( schema , combined . join ( " | " ) ) ;
7682 } ,
7783 allOf : ( schema ) => {
7884 // T1 & T2
79- return _ . map ( schema . allOf , complexTypeGetter ) . join ( " & " ) ;
85+ return nullableExtras ( schema , _ . map ( schema . allOf , complexTypeGetter ) . join ( " & " ) ) ;
8086 } ,
8187 anyOf : ( schema ) => {
8288 // T1 | T2 | (T1 & T2)
8389 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+ ) ;
8596 } ,
8697 // TODO
8798 not : ( schema ) => {
@@ -170,7 +181,8 @@ const schemaParsers = {
170181 typeIdentifier : "type" ,
171182 name : typeName ,
172183 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 ) ,
174186 } ;
175187 } ,
176188} ;
@@ -191,12 +203,10 @@ const parseSchema = (rawSchema, typeName, formattersMap) => {
191203 let parsedSchema = null ;
192204
193205 if ( typeof rawSchema === "string" ) {
194- console . log ( "WOW THERE IS STRING" , rawSchema ) ;
195206 return rawSchema ;
196207 }
197208
198209 if ( rawSchema . $parsedSchema ) {
199- console . log ( "IT IS ALREADY PARSED SCHEMA" , rawSchema ) ;
200210 schemaType = rawSchema . schemaType ;
201211 parsedSchema = rawSchema ;
202212 } else {
0 commit comments