From 886cd92b5e547a88b4b4a553d8b3f76e6456b2f8 Mon Sep 17 00:00:00 2001 From: valbort Date: Fri, 22 May 2026 13:26:21 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20double=20JSON=20parsing=20?= =?UTF-8?q?from=20request=20body?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove the 'await request.json()' call which was inadvertently left in the routing path. This ensures that the body bytes are passed directly to Pydantic's validate_json via FastAPIOptimizedJsonBytes, bypassing unnecessary Python dict allocations entirely. --- fastapi/routing.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fastapi/routing.py b/fastapi/routing.py index b3b70a9a1e..9629fa382b 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -412,19 +412,20 @@ def get_request_handler( else: body_bytes = await request.body() if body_bytes: - json_body: Any = Undefined + is_json_content = False content_type_value = request.headers.get("content-type") if not content_type_value: if not actual_strict_content_type: - json_body = await request.json() + is_json_content = True else: message = email.message.Message() message["content-type"] = content_type_value if message.get_content_maintype() == "application": subtype = message.get_content_subtype() if subtype == "json" or subtype.endswith("+json"): - json_body = await request.json() - if json_body != Undefined: + is_json_content = True + + if is_json_content: body = FastAPIOptimizedJsonBytes(body_bytes) else: body = body_bytes