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
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}
}
},
}
- }
+ },
+ "/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": {
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/")
"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",
"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",