🐛 Fix removing body from status codes that do not support it#5145
Conversation
Codecov Report
@@ Coverage Diff @@
## master #5145 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 532 532
Lines 13672 13684 +12
=========================================
+ Hits 13672 13684 +12
Continue to review full report at Codecov.
|
|
🚀 Deployed on https://62cffb07d3e5474e8d76b418--fastapi.netlify.app |
|
📝 Docs preview for commit ca239a6 at: https://62cffb47f3a67d4fbe8539c7--fastapi.netlify.app |
|
Good day, I may file an issue if you'd like but this commit introduces a regression Previously you could do this As per https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.2.md#patterned-fields-1 As of this commit/PR it throws an error upon adding a new router: (similar to #5166 (comment)) |
|
Hey @Kyle-sandeman-mrdfood, I just tried a lot of things to replicate your problem, but I can't seem to make it "break". Maybe it was fixed in a recent release. If you are still having problems, please create a new issue. Here's the example app I used, based on the docs to extend OpenAPI responses and adding a from typing import Union
from fastapi import FastAPI, APIRouter
from fastapi.responses import FileResponse
from pydantic import BaseModel
class Item(BaseModel):
id: str
value: str
responses = {
404: {"description": "Item not found"},
302: {"description": "The item was moved"},
403: {"description": "Not enough privileges"},
"5XX": {"description": "Server errors"},
}
app = FastAPI()
router = APIRouter()
@router.get(
"/items/{item_id}",
response_model=Item,
responses={**responses, 200: {"content": {"image/png": {}}}},
)
async def read_item(item_id: str, img: Union[bool, None] = None):
if img:
return FileResponse("image.png", media_type="image/png")
else:
return {"id": "foo", "value": "there goes my hero"}
app.include_router(router) |
|
Hi again, it may be related to the from typing import Union
from fastapi import FastAPI, APIRouter
from fastapi.responses import FileResponse
from pydantic import BaseModel
class Item(BaseModel):
id: str
value: str
responses = {
404: {"description": "Item not found"},
302: {"description": "The item was moved"},
403: {"description": "Not enough privileges"},
"5XX": {"description": "Server errors", "model": Item},
}
app = FastAPI(responses=responses)
router = APIRouter()
@router.get(
"/items/{item_id}",
response_model=Item,
)
async def read_item(item_id: str, img: Union[bool, None] = None):
if img:
return FileResponse("image.png", media_type="image/png")
else:
return {"id": "foo", "value": "there goes my hero"}
app.include_router(router) |
🐛 Fix removing body from status codes that do not support it
This is related to this issue here: #4050
Related to this issue in Starlette: Kludex/starlette#1764
And related to this PR in Starlette: Kludex/starlette#1765
This removes
fastapi.openapi.constants.STATUS_CODES_WITH_NO_BODY, it is replaced by a function in utils.