question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tutorial [EN] speak about ordering in Union variants

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
tiangolocommented, Jun 17, 2020

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 of Unions 🤔 ) with a warning about that.

Would you like to take a stab at that? 😬 😅 🤓 📝

0reactions
github-actions[bot]commented, Nov 15, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found