Fix Pydantic model validation assuming dict body

Request validation would crash with **body if the body was a list.
Response validation would crash the same way. Now checks isinstance
before unpacking. Also added DELETE to request validation methods.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-12 18:06:17 -04:00
parent 2440d4caed
commit 0b16558b04
+4 -2
View File
@@ -144,9 +144,11 @@ class Route(BaseRoute):
# Auto-validate request body with Pydantic model
req_model = getattr(self.endpoint, "_request_model", None)
if req_model is not None and request.method in ("post", "put", "patch"):
if req_model is not None and request.method in ("post", "put", "patch", "delete"):
try:
body = await request.media()
if not isinstance(body, dict):
raise TypeError("Request body must be a JSON object")
req_model(**body)
except Exception as exc:
response.status_code = 422
@@ -188,7 +190,7 @@ class Route(BaseRoute):
# Auto-serialize response with Pydantic model
resp_model = getattr(self.endpoint, "_response_model", None)
if resp_model is not None and response.media is not None:
if resp_model is not None and isinstance(response.media, dict):
try:
validated = resp_model(**response.media)
response.media = validated.model_dump()