@@ -38,7 +38,8 @@ export const normalizeIdentifier = (val: string, asVar = false) => {
3838const makeInlineEnum = ( s : OAS3 ) => {
3939 if ( ! s . enum ) return undefined
4040
41- const values = filterEmpty ( s . enum )
41+ // https://swagger.io/docs/specification/v3_0/data-models/enums/
42+ const values = filterEmpty ( s . enum ) as Array < string | number | boolean >
4243 if ( ! values . length ) return undefined
4344
4445 if ( ! s . type ) {
@@ -53,7 +54,7 @@ const makeInlineEnum = (s: OAS3) => {
5354 }
5455
5556 if ( s . type === "number" ) {
56- const tokens = uniq ( values ) . map ( ( x ) => f . createNumericLiteral ( x ) )
57+ const tokens = uniq ( values ) . map ( ( x ) => f . createNumericLiteral ( x as number ) )
5758 return f . createUnionTypeNode ( tokens . map ( ( x ) => f . createLiteralTypeNode ( x ) ) )
5859 }
5960
@@ -81,6 +82,13 @@ const makeObject = (ctx: Context, s: OAS3): ts.TypeNode => {
8182 return f . createKeywordTypeNode ( ts . SyntaxKind . ObjectKeyword )
8283}
8384
85+ const makeLiteralUnion = ( ctx : Context , types : string [ ] ) : ts . TypeNode => {
86+ const tokens = types . map ( ( x ) => makeType ( ctx , { type : x } ) )
87+ const hasUnknown = tokens . some ( ( x ) => x . kind === ts . SyntaxKind . UnknownKeyword )
88+ if ( hasUnknown ) return f . createKeywordTypeNode ( ts . SyntaxKind . UnknownKeyword )
89+ return f . createUnionTypeNode ( tokens )
90+ }
91+
8492export const makeType = ( ctx : Context , s ?: Referenced < OAS3 > ) : ts . TypeNode => {
8593 const mk = makeType . bind ( null , ctx )
8694
@@ -108,6 +116,7 @@ export const makeType = (ctx: Context, s?: Referenced<OAS3>): ts.TypeNode => {
108116
109117 if ( "enum" in s && s . enum && ! Array . isArray ( s . type ) ) {
110118 const isArray = s . type === "array"
119+ // @ts -expect-error todo: fix type
111120 const t = makeInlineEnum ( isArray ? { ...s , type : s . items ?. type } : s )
112121 if ( t ) return isArray ? f . createArrayTypeNode ( t ) : t
113122 }
@@ -123,26 +132,15 @@ export const makeType = (ctx: Context, s?: Referenced<OAS3>): ts.TypeNode => {
123132 }
124133
125134 if ( "type" in s ) {
126- // openapi v3.1 can have type as array
127- if ( Array . isArray ( s . type ) ) {
128- const types : OAS3 [ ] = [ ]
129- for ( const type of s . type ) {
130- if ( type === "null" ) types . push ( { type : "null" } )
131- else types . push ( { ...s , type } )
132- }
133-
134- return mk ( { oneOf : types } )
135- }
136-
137135 let t : ts . TypeNode
138136 // if (s.type === "object") t = f.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)
139137 if ( s . type === "object" ) t = makeObject ( ctx , s )
140138 else if ( s . type === "boolean" ) t = f . createKeywordTypeNode ( ts . SyntaxKind . BooleanKeyword )
141139 else if ( s . type === "number" ) t = f . createKeywordTypeNode ( ts . SyntaxKind . NumberKeyword )
142140 else if ( s . type === "string" ) t = f . createKeywordTypeNode ( ts . SyntaxKind . StringKeyword )
143- else if ( s . type === "array" ) t = f . createArrayTypeNode ( mk ( s . items ) )
144141 else if ( s . type === "null" ) t = f . createLiteralTypeNode ( f . createNull ( ) )
145- else if ( isArray ( s . type ) ) t = f . createUnionTypeNode ( s . type . map ( ( x ) => mk ( { type : x } ) ) )
142+ else if ( isArray ( s . type ) ) t = makeLiteralUnion ( ctx , s . type )
143+ else if ( s . type === "array" && ! isBoolean ( s . items ) ) t = f . createArrayTypeNode ( mk ( s . items ) )
146144 else {
147145 console . warn ( `makeType: unknown type "${ s . type } "` )
148146 return f . createKeywordTypeNode ( ts . SyntaxKind . UnknownKeyword )
@@ -153,7 +151,12 @@ export const makeType = (ctx: Context, s?: Referenced<OAS3>): ts.TypeNode => {
153151 if ( s . format === "date-time" && ctx . parseDates ) t = f . createTypeReferenceNode ( "Date" )
154152 }
155153
156- return s . nullable ? f . createUnionTypeNode ( [ t , f . createLiteralTypeNode ( f . createNull ( ) ) ] ) : t
154+ // openapi 3.0 has nullable as boolean property
155+ if ( "nullable" in s && s . nullable ) {
156+ return f . createUnionTypeNode ( [ t , f . createLiteralTypeNode ( f . createNull ( ) ) ] )
157+ }
158+
159+ return t
157160 }
158161
159162 // throw new Error(`makeType: unknown schema ${JSON.stringify(s)}`)
0 commit comments