Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions msgspec/_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ def to_schema(self, t: mi.Type, check_ref: bool = True) -> dict[str, Any]:
if t.min_length is not None:
schema["minProperties"] = t.min_length
elif isinstance(t, mi.UnionType):
# don't wrap simple optionals in an anyOf
if len(t.types) == 2 and t.includes_none:
return (
self.to_schema(t.types[0])
if not isinstance(t.types[0], mi.NoneType)
else self.to_schema(t.types[1])
)
structs = {}
other = []
tag_field = None
Expand Down
4 changes: 2 additions & 2 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class Polygon(msgspec.Struct):
"items": {"$ref": "#/$defs/Point"},
},
"name": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"type": "string",
"default": None,
},
"metadata": {
Expand Down Expand Up @@ -722,7 +722,7 @@ class Polygon:
"items": {"$ref": "#/$defs/Point"},
},
"name": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"type": "string",
"default": None,
},
"metadata": {
Expand Down