Skip to content

Commit 5c9e532

Browse files
committed
remove and compress oneOf/anyOf with null types
1 parent 8014c9c commit 5c9e532

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/Convertor.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class Convertor {
119119
this.removeEmptyRequired(schema)
120120
this.convertNullProperty(schema)
121121
this.convertDefaultValues(schema)
122+
this.convertOneOfAnyOfNulls(schema)
122123
this.removeInvalidFields(schema)
123124
}
124125

@@ -414,6 +415,35 @@ class Convertor {
414415
schema.anyOf = anyOf
415416
}
416417
}
418+
419+
convertOneOfAnyOfNulls(schema) {
420+
if (schema.oneOf || schema.anyOf) {
421+
const isOneOf = Boolean(schema.oneOf)
422+
const schemaOf = schema.oneOf || schema.anyOf
423+
const hasNullType = schemaOf.some(obj => {
424+
if (obj.type === 'null')
425+
return true
426+
})
427+
428+
if (hasNullType) {
429+
schemaOf.forEach(obj => {
430+
if (obj.type !== 'null') {
431+
obj.nullable = true
432+
}
433+
})
434+
const newOf = schemaOf.filter(obj => {
435+
if (obj.type !== 'null')
436+
return obj
437+
})
438+
439+
if (isOneOf) {
440+
schema.oneOf = newOf
441+
} else {
442+
schema.anyOf = newOf
443+
}
444+
}
445+
}
446+
}
417447
}
418448

419449
module.exports = Convertor

0 commit comments

Comments
 (0)