Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

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

Closed
9 tasks done
Kyle-sandeman-mrdfood opened this issue Aug 31, 2022 · 2 comments
Closed
9 tasks done
Labels
question Question or problem question-migrate

Comments

@Kyle-sandeman-mrdfood
Copy link

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

@Kyle-sandeman-mrdfood Kyle-sandeman-mrdfood added the question Question or problem label Aug 31, 2022
@JarroVGIT
Copy link
Contributor

A fix is ready here #5187

@Kyle-sandeman-mrdfood
Copy link
Author

Thanks, now just a question of who can approve+merge it

Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8380 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

3 participants