Tutorial [EN] speak about ordering in Union variants
See original GitHub issueDescribe the bug
The example in /fastapi/blob/master/docs_src/extra_models/tutorial003.py
describe how to use a Union
in a response_model
like so:
@app.get("/items/{item_id}", response_model=Union[PlaneItem, CarItem])
async def read_item(item_id: str):
return items[item_id]
However this will not work as expected if we will to flip the order of the items inside the Union[...]
.
@app.get("/items/{item_id}", response_model=Union[CarItem, PlaneItem])
async def read_item(item_id: str):
return items[item_id]
In that case the "size": 5
will not be returned for "item2"
.
And there is no mention of the issue anywhere in the text at:
https://fastapi.tiangolo.com/tutorial/extra-models/#union-or-anyof
To Reproduce
In the mindset of a new user, type the example flipping ItemCar
and ItemPlane
and try to figure out why you don’t get the expected result for item2
.
Expected behavior
At the very least there should be a note explaining that the order of Union variants are important in this context, and say how and why.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5 (4 by maintainers)
Top Results From Across the Web
MySQL UNION - Comprehensive Tutorial With Union Examples
This tutorial explains the MySQL UNION command, its types, Union vs Union All, and examples to combine data from 2 or more queries....
Read more >SQL UNION overview, usage and examples - SQLShack
This article provides overview of the SQL UNION operator, along with examples and explore some common questions like the differences between ...
Read more >SQL Set Operators: The Complete Guide to UNION ...
SQL set operators include UNION, UNION ALL, INTERSECT, MINUS, and EXCEPT. Learn what they are and how to use them in this guide....
Read more >UNION, INTERSECTION and EXCEPT - jOOQ
As previously mentioned in the manual's section about the ORDER BY clause, jOOQ has slightly changed the semantics of these set operators.
Read more >How to implement union operator in oracle with example
Guide to the UNION operator in Oracle. Here we discuss rules and restrictions along with various examples and code implementations of UNION operators....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Awesome @kbanc ! 🚀 Thanks a lot for checking it and replicating the problem here, it helps a lot! 🎉 🙇
So, yes, that’s part of how Pydantic works, it’s documented here: https://pydantic-docs.helpmanual.io/usage/types/#unions
We could have a section in the docs in the first part that mentions
Union
(I’m not sure where is the first mention ofUnion
s 🤔 ) with a warning about that.Would you like to take a stab at that? 😬 😅 🤓 📝
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.