From 932659f49ba5b27487c0cd3380d9a2c4eab7a8c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Aguiar?= Date: Fri, 1 Mar 2024 19:13:37 -0300 Subject: [PATCH] feat(openapi.utils): checks whether to superimpose an additional response's media_type over the default route's response class's media_type --- fastapi/openapi/utils.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 947eca948..6489441d8 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -345,11 +345,15 @@ def get_openapi_path( ) else: response_schema = {} - operation.setdefault("responses", {}).setdefault( - status_code, {} - ).setdefault("content", {}).setdefault(route_response_media_type, {})[ - "schema" - ] = response_schema + + route_responses = dict((str(k), v) for k, v in route.responses.items()) + if status_code not in route_responses or not route_responses.get(status_code).get('superimpose'): + operation.setdefault("responses", {}).setdefault( + status_code, {} + ).setdefault("content", {}).setdefault(route_response_media_type, {})[ + "schema" + ] = response_schema + if route.responses: operation_responses = operation.setdefault("responses", {}) for ( @@ -358,6 +362,7 @@ def get_openapi_path( ) in route.responses.items(): process_response = additional_response.copy() process_response.pop("model", None) + process_response.pop("superimpose", None) status_code_key = str(additional_status_code).upper() if status_code_key == "DEFAULT": status_code_key = "default"