Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/msgspec/_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ def to_schema(self, t: mi.Type, check_ref: bool = True) -> dict[str, Any]:
elif isinstance(t, mi.UnionType):
structs = {}
other = []
has_none = False
none_member = None
tag_field = None
for subtype in t.types:
Expand All @@ -341,7 +340,6 @@ def to_schema(self, t: mi.Type, check_ref: bool = True) -> dict[str, Any]:
tag_field = real_type.tag_field
structs[real_type.tag] = real_type
elif isinstance(real_type, mi.NoneType):
has_none = True
none_member = subtype
else:
other.append(subtype)
Expand All @@ -359,21 +357,21 @@ def to_schema(self, t: mi.Type, check_ref: bool = True) -> dict[str, Any]:
}
if options:
options.append(struct_schema)
if has_none:
if none_member is not None:
options.append(self.to_schema(none_member))
schema["anyOf"] = options
elif has_none:
elif none_member is not None:
schema["anyOf"] = [struct_schema, self.to_schema(none_member)]
else:
schema.update(struct_schema)
elif len(structs) == 1:
_, subtype = structs.popitem()
options.append(self.to_schema(subtype))
if has_none:
if none_member is not None:
options.append(self.to_schema(none_member))
schema["anyOf"] = options
else:
if has_none:
if none_member is not None:
options.append(self.to_schema(none_member))
schema["anyOf"] = options
elif isinstance(t, mi.LiteralType):
Expand Down
Loading