From aa87c18e114ac0d3c08dfda2bcd4f046084e935b Mon Sep 17 00:00:00 2001 From: Simon Huang Date: Mon, 8 Dec 2025 19:27:50 +0100 Subject: [PATCH] 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": {