Skip to content

Commit e049fc4

Browse files
flxdotflxdotsvlandegslafs
authored
🐛 Fix openapi generation with responses kwarg (fastapi#10895)
Co-authored-by: flxdot <[email protected]> Co-authored-by: Sofie Van Landeghem <[email protected]> Co-authored-by: Sławek Ehlert <[email protected]>
1 parent b29cf16 commit e049fc4

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

fastapi/routing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,9 @@ def __init__(
541541
additional_status_code
542542
), f"Status code {additional_status_code} must not have a response body"
543543
response_name = f"Response_{additional_status_code}_{self.unique_id}"
544-
response_field = create_model_field(name=response_name, type_=model)
544+
response_field = create_model_field(
545+
name=response_name, type_=model, mode="serialization"
546+
)
545547
response_fields[additional_status_code] = response_field
546548
if response_fields:
547549
self.response_fields: Dict[Union[int, str], ModelField] = response_fields

tests/test_computed_fields.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ def area(self) -> int:
2424
def read_root() -> Rectangle:
2525
return Rectangle(width=3, length=4)
2626

27+
@app.get("/responses", responses={200: {"model": Rectangle}})
28+
def read_responses() -> Rectangle:
29+
return Rectangle(width=3, length=4)
30+
2731
client = TestClient(app)
2832
return client
2933

3034

35+
@pytest.mark.parametrize("path", ["/", "/responses"])
3136
@needs_pydanticv2
32-
def test_get(client: TestClient):
33-
response = client.get("/")
37+
def test_get(client: TestClient, path: str):
38+
response = client.get(path)
3439
assert response.status_code == 200, response.text
3540
assert response.json() == {"width": 3, "length": 4, "area": 12}
3641

@@ -58,7 +63,23 @@ def test_openapi_schema(client: TestClient):
5863
}
5964
},
6065
}
61-
}
66+
},
67+
"/responses": {
68+
"get": {
69+
"summary": "Read Responses",
70+
"operationId": "read_responses_responses_get",
71+
"responses": {
72+
"200": {
73+
"description": "Successful Response",
74+
"content": {
75+
"application/json": {
76+
"schema": {"$ref": "#/components/schemas/Rectangle"}
77+
}
78+
},
79+
}
80+
},
81+
}
82+
},
6283
},
6384
"components": {
6485
"schemas": {

tests/test_openapi_separate_input_output_schemas.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class Item(BaseModel):
2626
def get_app_client(separate_input_output_schemas: bool = True) -> TestClient:
2727
app = FastAPI(separate_input_output_schemas=separate_input_output_schemas)
2828

29-
@app.post("/items/")
30-
def create_item(item: Item):
29+
@app.post("/items/", responses={402: {"model": Item}})
30+
def create_item(item: Item) -> Item:
3131
return item
3232

3333
@app.post("/items-list/")
@@ -174,7 +174,23 @@ def test_openapi_schema():
174174
"responses": {
175175
"200": {
176176
"description": "Successful Response",
177-
"content": {"application/json": {"schema": {}}},
177+
"content": {
178+
"application/json": {
179+
"schema": {
180+
"$ref": "#/components/schemas/Item-Output"
181+
}
182+
}
183+
},
184+
},
185+
"402": {
186+
"description": "Payment Required",
187+
"content": {
188+
"application/json": {
189+
"schema": {
190+
"$ref": "#/components/schemas/Item-Output"
191+
}
192+
}
193+
},
178194
},
179195
"422": {
180196
"description": "Validation Error",
@@ -374,7 +390,19 @@ def test_openapi_schema_no_separate():
374390
"responses": {
375391
"200": {
376392
"description": "Successful Response",
377-
"content": {"application/json": {"schema": {}}},
393+
"content": {
394+
"application/json": {
395+
"schema": {"$ref": "#/components/schemas/Item"}
396+
}
397+
},
398+
},
399+
"402": {
400+
"description": "Payment Required",
401+
"content": {
402+
"application/json": {
403+
"schema": {"$ref": "#/components/schemas/Item"}
404+
}
405+
},
378406
},
379407
"422": {
380408
"description": "Validation Error",

0 commit comments

Comments
 (0)