@@ -12,7 +12,7 @@ import {
12
12
ParsedSimpleValue ,
13
13
} from "../utils.js" ;
14
14
15
- interface TransformSchemaObjOptions extends GlobalContext {
15
+ export interface TransformSchemaObjOptions extends GlobalContext {
16
16
required : Set < string > ;
17
17
}
18
18
@@ -32,7 +32,7 @@ export function transformSchemaObjMap(obj: Record<string, any>, options: Transfo
32
32
const v = obj [ k ] ;
33
33
34
34
// 1. Add comment in jsdoc notation
35
- const comment = prepareComment ( v ) ;
35
+ const comment = prepareComment ( v , options ) ;
36
36
if ( comment ) output += comment ;
37
37
38
38
// 2. name (with “?” if optional property)
@@ -86,6 +86,10 @@ export function transformOneOf(oneOf: any, options: TransformSchemaObjOptions):
86
86
return tsUnionOf ( oneOf . map ( ( value : any ) => transformSchemaObj ( value , options ) ) ) ;
87
87
}
88
88
89
+ export function isNodeNullable ( node : any , options : TransformSchemaObjOptions ) : boolean {
90
+ return node . nullable || ( options . xNullableAsNullable && node [ "x-nullable" ] ) ;
91
+ }
92
+
89
93
/** Convert schema object to TypeScript */
90
94
export function transformSchemaObj ( node : any , options : TransformSchemaObjOptions ) : string {
91
95
const readonly = tsReadonly ( options . immutableTypes ) ;
@@ -96,7 +100,7 @@ export function transformSchemaObj(node: any, options: TransformSchemaObjOptions
96
100
const overriddenType = options . formatter && options . formatter ( node ) ;
97
101
98
102
// open nullable
99
- if ( node . nullable ) {
103
+ if ( isNodeNullable ( node , options ) ) {
100
104
output += "(" ;
101
105
}
102
106
@@ -117,13 +121,13 @@ export function transformSchemaObj(node: any, options: TransformSchemaObjOptions
117
121
break ;
118
122
}
119
123
case "const" : {
120
- output += parseSingleSimpleValue ( node . const , node . nullable ) ;
124
+ output += parseSingleSimpleValue ( node . const , isNodeNullable ( node , options ) ) ;
121
125
break ;
122
126
}
123
127
case "enum" : {
124
128
const items : Array < ParsedSimpleValue > = [ ] ;
125
129
( node . enum as unknown [ ] ) . forEach ( ( item ) => {
126
- const value = parseSingleSimpleValue ( item , node . nullable ) ;
130
+ const value = parseSingleSimpleValue ( item , isNodeNullable ( node , options ) ) ;
127
131
items . push ( value ) ;
128
132
} ) ;
129
133
output += tsUnionOf ( items ) ;
@@ -221,7 +225,7 @@ export function transformSchemaObj(node: any, options: TransformSchemaObjOptions
221
225
}
222
226
223
227
// close nullable
224
- if ( node . nullable ) {
228
+ if ( isNodeNullable ( node , options ) ) {
225
229
output += ") | null" ;
226
230
}
227
231
0 commit comments