datamodel-code-generator
datamodel-code-generator copied to clipboard
Imports break with set-default-enum-member in same file
Describe the bug When we use the --set-default-enum-member parameter, the value used for defaults is referencing module.EnumName.value, which fails to import when the containing object and the enum are defined in the same file.
We name our objects with module names to organize our models. So, our contact model is named "contacts.Contact" in our OpenAPI YAML, which results in the Contact being put in models/contacts.py. Because enums turn into model classes too, our "contacts.py" also has a class called ContactForCompanyManagement that encapsulates the enum.
In the case of the simplified example below, the resultant output should be that the Contact object has a Field called contact_for_company_management, and the first parameter to Field, for the default, should be ContactForCompanyManagement.actionable, but instead it is rendered in the file as contacts.ContactForCompanyManagement.actionable which is an invalid import since we are inside contacts already.
To Reproduce
Example schema:
// This is in the YAML file contacts.yaml
// Assume this is inside a model named "contacts.Contact"
{
"contact_for_company_management": {
"type": "string",
"enum": [ "actionable", "read_only"],
"default": "actionable"
}
}
Used commandline:
datamodel-codegen --input descriptions/v1/api.bundled.yaml --output app/v1/models/ \
--use-standard-collections \
--target-python-version 3.11 \
--input-file-type openapi \
--enum-field-as-literal one \
--use-subclass-enum \
--disable-timestamp \
--strict-nullable \
--set-default-enum-member \
--use-double-quotes \
--output-model-type pydantic_v2.BaseModel \
--base-class app.v1.models.base.BaseModel
Expected behavior We would expect that no module name should be used when referencing the enum when it's defined in the same file as where it's being used.
Version:
- OS: [e.g. iOS] MacOS 14.6.1
- Python version: 3.11
- datamodel-code-generator version: [e.g. 0.28.5]