✨ Add support of endpoint return type as response_model#4463
✨ Add support of endpoint return type as response_model#4463Tradunsky wants to merge 1 commit intofastapi:masterfrom
Conversation
|
oh.. I should have looked at closed PRs, issues and #101 Data-wise, it may help to analyze how often people need anything more sophisticated than just one model type. 🤔 Feel free to close this PR though. |
|
Found a lot more discussions about this already. My opinion still stays the same, but it may be annoying to go over these issues, again and again, so closing this PR. If anyone finds this, I would recommend looking at the small utility to get this as you need: |
Why this PR is open?
Tl;Dr; Generate endpoint response OpenAPI schema based on the endpoint return type.
In order to declare endpoint params' schema, you can just add typing to a param like this:
and it naturally generates OpenAPi spec for parameter types

The same applies to the request body, which is very convenient and feels natural and expected by the principal of least surprise 👍🏻
However, when you add return type - no response schema generated, return schema stays an arbitrary string.
Unless you specify
response_modelto generate OpenAPI spec:What this PR does?
It aligns behaviour of an endpoint parameter and return type to generate OpenAPI spec.
It is also backward compatible with existing code that has to put
response_modelin FastAPI route decorator.How this PR was tested?
pytest tests/test_response_class_is_return_type.py./scripts/test.shand run modified
docs_src/query_params/tutorial001.pylocally.WDYT?