Browse Source

🐛 Fix "default" extra response with extra status codes (#489)

* 🐛 Fix lowercase "default" extra response

* 🐛 Fix model for responses, to allow "default" plus status codes

*  Add test for "default" extra response
pull/438/merge
Sebastián Ramírez 6 years ago
committed by GitHub
parent
commit
55c4b5fb0b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      fastapi/openapi/models.py
  2. 9
      fastapi/openapi/utils.py
  3. 2
      tests/test_additional_responses_router.py

6
fastapi/openapi/models.py

@ -210,10 +210,6 @@ class Response(BaseModel):
links: Optional[Dict[str, Union[Link, Reference]]] = None links: Optional[Dict[str, Union[Link, Reference]]] = None
class Responses(BaseModel):
default: Response
class Operation(BaseModel): class Operation(BaseModel):
tags: Optional[List[str]] = None tags: Optional[List[str]] = None
summary: Optional[str] = None summary: Optional[str] = None
@ -222,7 +218,7 @@ class Operation(BaseModel):
operationId: Optional[str] = None operationId: Optional[str] = None
parameters: Optional[List[Union[Parameter, Reference]]] = None parameters: Optional[List[Union[Parameter, Reference]]] = None
requestBody: Optional[Union[RequestBody, Reference]] = None requestBody: Optional[Union[RequestBody, Reference]] = None
responses: Union[Responses, Dict[str, Response]] responses: Dict[str, Response]
# Workaround OpenAPI recursive reference # Workaround OpenAPI recursive reference
callbacks: Optional[Dict[str, Union[Dict[str, Any], Reference]]] = None callbacks: Optional[Dict[str, Union[Dict[str, Any], Reference]]] = None
deprecated: Optional[bool] = None deprecated: Optional[bool] = None

9
fastapi/openapi/utils.py

@ -49,7 +49,7 @@ status_code_ranges: Dict[str, str] = {
"3XX": "Redirection", "3XX": "Redirection",
"4XX": "Client Error", "4XX": "Client Error",
"5XX": "Server Error", "5XX": "Server Error",
"default": "Default Response", "DEFAULT": "Default Response",
} }
@ -205,9 +205,10 @@ def get_openapi_path(
response.setdefault( response.setdefault(
"description", status_text or "Additional Response" "description", status_text or "Additional Response"
) )
operation.setdefault("responses", {})[ status_code_key = str(additional_status_code).upper()
str(additional_status_code).upper() if status_code_key == "DEFAULT":
] = response status_code_key = "default"
operation.setdefault("responses", {})[status_code_key] = response
status_code = str(route.status_code) status_code = str(route.status_code)
response_schema = {"type": "string"} response_schema = {"type": "string"}
if lenient_issubclass(route.response_class, JSONResponse): if lenient_issubclass(route.response_class, JSONResponse):

2
tests/test_additional_responses_router.py

@ -26,6 +26,7 @@ async def b():
responses={ responses={
"400": {"description": "Error with str"}, "400": {"description": "Error with str"},
"5xx": {"description": "Error with range, lower"}, "5xx": {"description": "Error with range, lower"},
"default": {"description": "A default response"},
}, },
) )
async def c(): async def c():
@ -74,6 +75,7 @@ openapi_schema = {
"description": "Successful Response", "description": "Successful Response",
"content": {"application/json": {"schema": {}}}, "content": {"application/json": {"schema": {}}},
}, },
"default": {"description": "A default response"},
}, },
"summary": "C", "summary": "C",
"operationId": "c_c_get", "operationId": "c_c_get",

Loading…
Cancel
Save