]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:sparkles: Use default response status reasons in additional responses (#313)
authorEric Du <eric@honovation.com>
Tue, 18 Jun 2019 07:46:57 +0000 (15:46 +0800)
committerSebastián Ramírez <tiangolo@gmail.com>
Tue, 18 Jun 2019 07:46:57 +0000 (09:46 +0200)
* default the description of additional response to status reason phrase

* fix 404 description

* fix lint warning

* allow custom response status code

fastapi/openapi/utils.py
tests/test_tutorial/test_additional_responses/test_tutorial001.py

index 26d491beaeedb6eeeb45f83ec9bb700608d64771..6f741c6bcdb3ed78f538b41f5a7523720b92e6fe 100644 (file)
@@ -1,3 +1,4 @@
+import http.client
 from typing import Any, Dict, List, Optional, Sequence, Tuple, Type, cast
 
 from fastapi import routing
@@ -187,7 +188,10 @@ def get_openapi_path(
                         response.setdefault("content", {}).setdefault(
                             "application/json", {}
                         )["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", {})[
                         str(additional_status_code)
                     ] = response
index 7ab350e8dc40c70eac56f844216dadbf3f60db53..5036afda2e8446cb2fc1282e60577283959c884b 100644 (file)
@@ -12,7 +12,7 @@ openapi_schema = {
             "get": {
                 "responses": {
                     "404": {
-                        "description": "Additional Response",
+                        "description": "Not Found",
                         "content": {
                             "application/json": {
                                 "schema": {"$ref": "#/components/schemas/Message"}