From b88f868e50e1b12569211c240518e8517f2ebe90 Mon Sep 17 00:00:00 2001 From: Simon Huang Date: Mon, 8 Dec 2025 19:27:50 +0100 Subject: [PATCH 1/2] Make the logic more explicit in validation error insertion in get_open_api_path --- fastapi/openapi/utils.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 06c14861a..53333b664 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -417,10 +417,12 @@ def get_openapi_path( openapi_response["description"] = description http422 = "422" all_route_params = get_flat_params(route.dependant) - if (all_route_params or route.body_field) and not any( - status in operation["responses"] - for status in [http422, "4XX", "default"] - ): + requires_validation_error = bool(all_route_params or route.body_field) + has_validation_response = any( + status in operation["responses"] + for status in ("422", "4XX", "default") + ) + if requires_validation_error and not has_validation_response: operation["responses"][http422] = { "description": "Validation Error", "content": { From e7f361cacb8f0715c2ae6021511176fd46a88806 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Mon, 8 Dec 2025 18:32:30 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=8E=A8=20Auto=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/openapi/utils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 53333b664..88786087a 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -419,8 +419,7 @@ def get_openapi_path( all_route_params = get_flat_params(route.dependant) requires_validation_error = bool(all_route_params or route.body_field) has_validation_response = any( - status in operation["responses"] - for status in ("422", "4XX", "default") + status in operation["responses"] for status in ("422", "4XX", "default") ) if requires_validation_error and not has_validation_response: operation["responses"][http422] = {