From bc533c983d7ac431033d6e112324a3bd9fbe55f8 Mon Sep 17 00:00:00 2001 From: Arseny Leontev Date: Sat, 24 Sep 2022 01:20:32 +0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8Remove=20empty=20responses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/openapi/utils.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 86e15b46d..71429e913 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -193,6 +193,7 @@ def get_openapi_path( *, route: routing.APIRoute, model_name_map: Dict[type, str], operation_ids: Set[str] ) -> Tuple[Dict[str, Any], Dict[str, Any], Dict[str, Any]]: path = {} + empty_responses: List[int] = [] security_schemes: Dict[str, Any] = {} definitions: Dict[str, Any] = {} assert route.methods is not None, "Methods must be a list" @@ -296,7 +297,7 @@ def get_openapi_path( additional_response, ) in route.responses.items(): process_response = additional_response.copy() - process_response.pop("model", None) + process_rseponse_model = process_response.pop("model", None) status_code_key = str(additional_status_code).upper() if status_code_key == "DEFAULT": status_code_key = "default" @@ -306,6 +307,8 @@ def get_openapi_path( assert isinstance( process_response, dict ), "An additional response must be a dict" + if not process_response and not process_rseponse_model: + empty_responses.append(additional_status_code) field = route.response_fields.get(additional_status_code) additional_field_schema: Optional[Dict[str, Any]] = None if field: @@ -354,6 +357,9 @@ def get_openapi_path( ) if route.openapi_extra: deep_dict_update(operation, route.openapi_extra) + if empty_responses: + for response_status_code in empty_responses: + del operation["responses"][str(response_status_code)] path[method.lower()] = operation return path, security_schemes, definitions