Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type overrides #2243

Open
otonnesen opened this issue Dec 27, 2024 · 0 comments
Open

Type overrides #2243

otonnesen opened this issue Dec 27, 2024 · 0 comments

Comments

@otonnesen
Copy link
Contributor

Is your feature request related to a problem? Please describe.
At my work, we have a few custom pydantic models that aren't really easy or possible to encode in an OpenAPI spec. As a result, we define them as pydantic.StrictStr and end up doing a lot of annoying back-and-forth rote type conversions whenever we interact with them on any code-generated models.

Describe the solution you'd like
It would be great if we could just provide a mapping from OpenAPI spec model names to Python import strings and have datamodel codegen link to them instead. My rough idea for how this would work is to have a JSON file with the following structure:
type-overrides.json

{
  "Some.PotentiallyNamespaced.CodeGeneratedType": "my_app.some_module.types.MyType",
  ...
}

This file could then be passed in as a filename (similar to how --aliases is handled), like datamodel-codegen ... --type-overrides type-overrides.json.

Describe alternatives you've considered
Currently, we define complex models as pydantic.StrictStr and handle type conversions manually. This approach, however, is error-prone and requires a bunch of annoying boilerplate everywhere.

I briefly looked into trying to do this mapping with custom_templates, but it got ugly quick.

Additional context
I'd be willing to work on the change myself if there's any appetite to add the feature. I have a sorta-working WIP branch here: https://github.com/otonnesen/datamodel-code-generator/tree/experiment/type-overrides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant