From 0b16558b04bcda04f70054db7d4f97dfce653bbd Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 12 Apr 2026 18:06:17 -0400 Subject: [PATCH] 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) --- responder/routes.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/responder/routes.py b/responder/routes.py index a2a68d2..b0881ea 100644 --- a/responder/routes.py +++ b/responder/routes.py @@ -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()