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

[Enhancement] Add support for specifying `additional-imports` in the `extra-template-data` json file.

Open rpmcginty opened this issue 5 months ago • 0 comments

Is your feature request related to a problem? Please describe.

[!NOTE]
This is a nice-to-have more than something that fixes a problem.

Currently, if you want to override a scalar type defined in your source definition (for me it is graphql), you must specify this in a json file and pass to command using --extra-template-data.json option.

https://github.com/koxudaxi/datamodel-code-generator/blob/main/tests/data/graphql/additional-imports-types.json

{
  "Date": {
    "py_type": "date"
  },
  "DateTime": {
    "py_type": "datetime"
  },
  "MyCustomClass": {
    "py_type": "MyCustomPythonClass"
  }
}

If you want to specify a custom class (as shown in the above example), you must separately specify the additional import for this class using --additional-imports option.

Describe the solution you'd like

Instead of having to specify in two different locations, it would be nice to add this additional config info in the extra-template-data.json file.

{
  "Date": {
    "py_type": "date",
    "additional_imports": "datetime.date"
  },
  "DateTime": {
    "py_type": "datetime",
    "additional_imports": "datetime.datetime"
  },
  "MyCustomClass": {
    "py_type": "MyCustomPythonClass",
    "additional_imports": "my_path.to.my.custom.MyCustomPythonClass"
  }
}

rpmcginty avatar Jun 13 '25 01:06 rpmcginty