Browse Source

Use default response status reasons in additional responses (#313)

* default the description of additional response to status reason phrase

* fix 404 description

* fix lint warning

* allow custom response status code
pull/330/head
Eric Du 6 years ago
committed by Sebastián Ramírez
parent
commit
5b3adfe449
  1. 6
      fastapi/openapi/utils.py
  2. 2
      tests/test_tutorial/test_additional_responses/test_tutorial001.py

6
fastapi/openapi/utils.py

@ -1,3 +1,4 @@
import http.client
from typing import Any, Dict, List, Optional, Sequence, Tuple, Type, cast from typing import Any, Dict, List, Optional, Sequence, Tuple, Type, cast
from fastapi import routing from fastapi import routing
@ -187,7 +188,10 @@ def get_openapi_path(
response.setdefault("content", {}).setdefault( response.setdefault("content", {}).setdefault(
"application/json", {} "application/json", {}
)["schema"] = response_schema )["schema"] = response_schema
response.setdefault("description", "Additional Response") status_text = http.client.responses.get(int(additional_status_code))
response.setdefault(
"description", status_text or "Additional Response"
)
operation.setdefault("responses", {})[ operation.setdefault("responses", {})[
str(additional_status_code) str(additional_status_code)
] = response ] = response

2
tests/test_tutorial/test_additional_responses/test_tutorial001.py

@ -12,7 +12,7 @@ openapi_schema = {
"get": { "get": {
"responses": { "responses": {
"404": { "404": {
"description": "Additional Response", "description": "Not Found",
"content": { "content": {
"application/json": { "application/json": {
"schema": {"$ref": "#/components/schemas/Message"} "schema": {"$ref": "#/components/schemas/Message"}

Loading…
Cancel
Save