@@ -11,17 +11,20 @@ const typeAliases = {
11
11
integer : "number" ,
12
12
} ;
13
13
14
- const findSchemaType = schema => {
14
+ const findSchemaType = ( schema ) => {
15
15
if ( schema . enum ) return "enum" ;
16
16
if ( schema . properties ) return "object" ;
17
17
if ( schema . allOf || schema . oneOf || schema . anyOf || schema . not ) return "complex" ;
18
18
19
19
return "primitive" ;
20
20
} ;
21
21
22
- const getPrimitiveType = property => {
23
- const type = _ . get ( property , "type" ) ;
24
- return typeAliases [ type ] || type || DEFAULT_PRIMITIVE_TYPE ;
22
+ const getPrimitiveType = ( property ) => {
23
+ const { type, nullable } = property || { } ;
24
+ const primitiveType = typeAliases [ type ] || type ;
25
+ return primitiveType
26
+ ? ( nullable && `${ primitiveType } | null` ) || primitiveType
27
+ : DEFAULT_PRIMITIVE_TYPE ;
25
28
} ;
26
29
27
30
const specificObjectTypes = {
@@ -31,24 +34,24 @@ const specificObjectTypes = {
31
34
} ,
32
35
} ;
33
36
34
- const getRefType = property => {
37
+ const getRefType = ( property ) => {
35
38
const ref = property && property [ "$ref" ] ;
36
39
return ( ref && config . componentsMap [ ref ] ) || null ;
37
40
} ;
38
41
39
- const getRefTypeName = property => {
42
+ const getRefTypeName = ( property ) => {
40
43
const refTypeInfo = getRefType ( property ) ;
41
44
return refTypeInfo && checkAndRenameModelName ( refTypeInfo . typeName ) ;
42
45
} ;
43
46
44
- const getType = property => {
47
+ const getType = ( property ) => {
45
48
if ( ! property ) return DEFAULT_PRIMITIVE_TYPE ;
46
49
47
50
const anotherTypeGetter = specificObjectTypes [ property . type ] || getPrimitiveType ;
48
51
return getRefTypeName ( property ) || anotherTypeGetter ( property ) ;
49
52
} ;
50
53
51
- const getObjectTypeContent = properties => {
54
+ const getObjectTypeContent = ( properties ) => {
52
55
return _ . map ( properties , ( property , name ) => {
53
56
// TODO: probably nullable should'n be use as required/no-required conditions
54
57
const isRequired =
@@ -66,27 +69,27 @@ const getObjectTypeContent = properties => {
66
69
const complexTypeGetter = ( { description, ...schema } ) => getInlineParseContent ( schema ) ;
67
70
68
71
const complexSchemaParsers = {
69
- oneOf : schema => {
72
+ oneOf : ( schema ) => {
70
73
// T1 | T2
71
74
const combined = _ . map ( schema . oneOf , complexTypeGetter ) ;
72
75
return combined . join ( " | " ) ;
73
76
} ,
74
- allOf : schema => {
77
+ allOf : ( schema ) => {
75
78
// T1 & T2
76
79
return _ . map ( schema . allOf , complexTypeGetter ) . join ( " & " ) ;
77
80
} ,
78
- anyOf : schema => {
81
+ anyOf : ( schema ) => {
79
82
// T1 | T2 | (T1 & T2)
80
83
const combined = _ . map ( schema . anyOf , complexTypeGetter ) ;
81
84
return `${ combined . join ( " | " ) } ` + ( combined . length > 1 ? ` | (${ combined . join ( " & " ) } )` : "" ) ;
82
85
} ,
83
86
// TODO
84
- not : schema => {
87
+ not : ( schema ) => {
85
88
// TODO
86
89
} ,
87
90
} ;
88
91
89
- const getComplexType = schema => {
92
+ const getComplexType = ( schema ) => {
90
93
if ( schema . oneOf ) return "oneOf" ;
91
94
if ( schema . allOf ) return "allOf" ;
92
95
if ( schema . anyOf ) return "anyOf" ;
@@ -108,7 +111,7 @@ const schemaParsers = {
108
111
typeIdentifier : isIntegerEnum ? "type" : "enum" ,
109
112
name : typeName ,
110
113
description : formatDescription ( schema . description ) ,
111
- content : _ . map ( schema . enum , key => ( {
114
+ content : _ . map ( schema . enum , ( key ) => ( {
112
115
key,
113
116
type,
114
117
value : isIntegerEnum ? `${ key } ` : `"${ key } "` ,
@@ -117,7 +120,7 @@ const schemaParsers = {
117
120
} ,
118
121
object : ( schema , typeName ) => {
119
122
if ( _ . isArray ( schema . required ) && schema . properties ) {
120
- schema . required . forEach ( requiredFieldName => {
123
+ schema . required . forEach ( ( requiredFieldName ) => {
121
124
if ( schema . properties [ requiredFieldName ] ) {
122
125
schema . properties [ requiredFieldName ] . required = true ;
123
126
}
@@ -134,7 +137,7 @@ const schemaParsers = {
134
137
typeIdentifier : "interface" ,
135
138
name : typeName ,
136
139
description : formatDescription ( schema . description ) ,
137
- allFieldsAreOptional : ! _ . some ( _ . values ( typeContent ) , part => part . isRequired ) ,
140
+ allFieldsAreOptional : ! _ . some ( _ . values ( typeContent ) , ( part ) => part . isRequired ) ,
138
141
content : typeContent ,
139
142
} ;
140
143
} ,
@@ -172,7 +175,7 @@ const schemaParsers = {
172
175
} ,
173
176
} ;
174
177
175
- const checkAndFixSchema = schema => {
178
+ const checkAndFixSchema = ( schema ) => {
176
179
if ( schema . items && ! schema . type ) {
177
180
schema . type = "array" ;
178
181
}
@@ -208,10 +211,10 @@ const parseSchema = (rawSchema, typeName, formattersMap) => {
208
211
) ;
209
212
} ;
210
213
211
- const parseSchemas = components =>
214
+ const parseSchemas = ( components ) =>
212
215
_ . map ( _ . get ( components , "schemas" ) , ( schema , typeName ) => parseSchema ( schema , typeName ) ) ;
213
216
214
- const getInlineParseContent = rawTypeData =>
217
+ const getInlineParseContent = ( rawTypeData ) =>
215
218
parseSchema ( rawTypeData , null , inlineExtraFormatters ) . content ;
216
219
217
220
module . exports = {
0 commit comments