From b88f868e50e1b12569211c240518e8517f2ebe90 Mon Sep 17 00:00:00 2001 From: Simon Huang Date: Mon, 8 Dec 2025 19:27:50 +0100 Subject: [PATCH 1/4] 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/4] =?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] = { From aa87c18e114ac0d3c08dfda2bcd4f046084e935b Mon Sep 17 00:00:00 2001 From: Simon Huang Date: Mon, 8 Dec 2025 19:27:50 +0100 Subject: [PATCH 3/4] Make the logic more explicit in validation error insertion in get_open_api_path --- fastapi/openapi/utils.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 06c14861a..88786087a 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -417,10 +417,11 @@ 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 6aebae99271e280bf7d26d6c71026f2ef324f1e3 Mon Sep 17 00:00:00 2001 From: Simon-Huang-1 <72698744+Simon-Huang-1@users.noreply.github.com> Date: Tue, 9 Dec 2025 18:37:17 +0100 Subject: [PATCH 4/4] Update fastapi/openapi/utils.py Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com> --- fastapi/openapi/utils.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 88786087a..9d5c98e88 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -417,11 +417,13 @@ def get_openapi_path( openapi_response["description"] = description http422 = "422" 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") - ) - if requires_validation_error and not has_validation_response: + if ( + (all_route_params or route.body_field) # May raise validation error + and not any( # Doesn't have a defined validation error response already + status in operation["responses"] + for status in [http422, "4XX", "default"] + ) + ): operation["responses"][http422] = { "description": "Validation Error", "content": {