[FEATURE] Inheritance and Polymorphism support
See original GitHub issueActually, there is no support for Inheritance and Polymorphism as I know in fastapi : https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/
Maybe annotations like that will do the job :
class Item(BaseModel):
type: str
class CarItem(Item):
type: str
class PlaneItem(Item):
type: str
size: str
@app.post("/items/", response_model=Item, one_of=[CarItem, PlaneItem], discriminator="type")
async def create_item(*, item: Item):
return item
For now, we need to define an any
response_model since defining an Item
response_model will remove the extra attributes during serialization.
I’ll try to hack using the based starlette api : https://www.starlette.io/schemas/ but comment are unused by fastapi (except for description)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:22 (12 by maintainers)
Top Results From Across the Web
5.4: Difference between Inheritance and Polymorphism
Inheritance : Inheritance is one in which a new class is created that inherits the properties of the already exist class. It supports...
Read more >Understanding Java Inheritance and Polymorphism - Section.io
In inheritance, we create new classes that inherit features of the superclass while polymorphism decides what form of method to execute.
Read more >Chapter 13. Inheritance and Polymorphism
Inheritance is a language construct that supports the sharing of features amongst different objects. Consider the domain of vehicles, which includes bicycles, ...
Read more >Important Facts About Inheritance and Polymorphism
Two of the benefits of OT are code reusability and extensibility, and inheritance allows the implementation of both of these features. When new...
Read more >Polymorphism vs. Inheritance: Difference Between ... - upGrad
Inheritance is essentially making a class, and then having other classes in your program get their feature form the already existing base class....
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 Free
Top 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
I think real inheritance and polymorphism support would be very useful. Comments above show that using
typing.Union
forces users to carefully put the class in order and is not a generic solution.@mcauto: Ah, that’s a common mistake people make. Pydantic can’t tell what data structure your JSON is supposed to be aside from matching it against one of your possible models. As it happens, since
Woman
adds no new fields toHuman
andMan
does, there’s no way for Pydantic to tell whether the payload you sent it just aWoman
object (essentially aHuman
object) with extra fields or aMan
object, since both match.If you really want the absence of
something
to define whether or not yourHuman
subtype isMan
orWoman
, I believe you can just haveWoman
forbid the presence of extra fields, like so:(I have not tested it myself, though, so apologies if this doesn’t work)