From 55c4b5fb0bd260765b6eedbf8c200caa3cdc670e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Fri, 30 Aug 2019 16:34:47 -0500 Subject: [PATCH] :bug: Fix "default" extra response with extra status codes (#489) * :bug: Fix lowercase "default" extra response * :bug: Fix model for responses, to allow "default" plus status codes * :white_check_mark: Add test for "default" extra response --- fastapi/openapi/models.py | 6 +----- fastapi/openapi/utils.py | 9 +++++---- tests/test_additional_responses_router.py | 2 ++ 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/fastapi/openapi/models.py b/fastapi/openapi/models.py index ddc5c6847..3dd9f04dc 100644 --- a/fastapi/openapi/models.py +++ b/fastapi/openapi/models.py @@ -210,10 +210,6 @@ class Response(BaseModel): links: Optional[Dict[str, Union[Link, Reference]]] = None -class Responses(BaseModel): - default: Response - - class Operation(BaseModel): tags: Optional[List[str]] = None summary: Optional[str] = None @@ -222,7 +218,7 @@ class Operation(BaseModel): operationId: Optional[str] = None parameters: Optional[List[Union[Parameter, Reference]]] = None requestBody: Optional[Union[RequestBody, Reference]] = None - responses: Union[Responses, Dict[str, Response]] + responses: Dict[str, Response] # Workaround OpenAPI recursive reference callbacks: Optional[Dict[str, Union[Dict[str, Any], Reference]]] = None deprecated: Optional[bool] = None diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index 2ee8c241c..c3cc120fd 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -49,7 +49,7 @@ status_code_ranges: Dict[str, str] = { "3XX": "Redirection", "4XX": "Client Error", "5XX": "Server Error", - "default": "Default Response", + "DEFAULT": "Default Response", } @@ -205,9 +205,10 @@ def get_openapi_path( response.setdefault( "description", status_text or "Additional Response" ) - operation.setdefault("responses", {})[ - str(additional_status_code).upper() - ] = response + status_code_key = str(additional_status_code).upper() + if status_code_key == "DEFAULT": + status_code_key = "default" + operation.setdefault("responses", {})[status_code_key] = response status_code = str(route.status_code) response_schema = {"type": "string"} if lenient_issubclass(route.response_class, JSONResponse): diff --git a/tests/test_additional_responses_router.py b/tests/test_additional_responses_router.py index ce66ead7e..028026e67 100644 --- a/tests/test_additional_responses_router.py +++ b/tests/test_additional_responses_router.py @@ -26,6 +26,7 @@ async def b(): responses={ "400": {"description": "Error with str"}, "5xx": {"description": "Error with range, lower"}, + "default": {"description": "A default response"}, }, ) async def c(): @@ -74,6 +75,7 @@ openapi_schema = { "description": "Successful Response", "content": {"application/json": {"schema": {}}}, }, + "default": {"description": "A default response"}, }, "summary": "C", "operationId": "c_c_get",