Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Regression in OpenAPI related checks during app.include_router(router) #5326

@Kyle-sandeman-mrdfood

Description

@Kyle-sandeman-mrdfood

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

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)

Description

In approx 0.76.0 a regression was observed related to the OpenAPI responses no longer accepting patterns of the form 4XX 5XX etc.

>>> app.include_router(router)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/venv/lib/python3.8/site-packages/fastapi/applications.py", line 420, in include_router
    self.router.include_router(
  File "/tmp/venv/lib/python3.8/site-packages/fastapi/routing.py", line 738, in include_router
    self.add_api_route(
  File "/tmp/venv/lib/python3.8/site-packages/fastapi/routing.py", line 565, in add_api_route
    route = route_class(
  File "/tmp/venv/lib/python3.8/site-packages/fastapi/routing.py", line 422, in __init__
    assert is_body_allowed_for_status_code(
  File "/tmp/venv/lib/python3.8/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: '5XX'

Please refer to this PR #5145 which introduced the new code

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.81.0

Python Version

3.8.13

Additional Context

I can't actually commit to a PR/monitoring this repo/such. I will likely sponsor the project soon as I love it, but I simply don't have that kind of time

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions