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.

[Question]Is 'allOf' supported by Pydantic?

See original GitHub issue

Question

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:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
bquestionscommented, Apr 17, 2019

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!

1reaction
tiangolocommented, Apr 16, 2019

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 from UserInDB, and that model, in turn, inherited from UserBase. In this case, the client would have to deal with 3 models/schemas, in a chain of inheritance. While he actually just cared about the User 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.

Read more comments on GitHub >

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

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