Skip to content

Commit 88b1224

Browse files
committed
fix(_json_schema): preserve None-member metadata and nest Optional tagged union
Address review on the optional-union null-ordering change: - The union unwrap loop remembered only a has_none flag and appended a hardcoded {"type": "null"}, dropping any Meta metadata carried on an Annotated[None, Meta(...)] member. Track the actual none_member subtype and emit self.to_schema(none_member) so its metadata is preserved. - Optional[tagged union] flattened the schema shape when no other members were present. Keep the nested form and emit {"anyOf": [struct_schema, <null>]} so only ordering, not shape, changes. Update test_struct_tagged_union_with_none to the nested anyOf shape and add coverage for Union[Point, Point3D, int, None] and Annotated[None, Meta(...)].
1 parent b5b078a commit 88b1224

2 files changed

Lines changed: 51 additions & 10 deletions

File tree

‎src/msgspec/_json_schema.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ def to_schema(self, t: mi.Type, check_ref: bool = True) -> dict[str, Any]:
331331
structs = {}
332332
other = []
333333
has_none = False
334+
none_member = None
334335
tag_field = None
335336
for subtype in t.types:
336337
real_type = subtype
@@ -341,6 +342,7 @@ def to_schema(self, t: mi.Type, check_ref: bool = True) -> dict[str, Any]:
341342
structs[real_type.tag] = real_type
342343
elif isinstance(real_type, mi.NoneType):
343344
has_none = True
345+
none_member = subtype
344346
else:
345347
other.append(subtype)
346348

@@ -358,21 +360,21 @@ def to_schema(self, t: mi.Type, check_ref: bool = True) -> dict[str, Any]:
358360
if options:
359361
options.append(struct_schema)
360362
if has_none:
361-
options.append({"type": "null"})
363+
options.append(self.to_schema(none_member))
362364
schema["anyOf"] = options
365+
elif has_none:
366+
schema["anyOf"] = [struct_schema, self.to_schema(none_member)]
363367
else:
364368
schema.update(struct_schema)
365-
if has_none:
366-
schema["anyOf"] = schema["anyOf"] + [{"type": "null"}]
367369
elif len(structs) == 1:
368370
_, subtype = structs.popitem()
369371
options.append(self.to_schema(subtype))
370372
if has_none:
371-
options.append({"type": "null"})
373+
options.append(self.to_schema(none_member))
372374
schema["anyOf"] = options
373375
else:
374376
if has_none:
375-
options.append({"type": "null"})
377+
options.append(self.to_schema(none_member))
376378
schema["anyOf"] = options
377379
elif isinstance(t, mi.LiteralType):
378380
schema["enum"] = sorted(t.values)

‎tests/unit/test_schema.py‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -925,12 +925,17 @@ class Point3D(Point):
925925
z: int
926926

927927
schema = msgspec.json.schema(Union[Point, Point3D, None])
928-
assert schema["anyOf"][-1] == {"type": "null"}
929-
assert schema["anyOf"][:-1] == [
930-
{"$ref": "#/$defs/Point"},
931-
{"$ref": "#/$defs/Point3D"},
928+
assert schema["anyOf"] == [
929+
{
930+
"anyOf": [{"$ref": "#/$defs/Point"}, {"$ref": "#/$defs/Point3D"}],
931+
"discriminator": {
932+
"mapping": {"Point": "#/$defs/Point", "Point3D": "#/$defs/Point3D"},
933+
"propertyName": "type",
934+
},
935+
},
936+
{"type": "null"},
932937
]
933-
assert "discriminator" in schema
938+
assert "discriminator" not in schema
934939

935940

936941
def test_struct_tagged_union_mixed_types():
@@ -979,6 +984,40 @@ class Point3D(Point):
979984
}
980985

981986

987+
def test_struct_tagged_union_with_none_and_other():
988+
class Point(msgspec.Struct, tag=True):
989+
x: int
990+
y: int
991+
992+
class Point3D(Point):
993+
z: int
994+
995+
schema = msgspec.json.schema(Union[Point, Point3D, int, None])
996+
assert schema["anyOf"] == [
997+
{"type": "integer"},
998+
{
999+
"anyOf": [{"$ref": "#/$defs/Point"}, {"$ref": "#/$defs/Point3D"}],
1000+
"discriminator": {
1001+
"mapping": {"Point": "#/$defs/Point", "Point3D": "#/$defs/Point3D"},
1002+
"propertyName": "type",
1003+
},
1004+
},
1005+
{"type": "null"},
1006+
]
1007+
assert "discriminator" not in schema
1008+
1009+
1010+
def test_optional_union_null_member_metadata_preserved():
1011+
NullWithMeta = Annotated[None, msgspec.Meta(description="explicitly unset")]
1012+
1013+
assert msgspec.json.schema(Union[int, NullWithMeta]) == {
1014+
"anyOf": [
1015+
{"type": "integer"},
1016+
{"type": "null", "description": "explicitly unset"},
1017+
],
1018+
}
1019+
1020+
9821021
def test_struct_array_union():
9831022
class Point(msgspec.Struct, array_like=True, tag=True):
9841023
x: int

0 commit comments

Comments
 (0)