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

Skip to content

Unavailability to set default response for OpenAPI doc at 0.79.0 #5166

@matiuszka

Description

@matiuszka

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 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

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