Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix custom response input output schema bug #11517

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion fastapi/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,11 @@ def __init__(
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
Expand Down
36 changes: 32 additions & 4 deletions tests/test_openapi_separate_input_output_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down