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

Select response model specified in responses based on status code #2022

Closed
9 tasks done
languitar opened this issue Sep 7, 2020 · 0 comments
Closed
9 tasks done

Select response model specified in responses based on status code #2022

languitar opened this issue Sep 7, 2020 · 0 comments

Comments

@languitar
Copy link

languitar commented Sep 7, 2020

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.
  • After submitting this, I commit to:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • Or, I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug

Example

Here's a self-contained minimal, reproducible, example with my use case:

from fastapi import FastAPI, Response
from pydantic import BaseModel

api = FastAPI()


class DefaultModel(BaseModel):
    default: str = "yes"


class OtherModel(BaseModel):
    something: str = "something"
    other: str = "other"


@api.get("/test", response_model=DefaultModel, responses={503: {"model": OtherModel}})
def get(response: Response):
    response.status_code = 503
    return OtherModel()

Description

In contradiction to the documentation for the 503 status code, the sample code will result in a reply {"default": "yes"} without any validation errors.

It would be nice if fastapi could select the correct model depending on the status code declared in the response object.

The solution you would like

The code should result in an OtherModel being serialized instead of the default one based on the selected status code.

Describe alternatives you've considered

Using JSONResponse manually is tedious and error prone. At least using a model directly in JSONResponse would be nice instead of having to convert it manually to dict.

Environment

  • OS: [e.g. Linux / Windows / macOS]: linux
  • FastAPI Version [e.g. 0.3.0]: 0.61.1
  • Python version: 3.8.5
@languitar languitar added the feature New feature or request label Sep 7, 2020
@tiangolo tiangolo added question Question or problem reviewed question-migrate and removed feature New feature or request labels Feb 23, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #9129 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

2 participants