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

Use validators for models that have "one of" requirements.

Open jkugler opened this issue 5 months ago • 0 comments

Is your feature request related to a problem? Please describe. When a model has a "oneOf" requirement, Datamodel creates multiple models, with each field required, and then creates a "root" model that merges all those models. This makes attribute access painful.

Describe the solution you'd like Instead of creating multiple models, use Pydantic validators to make sure on one of the fields has a value, and raise an error if not the case.

Describe alternatives you've considered I have modified the template for Root models to add __getattr__ and __setattr__ methods. But this would not be needed if it didn't create multiple models.

jkugler avatar Jun 08 '25 06:06 jkugler