@@ -94,23 +94,38 @@ export function transformSchemaObjectWithComposition(
94
94
! ( "additionalProperties" in schemaObject )
95
95
) {
96
96
// hoist enum to top level if string/number enum and option is enabled
97
- if ( options . ctx . enum && schemaObject . enum . every ( ( v ) => typeof v === "string" || typeof v === "number" ) ) {
97
+ if (
98
+ options . ctx . enum &&
99
+ schemaObject . enum . every ( ( v ) => typeof v === "string" || typeof v === "number" || v === null )
100
+ ) {
98
101
let enumName = parseRef ( options . path ?? "" ) . pointer . join ( "/" ) ;
99
102
// allow #/components/schemas to have simpler names
100
103
enumName = enumName . replace ( "components/schemas" , "" ) ;
101
104
const metadata = schemaObject . enum . map ( ( _ , i ) => ( {
102
105
name : schemaObject [ "x-enum-varnames" ] ?. [ i ] ?? schemaObject [ "x-enumNames" ] ?. [ i ] ,
103
106
description : schemaObject [ "x-enum-descriptions" ] ?. [ i ] ?? schemaObject [ "x-enumDescriptions" ] ?. [ i ] ,
104
107
} ) ) ;
105
- const enumType = tsEnum ( enumName , schemaObject . enum as ( string | number ) [ ] , metadata , {
108
+
109
+ // enums can contain null values, but dont want to output them
110
+ let hasNull = false ;
111
+ const validSchemaEnums = schemaObject . enum . filter ( ( enumValue ) => {
112
+ if ( enumValue === null ) {
113
+ hasNull = true ;
114
+ return false ;
115
+ }
116
+
117
+ return true ;
118
+ } ) ;
119
+ const enumType = tsEnum ( enumName , validSchemaEnums as ( string | number ) [ ] , metadata , {
106
120
shouldCache : options . ctx . dedupeEnums ,
107
121
export : true ,
108
122
// readonly: TS enum do not support the readonly modifier
109
123
} ) ;
110
124
if ( ! options . ctx . injectFooter . includes ( enumType ) ) {
111
125
options . ctx . injectFooter . push ( enumType ) ;
112
126
}
113
- return ts . factory . createTypeReferenceNode ( enumType . name ) ;
127
+ const ref = ts . factory . createTypeReferenceNode ( enumType . name ) ;
128
+ return hasNull ? tsUnion ( [ ref , NULL ] ) : ref ;
114
129
}
115
130
const enumType = schemaObject . enum . map ( tsLiteral ) ;
116
131
if (
0 commit comments