]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
🐛 Fix openapi generation with responses kwarg (#10895)
authorFelix Fanghaenel <35657654+flxdot@users.noreply.github.com>
Sat, 12 Oct 2024 09:44:57 +0000 (11:44 +0200)
committerGitHub <noreply@github.com>
Sat, 12 Oct 2024 09:44:57 +0000 (11:44 +0200)
Co-authored-by: flxdot <felix.fanghaenel@nitrex.com>
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
Co-authored-by: Sławek Ehlert <slawomir.ehlert@gmail.com>
fastapi/routing.py
tests/test_computed_fields.py
tests/test_openapi_separate_input_output_schemas.py

index 86e30360216cb050b3debc81cf73537327e4b4fd..8ea4bb219a7daad818ac42b8dba44339f73f9273 100644 (file)
@@ -541,7 +541,9 @@ class APIRoute(routing.Route):
                     additional_status_code
                 ), f"Status code {additional_status_code} must not have a response body"
                 response_name = f"Response_{additional_status_code}_{self.unique_id}"
-                response_field = create_model_field(name=response_name, type_=model)
+                response_field = create_model_field(
+                    name=response_name, type_=model, mode="serialization"
+                )
                 response_fields[additional_status_code] = response_field
         if response_fields:
             self.response_fields: Dict[Union[int, str], ModelField] = response_fields
index 5286507b2d348057ab7f644116d3c56c7a8953d2..a1b4121688784dce8ffde8c1a5cd2b2b249dbc3e 100644 (file)
@@ -24,13 +24,18 @@ def get_client():
     def read_root() -> Rectangle:
         return Rectangle(width=3, length=4)
 
+    @app.get("/responses", responses={200: {"model": Rectangle}})
+    def read_responses() -> Rectangle:
+        return Rectangle(width=3, length=4)
+
     client = TestClient(app)
     return client
 
 
+@pytest.mark.parametrize("path", ["/", "/responses"])
 @needs_pydanticv2
-def test_get(client: TestClient):
-    response = client.get("/")
+def test_get(client: TestClient, path: str):
+    response = client.get(path)
     assert response.status_code == 200, response.text
     assert response.json() == {"width": 3, "length": 4, "area": 12}
 
@@ -58,7 +63,23 @@ def test_openapi_schema(client: TestClient):
                         }
                     },
                 }
-            }
+            },
+            "/responses": {
+                "get": {
+                    "summary": "Read Responses",
+                    "operationId": "read_responses_responses_get",
+                    "responses": {
+                        "200": {
+                            "description": "Successful Response",
+                            "content": {
+                                "application/json": {
+                                    "schema": {"$ref": "#/components/schemas/Rectangle"}
+                                }
+                            },
+                        }
+                    },
+                }
+            },
         },
         "components": {
             "schemas": {
index aeb85f735b66655787e8347080218a53221c9041..f7e045259faf7afbd713f6a2f4d505157185e977 100644 (file)
@@ -26,8 +26,8 @@ class Item(BaseModel):
 def get_app_client(separate_input_output_schemas: bool = True) -> TestClient:
     app = FastAPI(separate_input_output_schemas=separate_input_output_schemas)
 
-    @app.post("/items/")
-    def create_item(item: Item):
+    @app.post("/items/", responses={402: {"model": Item}})
+    def create_item(item: Item) -> Item:
         return item
 
     @app.post("/items-list/")
@@ -174,7 +174,23 @@ def test_openapi_schema():
                     "responses": {
                         "200": {
                             "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
+                            "content": {
+                                "application/json": {
+                                    "schema": {
+                                        "$ref": "#/components/schemas/Item-Output"
+                                    }
+                                }
+                            },
+                        },
+                        "402": {
+                            "description": "Payment Required",
+                            "content": {
+                                "application/json": {
+                                    "schema": {
+                                        "$ref": "#/components/schemas/Item-Output"
+                                    }
+                                }
+                            },
                         },
                         "422": {
                             "description": "Validation Error",
@@ -374,7 +390,19 @@ def test_openapi_schema_no_separate():
                     "responses": {
                         "200": {
                             "description": "Successful Response",
-                            "content": {"application/json": {"schema": {}}},
+                            "content": {
+                                "application/json": {
+                                    "schema": {"$ref": "#/components/schemas/Item"}
+                                }
+                            },
+                        },
+                        "402": {
+                            "description": "Payment Required",
+                            "content": {
+                                "application/json": {
+                                    "schema": {"$ref": "#/components/schemas/Item"}
+                                }
+                            },
                         },
                         "422": {
                             "description": "Validation Error",