@@ -19,7 +19,7 @@ class InvalidTypeError extends Error {
1919
2020const oasExtensionPrefix = 'x-' ;
2121
22- const handleDefinition = async < T extends JSONSchema = JSONSchema > (
22+ const handleDefinition = async < T extends JSONSchema4 = JSONSchema4 > (
2323 def : JSONSchema7Definition | JSONSchema6Definition | JSONSchema4 ,
2424 schema : T
2525) => {
@@ -52,7 +52,8 @@ const handleDefinition = async <T extends JSONSchema = JSONSchema>(
5252 delete ( < any > walker . rootSchema ) . definitions ;
5353 }
5454 return walker . rootSchema ;
55- } else if ( Array . isArray ( def ) ) {
55+ }
56+ if ( Array . isArray ( def ) ) {
5657 // if it's an array, we might want to reconstruct the type;
5758 const typeArr = def ;
5859 const hasNull = typeArr . includes ( 'null' ) ;
@@ -69,7 +70,7 @@ const handleDefinition = async <T extends JSONSchema = JSONSchema>(
6970 return def ;
7071} ;
7172
72- const convert = async < T extends JSONSchema = JSONSchema > (
73+ const convert = async < T extends object = JSONSchema4 > (
7374 schema : T ,
7475 options ?: Options
7576) : Promise < OpenAPIV3 . Document > => {
@@ -108,6 +109,7 @@ function convertSchema(schema: SchemaType | undefined) {
108109 schema = convertTypes ( schema ) ;
109110 schema = rewriteConst ( schema ) ;
110111 schema = convertDependencies ( schema ) ;
112+ schema = convertNullable ( schema ) ;
111113 schema = rewriteIfThenElse ( schema ) ;
112114 schema = rewriteExclusiveMinMax ( schema ) ;
113115 schema = convertExamples ( schema ) ;
@@ -196,6 +198,30 @@ function convertDependencies(schema: SchemaType) {
196198 return schema ;
197199}
198200
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+
199225function convertTypes ( schema : SchemaType ) {
200226 if ( typeof schema !== 'object' ) {
201227 return schema ;
@@ -300,11 +326,11 @@ function rewriteIfThenElse(schema: SchemaType) {
300326function rewriteExclusiveMinMax ( schema : SchemaType ) {
301327 if ( typeof schema . exclusiveMaximum === 'number' ) {
302328 schema . maximum = schema . exclusiveMaximum ;
303- schema . exclusiveMaximum = true ;
329+ ( schema as JSONSchema4 ) . exclusiveMaximum = true ;
304330 }
305331 if ( typeof schema . exclusiveMinimum === 'number' ) {
306332 schema . minimum = schema . exclusiveMinimum ;
307- schema . exclusiveMinimum = true ;
333+ ( schema as JSONSchema4 ) . exclusiveMinimum = true ;
308334 }
309335 return schema ;
310336}
0 commit comments