Skip to content

Commit e865197

Browse files
committed
Fixed an issue where defs are not included in the schema for msgspec.
1 parent f19022a commit e865197

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

src/quart_schema/conversion.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@ def model_schema(
208208
)
209209
elif _use_msgspec(model_class, preference):
210210
_, schema = schema_components([model_class], ref_template=MSGSPEC_REF_TEMPLATE)
211-
return list(schema.values())[0]
211+
schema_name = list(schema.keys())[0]
212+
main_schema = schema.pop(schema_name)
213+
if schema: # Remaining schemas (like Attribute) become $defs
214+
main_schema["$defs"] = schema
215+
return main_schema
212216
elif not PYDANTIC_INSTALLED and not MSGSPEC_INSTALLED:
213217
raise TypeError(
214218
f"Cannot create schema for {model_class} - try installing msgspec or pydantic"

tests/test_conversion.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import Any, TypedDict
2+
from typing import Any, Generic, TypedDict, TypeVar
33

44
import pytest
55
from attrs import define
@@ -132,6 +132,62 @@ def test_model_schema_msgspec(type_: TestType, preference: str) -> None:
132132
assert schema == expected
133133

134134

135+
A = TypeVar("A")
136+
M = TypeVar("M")
137+
138+
139+
class Modifier(TypedDict):
140+
mod: int
141+
142+
143+
class Attribute(TypedDict):
144+
title: str
145+
146+
147+
class Resource(TypedDict, Generic[A, M]):
148+
foo: str
149+
attribute: A
150+
modifier: M
151+
152+
153+
def test_nested_generic_ref_included():
154+
schema = model_schema(
155+
Resource[Attribute, Modifier],
156+
preference="msgspec",
157+
)
158+
159+
assert schema == {
160+
"title": "Resource[Attribute, Modifier]",
161+
"type": "object",
162+
"properties": {
163+
"attribute": {
164+
"$ref": "#/components/schemas/Attribute",
165+
},
166+
"modifier": {
167+
"$ref": "#/components/schemas/Modifier",
168+
},
169+
"foo": {"type": "string"},
170+
},
171+
"required": ["attribute", "foo", "modifier"],
172+
"$defs": {
173+
"Attribute": {
174+
"properties": {
175+
"title": {"type": "string"},
176+
},
177+
"required": ["title"],
178+
"title": "Attribute",
179+
"type": "object",
180+
},
181+
"Modifier": {
182+
"properties": {"mod": {"type": "integer"}},
183+
"required": ["mod"],
184+
"title": "Modifier",
185+
"type": "object",
186+
},
187+
},
188+
}
189+
190+
135191
@define
136192
class AHeaders:
137193
x_info: str

0 commit comments

Comments
 (0)