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
Use return type annotation for response_model by default. #875
Conversation
Codecov Report
@@ Coverage Diff @@
## master #875 +/- ##
===========================================
- Coverage 100% 38.48% -61.52%
===========================================
Files 289 287 -2
Lines 7595 7574 -21
===========================================
- Hits 7595 2915 -4680
- Misses 0 4659 +4659
Continue to review full report at Codecov.
|
Thanks for your interest @juhovh-aiven ! ✨ Check the note here in the "Technical Details": https://fastapi.tiangolo.com/tutorial/response-model/#response_model_include-and-response_model_exclude
It also means that if you return a It would be quite easy that in many cases the I'll close this PR, but feel free to add more comments, issues, or new PRs. |
Thanks for a quick response! I had actually already checked that note in the "Technical Details", and I also use that feature quite heavily elsewhere (returning a dict and then validating/limiting fields through a pydantic model). However, my suggestion was simply about removing duplication, i.e. if the But as mentioned, it's pretty easy to do with a custom route class with its own constructor, so I'll just keep going with that. Thank you for your good work with FastAPI, I must say I quite like it. |
This was added in #1436 🚀 It's available in FastAPI The new docs are here: https://fastapi.tiangolo.com/tutorial/response-model/ 🤓 |
This shouldn't be merged as-is, because I haven't added related tests yet, but I wanted to send this up for discussion. In my use case I am basically already returning the correct models from the endpoint functions already, and have return types enforced through mypy, so it feels quite unnecessary to duplicate the return type to the response_model argument to get the correct OpenAPI docs.
Basically I ended up creating a custom router that does similar logic as in this PR, but I am curious why it is not the default, since this behaviour would feel intuitive to me. I'm not offended at all if this gets rejected, but thought it's easier to explain what I'm asking through a PR instead of trying to explain it verbally in an issue.