mirror of
https://github.com/kennethreitz/responder.git
synced 2026-06-05 06:46:14 +00:00
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:
+4
-2
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user