Repository: koxudaxi/datamodel-code-generator (Python)

Issue:
--capitalise-enum-members not working since v0.28.5
When using the flag `--capitalise-enum-members` (or `--capitalize-enum-members`) in e.g.

```bash
python3 -m datamodel_code_generator
  --input-file-type openapi \
  --output-model-type pydantic_v2.BaseModel \
  --encoding "UTF-8" \
  --disable-timestamp \
  --use-schema-description \
  --use-standard-collections \
  --use-union-operator \
  --use-default-kwarg \
  --field-constraints \
  --output-datetime-class AwareDatetime \
  --capitalise-enum-members \
  --enum-field-as-literal one \
  --set-default-enum-member \
  --use-subclass-enum \
  --allow-population-by-field-name \
  --snake-case-field \
  --strict-nullable \
  --use-double-quotes \
  --target-python-version 3.11 \
  --input myschema.yaml \
  --output mymodels.py
```

on say a schema like

```yaml
# file myschema.yaml
openapi: 3.0.3
info:
  version: X.Y.Z
  title: example schema
servers:
  - url: "https://acme.org"
paths: {}
components:
  schemas:
    EnumSystems:
      type: enum
      enum:
        - linux
        - osx
        - windows
```

under v0.28.4 this results (correctly) in

```py
from enum import Enum

class EnumSystems(Enum):
  LINUX = "linux"
  OSX = "osx"
  WINDOWS = "windows"
```

but since v0.28.5 it results incorrectly in

```py
from enum import Enum

class EnumSystems(Enum):
  linux = "linux"
  osx = "osx"
  windows = "windows"
```

Using:

- python v3.11
- `datamodel-code-generator==0.28.5` (to reproduce the flaw)
- `datamodel-code-generator>=0.28,<0.28.5` (to reproduce the correct behaviour)


Discussion:
I think this is the combination of `--snake-case-field` and `--capitalise-enum-members`
I am experiencing the same issue



Patch:
diff --git a/src/datamodel_code_generator/parser/base.py b/src/datamodel_code_generator/parser/base.py
index 35fa842f8..74df98e54 100644
--- a/src/datamodel_code_generator/parser/base.py
+++ b/src/datamodel_code_generator/parser/base.py
@@ -1121,6 +1121,9 @@ def __change_field_name(
         if self.data_model_type != pydantic_model_v2.BaseModel:
             return
         for model in models:
+            if model.base_class == "Enum":
+                continue
+
             for field in model.fields:
                 filed_name = field.name
                 filed_name_resolver = ModelResolver(snake_case_field=self.snake_case_field, remove_suffix_number=True)


Test patch:
diff --git a/tests/test_main_kr.py b/tests/test_main_kr.py
index 7080bd5cb..197f3c015 100644
--- a/tests/test_main_kr.py
+++ b/tests/test_main_kr.py
@@ -304,3 +304,61 @@ def test_main_use_field_description() -> None:
         generated = output_file.read_text()
         expected = (EXPECTED_MAIN_KR_PATH / "main_use_field_description" / "output.py").read_text()
         assert generated == expected
+
+
+def test_capitalise_enum_members(tmp_path: Path) -> None:
+    """capitalise-enum-members not working since v0.28.5
+
+    From https://github.com/koxudaxi/datamodel-code-generator/issues/2370
+    """
+    input_data = """
+openapi: 3.0.3
+info:
+  version: X.Y.Z
+  title: example schema
+servers:
+  - url: "https://acme.org"
+paths: {}
+components:
+  schemas:
+    EnumSystems:
+      type: enum
+      enum:
+        - linux
+        - osx
+        - windows
+"""
+    input_file = tmp_path / "myschema.yaml"
+    input_file.write_text(input_data, encoding="utf_8")
+
+    expected_output = """# generated by datamodel-codegen:
+#   filename:  myschema.yaml
+
+from __future__ import annotations
+
+from enum import Enum
+
+
+class EnumSystems(Enum):
+    LINUX = 'linux'
+    OSX = 'osx'
+    WINDOWS = 'windows'
+"""
+
+    output_file: Path = tmp_path / "output.py"
+    return_code: Exit = main([
+        "--output-model-type",
+        "pydantic_v2.BaseModel",
+        "--disable-timestamp",
+        "--capitalise-enum-members",
+        "--snake-case-field",
+        "--input",
+        str(input_file),
+        "--output",
+        str(output_file),
+    ])
+    assert return_code == Exit.OK
+    output_file_read_text = output_file.read_text(encoding="utf_8")
+    assert output_file_read_text == expected_output, (
+        f"\nExpected  output:\n{expected_output}\n\nGenerated output:\n{output_file_read_text}"
+    )