Skip to content

[FEATURE] proper support of disciriminated unions in Pydantic #2315

@412b

Description

@412b

Why do we need this improvement?

There were some steps made towards support of discriminators for Pydantic, unfortunately those don't bring full support of that feature.
Generated code should follow Pydantic's way of using Unions with discriminators described.
Current code will serialize objects correctly, but Pydantic is not able to find a correct resolution during deserialization because of the missing wiring in the code.

How will this change help?

Discriminated objects will be correctly deserialized by Pydantic.

Screenshots

No response

How could it be implemented/designed?

I do have a PR ready for it.
it is a 2-fold solution:

  1. it requires changes in the "container" field (so any field having A | B, where A and B are distinguished by discriminator). Union or | must be wrapped in Annotated with Field(discriminator=...) provided.
  2. discriminator field in the object must be Literal.

🚧 Breaking changes

Yes

👀 Have you checked for similar open issues?

  • I checked and didn't find a similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue?

Yes I am willing to submit a PR!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions