mirror of https://github.com/tiangolo/fastapi.git
🐛 Fix openapi generation with responses kwarg (#10895)
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>
This commit is contained in:
parent
b29cf1621a
commit
e049fc4ea1
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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": {
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Reference in New Issue