datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Support for Pydantic discriminator

Open bernardoVale opened this issue 3 years ago • 1 comments
trafficstars

Describe the solution you'd like The ability to use pydantic discriminator when defining the field schema.

https://pydantic-docs.helpmanual.io/usage/types/#discriminated-unions-aka-tagged-unions

Describe alternatives you've considered The alternative is always to ensure that all subtypes have a unique and required field so that pydantic generates the correct type instead of using a common field and using the value as the discriminator.

Additional context

https://pydantic-docs.helpmanual.io/usage/types/#discriminated-unions-aka-tagged-unions

bernardoVale avatar Sep 01 '22 22:09 bernardoVale

I hacked the code a little bit and managed to make it work the way I want by adding discriminator to this list: https://github.com/koxudaxi/datamodel-code-generator/blob/master/datamodel_code_generator/parser/jsonschema.py#L274 is that an acceptable implementation?

bernardoVale avatar Sep 01 '22 22:09 bernardoVale