@@ -19,26 +19,28 @@ function getTypeFromSchema(
19
19
const refType = schema . $ref . split ( "/" ) . pop ( ) ;
20
20
return sanitizeTypeName ( refType as string ) ;
21
21
}
22
+ const nullable = schema . nullable ? " | null" : "" ;
22
23
23
24
// Handle enum types properly
24
25
if ( schema . enum ) {
25
- return schema . enum . map ( ( e ) => ( typeof e === "string" ? `'${ e } '` : e ) ) . join ( " | " ) ;
26
+ return schema . enum . map ( ( e ) => ( typeof e === "string" ? `'${ e } '` : e ) ) . join ( " | " ) + nullable ;
26
27
}
27
28
28
29
switch ( schema . type ) {
29
30
case "string" :
30
31
if ( "format" in schema && schema . format === "binary" ) {
31
- return " string | { name?: string; type?: string; uri: string }" ;
32
+ return ` string | { name?: string; type?: string; uri: string }${ nullable } ` ;
32
33
}
33
- return "string" ;
34
+
35
+ return `string${ nullable } ` ;
34
36
case "number" :
35
37
case "integer" :
36
- return " number" ;
38
+ return ` number${ nullable } ` ;
37
39
case "boolean" :
38
- return " boolean" ;
40
+ return ` boolean${ nullable } ` ;
39
41
case "array" : {
40
42
const itemType = getTypeFromSchema ( schema . items , context ) ;
41
- return `Array<${ itemType } >` ;
43
+ return `Array<${ itemType } >${ nullable } ` ;
42
44
}
43
45
case "object" :
44
46
if ( schema . properties ) {
@@ -50,18 +52,18 @@ function getTypeFromSchema(
50
52
return ` ${ safeName } ${ isRequired ? "" : "?" } : ${ propertyType } ;` ;
51
53
} )
52
54
. join ( "\n" ) ;
53
- return `{\n ${ properties } \n}` ;
55
+ return `{${ properties } \n} ${ nullable } ` ;
54
56
}
55
57
if ( schema . additionalProperties ) {
56
58
const valueType =
57
59
typeof schema . additionalProperties === "boolean"
58
60
? "any"
59
61
: getTypeFromSchema ( schema . additionalProperties , context ) ;
60
- return `Record<string, ${ valueType } >` ;
62
+ return `Record<string, ${ valueType } >${ nullable } ` ;
61
63
}
62
- return " Record<string, any>" ;
64
+ return ` Record<string, any>${ nullable } ` ;
63
65
default :
64
- return " any" ;
66
+ return ` any${ nullable } ` ;
65
67
}
66
68
}
67
69
@@ -114,9 +116,9 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
114
116
if ( operationObject . requestBody ) {
115
117
const content = ( operationObject . requestBody as OpenAPIV3 . RequestBodyObject ) . content ;
116
118
const jsonContent =
119
+ content [ "application/ld+json" ] ??
117
120
content [ "application/json" ] ??
118
121
content [ "multipart/form-data" ] ??
119
- content [ "application/ld+json" ] ??
120
122
content [ "application/octet-stream" ] ;
121
123
if ( jsonContent ?. schema ) {
122
124
const typeName = `${ operationId } Request` ;
@@ -129,8 +131,8 @@ export function generateTypeDefinitions(spec: OpenAPIV3.Document): string {
129
131
for ( const [ code , response ] of Object . entries ( operationObject . responses ) ) {
130
132
const responseObj = response as OpenAPIV3 . ResponseObject ;
131
133
const content =
132
- responseObj . content ?. [ "application/json" ] ??
133
134
responseObj . content ?. [ "application/ld+json" ] ??
135
+ responseObj . content ?. [ "application/json" ] ??
134
136
responseObj . content ?. [ "application/octet-stream" ] ;
135
137
if ( content ?. schema ) {
136
138
const typeName = `${ operationId } Response${ code } ` ;
0 commit comments