[Question]Is 'allOf' supported by Pydantic?
See original GitHub issueQuestion
For bugs/questions:
- OS: CentOS 7.5
- Python version: 3.6.0
- Pydantic version: 0.19
Does pydantic currently support polymorphism/inheritance usingallOf
? For example if I have the following code (taken from https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#models-with-composition):
from pydantic import BaseModel, Schema
def Error(BaseModel):
message: str = Schema(..., description="")
code: int = Schema(..., description="")
def ExtendedErrorModel(Error):
rootCause: str= Schema(..., description="")
If I were to generate the schema for ExtendedErrorModel
I should get something that matches the following which uses allOf
to signify that if I’m creating an ExtendedErrorModel
object, it not only specifies rootCause
but also code and message
:
components:
schemas:
ErrorModel:
type: object
required:
- message
- code
properties:
message:
type: string
code:
type: integer
minimum: 100
maximum: 600
ExtendedErrorModel:
allOf:
- $ref: '#/components/schemas/ErrorModel'
- type: object
required:
- rootCause
properties:
rootCause:
type: string
However when it’s actually generated, everything is flattened so the properties from the ErrorModel
appear regularly on the ExtendedErrorModel
. Which is how it should validate it, but not how the OpenAPI schema should appear:
components:
schemas:
ErrorModel:
type: object
required:
- message
- code
- rootCause
properties:
message:
type: string
code:
type: integer
minimum: 100
maximum: 600
rootCause:
type: string
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
[Question]Is 'allOf' supported by Pydantic? · Issue #478 - GitHub
Question For bugs/questions: OS: CentOS 7.5 Python version: 3.6.0 ... Does pydantic currently support polymorphism/inheritance using allOf ?
Read more >How do I force pydantic schema into recursion instead of ...
According to the documentation, I would expect that to occur when modifying the title of the Field object. Q: It not via the...
Read more >pydantic/Lobby - Gitter
hi! a quick question - is there any way to specify $id and $schema ... that I'm not lieing if I'm writing that...
Read more >Features - FastAPI
Support for complex user authentication systems, database connections, etc. ... With FastAPI you get all of Pydantic's features (as FastAPI is based on ......
Read more >Pydantic V2 Plan
Pydantic has always had special support for JSON, ... please feel free to create a discussion if you have any questions or suggestions....
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
Lol yeah sure, I can take a look into a potential solution when I have some free time this weekend to see if it would work.
Thank you!
Thanks for tagging me @samuelcolvin .
@bquestions, so, generating schemas with inheritance using
allOf
as you suggest, is currently not supported. Inheritance of Pydantic models is supported and works, but the generated JSON Schema is flattened as you have noticed.I think implementing it would be non-trivial, and I’m not sure if there would be any advantage of having it.
For example, FastAPI is based on Pydantic, built around OpenAPI, it generates OpenAPI schemas automatically, etc. And using the current implementation of Pydantic models has worked for most of the use cases (all the ones I know).
On the other side, if it was implemented that way, I’m not sure it would be the best option. For example, let’s say a client gets to see a
User
model on the OpenAPI schema, and this model inherited fromUserInDB
, and that model, in turn, inherited fromUserBase
. In this case, the client would have to deal with 3 models/schemas, in a chain of inheritance. While he actually just cared about theUser
model, the other models are never even used by him but by the backend directly. And still he would have to deal with multiple inheritance, etc.