Skip to content

Commit bf7fd97

Browse files
committed
deal with oneOf and anyOf's that are nullable
1 parent 4c6642e commit bf7fd97

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/Convertor.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -481,19 +481,25 @@ class Convertor {
481481
});
482482

483483
if (hasNullType) {
484-
schemaOf.forEach((obj) => {
485-
if (obj.type !== "null") {
486-
obj.nullable = true;
487-
}
488-
});
489-
const newOf = schemaOf.filter((obj) => {
490-
if (obj.type !== "null") return obj;
491-
});
484+
const nullableSchemasFiltered = schemaOf
485+
.filter((schemaContainignNull) => {
486+
if (schemaContainignNull.type !== "null") {
487+
return schemaContainignNull;
488+
}
489+
})
490+
.map((schemasNotContainingNull) => {
491+
schemasNotContainingNull.nullable = true;
492+
return schemasNotContainingNull;
493+
});
492494

493-
if (isOneOf) {
494-
schema.oneOf = newOf;
495+
if (nullableSchemasFiltered.length > 1) {
496+
if (isOneOf) schema.oneOf = nullableSchemasFiltered;
497+
else schema.anyOf = nullableSchemasFiltered;
495498
} else {
496-
schema.anyOf = newOf;
499+
if (isOneOf) delete schema.oneOf;
500+
else delete schema.anyOf;
501+
502+
Object.assign(schema, nullableSchemasFiltered[0]);
497503
}
498504
}
499505
}

0 commit comments

Comments
 (0)