Repository: koxudaxi/datamodel-code-generator

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 hints:
codspeed-hq[bot]: <!-- __CODSPEED_PERFORMANCE_REPORT_COMMENT__ -->
<!-- __CODSPEED_INSTRUMENTATION_PERFORMANCE_REPORT_COMMENT__ -->

## [CodSpeed Performance Report](https://codspeed.io/koxudaxi/datamodel-code-generator/branches/kevin-paulson-mindbridge-ai%3Acapitalise-enum-members-fix)

### Merging #2389 will **not alter performance**

<sub>Comparing <code>kevin-paulson-mindbridge-ai:capitalise-enum-members-fix</code> (27ae113) with <code>main</code> (c0e19d9)</sub>



### Summary

`✅ 31` untouched benchmarks  




kevin-paulson-mindbridge-ai: I believe the issue was that for Enums this was trying to set an alias using `Field`. Let me know if this is correct or if any changes should be made. Again, certainly happy for anyone else create their own PR (or update this one) if they want to.
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/koxudaxi/datamodel-code-generator/pull/2389?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Koudai+Aono) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 98.12%. Comparing base [(`4fa8ea3`)](https://app.codecov.io/gh/koxudaxi/datamodel-code-generator/commit/4fa8ea313b82c649088d73f304ec44ce3e347f20?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Koudai+Aono) to head [(`860a5a0`)](https://app.codecov.io/gh/koxudaxi/datamodel-code-generator/commit/860a5a0032f9da4747105c7315514c1ad8e4548d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Koudai+Aono).

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #2389   +/-   ##
=======================================
  Coverage   98.12%   98.12%           
=======================================
  Files          65       65           
  Lines        8008     8020   +12     
  Branches      808      809    +1     
=======================================
+ Hits         7858     7870   +12     
  Misses        114      114           
  Partials       36       36           
```

| [Flag](https://app.codecov.io/gh/koxudaxi/datamodel-code-generator/pull/2389/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Koudai+Aono) | Coverage Δ | |
|---|---|---|
| [unittests](https://app.codecov.io/gh/koxudaxi/datamodel-code-generator/pull/2389/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Koudai+Aono) | `98.12% <100.00%> (+<0.01%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Koudai+Aono#carryforward-flags-in-the-pull-request-comment) to find out more.

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/koxudaxi/datamodel-code-generator/pull/2389?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Koudai+Aono).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Koudai+Aono).

<details><summary> :rocket: New features to boost your workflow: </summary>

- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>

Solution patch:
diff --git a/src/datamodel_code_generator/parser/base.py b/src/datamodel_code_generator/parser/base.py
index 35fa842f..74df98e5 100644
--- a/src/datamodel_code_generator/parser/base.py
+++ b/src/datamodel_code_generator/parser/base.py
@@ -1121,6 +1121,9 @@ class Parser(ABC):
         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 7080bd5c..197f3c01 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}"
+    )