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

recover response_model from output annotations #4734

Closed
9 tasks done
AM-I-Human opened this issue Mar 28, 2022 · 3 comments
Closed
9 tasks done

recover response_model from output annotations #4734

AM-I-Human opened this issue Mar 28, 2022 · 3 comments

Comments

@AM-I-Human
Copy link

AM-I-Human commented Mar 28, 2022

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

app = FastAPI()


@router.post("/have_output_example_and_schema", response_model=list)
async def test(input: int-> list:
    return []


@router.post("/have_not_output_example_and_schema")
async def test_2(input: int-> list:
    return []

Description

I think it will be nice if the type in response_model (in the app decorators) will be infered from the output annotation of the function.
By doing so you can avoid some documentation-code inconsistency.

Wanted Solution

When response_model is None it will try to use the annotation one, es by using self.response_model= response_model or get_output_annotation(fn)

@router.post("/have_not_output_example_and_schema")
async def test_2(input: int-> list:
    return []

Will be the same as

@router.post("/have_not_output_example_and_schema",response_model=list)
async def test_2(input: int-> list:
    return []

Wanted Code

from fastapi import FastAPI

app = FastAPI()


@router.post("/have_output_example_and_schema")
async def test(input: int-> list:
    return []


@router.post("/have_not_output_example_and_schema")
async def test_2(input: int-> list:
    return []

Alternatives

No response

Operating System

Windows

Operating System Details

Windows 11

FastAPI Version

0.75.0

Python Version

Python 3.10.4

Additional Context

No response

@AM-I-Human AM-I-Human added the feature New feature or request label Mar 28, 2022
@chbndrhnns
Copy link

This has been discarded earlier, like in #101

@AM-I-Human
Copy link
Author

AM-I-Human commented Mar 28, 2022

Ah! I missed that, thanks.

@tiangolo
Copy link
Owner

Thanks for the help here @chbndrhnns! 👏 🙇

Thanks for reporting back and closing the issue @AM-I-Human 👍

This is now supported (optionally) in the latest versions. 🚀

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

@tiangolo tiangolo added question Question or problem answered reviewed and removed feature New feature or request labels Feb 22, 2023
@tiangolo tiangolo changed the title [FEATURE] recover response_model from output annotations recover response_model from output annotations Feb 24, 2023
@tiangolo tiangolo reopened this Feb 27, 2023
Repository owner locked and limited conversation to collaborators Feb 27, 2023
@tiangolo tiangolo converted this issue into discussion #6355 Feb 27, 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

3 participants