First Check
Commit to Help
Example Code
from fastapi import FastAPI
from fastapi.responses import JSONResponse
from pydantic import BaseModel
class Item(BaseModel):
id: str
value: str
class Message(BaseModel):
message: str
app = FastAPI()
@app.get("/items/{item_id}", response_model=Item, responses={"default": {"model": Message}})
async def read_item(item_id: str):
if item_id == "foo":
return {"id": "foo", "value": "there goes my hero"}
else:
return JSONResponse(status_code=404, content={"message": "Item not found"})
Description
Prior to 0.79.0 I was able to specify the default response for the path. According to the OpenAPI Spec, it should be somehow possible: https://swagger.io/specification/#path-item-object
At 0.79.0 I am receiving this exception:
Traceback (most recent call last):
File "<stdin>", line 19, in <module>
File "/Users/matnowak/.pyenv/versions/3.9.11/envs/radkit3.9/lib/python3.9/site-packages/fastapi/routing.py", line 622, in decorator
self.add_api_route(
File "/Users/matnowak/.pyenv/versions/3.9.11/envs/radkit3.9/lib/python3.9/site-packages/fastapi/routing.py", line 561, in add_api_route
route = route_class(
File "/Users/matnowak/.pyenv/versions/3.9.11/envs/radkit3.9/lib/python3.9/site-packages/fastapi/routing.py", line 418, in __init__
assert is_body_allowed_for_status_code(
File "/Users/matnowak/.pyenv/versions/3.9.11/envs/radkit3.9/lib/python3.9/site-packages/fastapi/utils.py", line 24, in is_body_allowed_for_status_code
current_status_code = int(status_code)
ValueError: invalid literal for int() with base 10: 'default'
invalid literal for int() with base 10: 'default'
How to set the default response in OpenAPI?
Operating System
Linux, Windows, macOS
Operating System Details
No response
FastAPI Version
0.79.0
Python Version
3.9.11
Additional Context
No response
First Check
Commit to Help
Example Code
Description
Prior to
0.79.0I was able to specify thedefaultresponse for the path. According to the OpenAPI Spec, it should be somehow possible: https://swagger.io/specification/#path-item-objectAt
0.79.0I am receiving this exception:How to set the default response in OpenAPI?
Operating System
Linux, Windows, macOS
Operating System Details
No response
FastAPI Version
0.79.0
Python Version
3.9.11
Additional Context
No response