@@ -19,7 +19,7 @@ class InvalidTypeError extends Error {
19
19
20
20
const oasExtensionPrefix = 'x-' ;
21
21
22
- const handleDefinition = async < T extends JSONSchema = JSONSchema > (
22
+ const handleDefinition = async < T extends JSONSchema4 = JSONSchema4 > (
23
23
def : JSONSchema7Definition | JSONSchema6Definition | JSONSchema4 ,
24
24
schema : T
25
25
) => {
@@ -52,7 +52,8 @@ const handleDefinition = async <T extends JSONSchema = JSONSchema>(
52
52
delete ( < any > walker . rootSchema ) . definitions ;
53
53
}
54
54
return walker . rootSchema ;
55
- } else if ( Array . isArray ( def ) ) {
55
+ }
56
+ if ( Array . isArray ( def ) ) {
56
57
// if it's an array, we might want to reconstruct the type;
57
58
const typeArr = def ;
58
59
const hasNull = typeArr . includes ( 'null' ) ;
@@ -69,7 +70,7 @@ const handleDefinition = async <T extends JSONSchema = JSONSchema>(
69
70
return def ;
70
71
} ;
71
72
72
- const convert = async < T extends JSONSchema = JSONSchema > (
73
+ const convert = async < T extends object = JSONSchema4 > (
73
74
schema : T ,
74
75
options ?: Options
75
76
) : Promise < OpenAPIV3 . Document > => {
@@ -108,6 +109,7 @@ function convertSchema(schema: SchemaType | undefined) {
108
109
schema = convertTypes ( schema ) ;
109
110
schema = rewriteConst ( schema ) ;
110
111
schema = convertDependencies ( schema ) ;
112
+ schema = convertNullable ( schema ) ;
111
113
schema = rewriteIfThenElse ( schema ) ;
112
114
schema = rewriteExclusiveMinMax ( schema ) ;
113
115
schema = convertExamples ( schema ) ;
@@ -196,6 +198,30 @@ function convertDependencies(schema: SchemaType) {
196
198
return schema ;
197
199
}
198
200
201
+ function convertNullable ( schema : SchemaType ) {
202
+ for ( const key of [ 'oneOf' , 'anyOf' ] as const ) {
203
+ const schemas = schema [ key ] as JSONSchema4 [ ] ;
204
+ if ( ! Array . isArray ( schemas ) ) {
205
+ return schema ;
206
+ }
207
+
208
+ const hasNullable = schemas . some ( ( item ) => item . type === 'null' ) ;
209
+
210
+ if ( ! hasNullable ) {
211
+ return schema ;
212
+ }
213
+
214
+ const filtered = schemas . filter ( ( l ) => l . type !== 'null' ) ;
215
+ for ( const schemaEntry of filtered ) {
216
+ schemaEntry . nullable = true ;
217
+ }
218
+
219
+ schema [ key ] = filtered ;
220
+ }
221
+
222
+ return schema ;
223
+ }
224
+
199
225
function convertTypes ( schema : SchemaType ) {
200
226
if ( typeof schema !== 'object' ) {
201
227
return schema ;
@@ -300,11 +326,11 @@ function rewriteIfThenElse(schema: SchemaType) {
300
326
function rewriteExclusiveMinMax ( schema : SchemaType ) {
301
327
if ( typeof schema . exclusiveMaximum === 'number' ) {
302
328
schema . maximum = schema . exclusiveMaximum ;
303
- schema . exclusiveMaximum = true ;
329
+ ( schema as JSONSchema4 ) . exclusiveMaximum = true ;
304
330
}
305
331
if ( typeof schema . exclusiveMinimum === 'number' ) {
306
332
schema . minimum = schema . exclusiveMinimum ;
307
- schema . exclusiveMinimum = true ;
333
+ ( schema as JSONSchema4 ) . exclusiveMinimum = true ;
308
334
}
309
335
return schema ;
310
336
}
0 commit comments