Repository: koxudaxi/datamodel-code-generator

Issue:
`--set-default-enum-member` neglects integer type enums
**Describe the bug**

From the README, the `--set-default-enum-member` flag should "Set enum members as default values for enum field." This seems to work reliably until an integer type enum is used. Instead of setting a default enum type, a literal is used instead.

**To Reproduce**

Example schema:
```json
{
    "openapi": "3.0.0",
    "info": {
      "title": "Test API",
      "version": "1.0.0"
    },
    "paths": {},
    "components": {
      "schemas": {
        "Authority": {
          "type": "integer",
          "enum": [1000, 100, 10, 0],
          "title": "Authority",
          "description": "example"
        },
        "Referrer": {
                "properties": {
                    "permission": {
                        "$ref": "#/components/schemas/Authority",
                        "description": "The permission that the principals will be granted. ",
                        "default": 0
                    }
                },
                "additionalProperties": false,
                "type": "object",
                "title": "Referrer",
                "description": "example"
            }
      }
    }
  }
```

Used commandline:
```
$ datamodel-codegen --input openapi.json --output out.py --field-constraints --output-model-type pydantic_v2.BaseModel --set-default-enum-member
```

**Expected behavior**

We would expect the output script to be:
```py
class Authority(Enum):
    integer_1000 = 1000
    integer_100 = 100
    integer_10 = 10
    integer_0 = 0


class Referrer(BaseModel):
    model_config = ConfigDict(
        extra='forbid',
    )
    permission: Optional[Authority] = Field(
        Authority.integer_0,  # <--- enum type
        description='The permission that the principals will be granted. '
    )
```

But instead we get:
```py
class Authority(Enum):
    integer_1000 = 1000
    integer_100 = 100
    integer_10 = 10
    integer_0 = 0


class Referrer(BaseModel):
    model_config = ConfigDict(
        extra='forbid',
    )
    permission: Optional[Authority] = Field(
        0, # <--- literal type
        description='The permission that the principals will be granted. '
    )
```


**Version:**
 - OS: MacOS M1 Sequoia 15.1.1
 - Python version: 3.10.0
 - datamodel-code-generator version: 0.26.4



Discussion:
This will cause a type checker to complain due to the mismatch between the annotated versus assigned type, if that wasn't clear.



Patch:
diff --git a/datamodel_code_generator/model/enum.py b/datamodel_code_generator/model/enum.py
index c3275a140..541c64de5 100644
--- a/datamodel_code_generator/model/enum.py
+++ b/datamodel_code_generator/model/enum.py
@@ -82,10 +82,22 @@ def get_member(self, field: DataModelFieldBase) -> Member:
 
     def find_member(self, value: Any) -> Optional[Member]:
         repr_value = repr(value)
-        for field in self.fields:  # pragma: no cover
-            if field.default == repr_value:
+        # Remove surrounding quotes from the string representation
+        str_value = str(value).strip('\'"')
+
+        for field in self.fields:
+            # Remove surrounding quotes from field default value
+            field_default = field.default.strip('\'"')
+
+            # Compare values after removing quotes
+            if field_default == str_value:
                 return self.get_member(field)
-        return None  # pragma: no cover
+
+            # Keep original comparison for backwards compatibility
+            if field.default == repr_value:  # pragma: no cover
+                return self.get_member(field)
+
+        return None
 
     @property
     def imports(self) -> Tuple[Import, ...]:


Test patch:
diff --git a/tests/parser/test_base.py b/tests/parser/test_base.py
index a36951f1b..c90a753b3 100644
--- a/tests/parser/test_base.py
+++ b/tests/parser/test_base.py
@@ -287,6 +287,137 @@ def test_postprocess_result_modules(input_data, expected):
     assert result == expected
 
 
+def test_find_member_with_integer_enum():
+    """Test find_member method with integer enum values"""
+    from datamodel_code_generator.model.enum import Enum
+    from datamodel_code_generator.model.pydantic.base_model import DataModelField
+    from datamodel_code_generator.reference import Reference
+    from datamodel_code_generator.types import DataType
+
+    # Create test Enum with integer values
+    enum = Enum(
+        reference=Reference(
+            path='test_path', original_name='TestEnum', name='TestEnum'
+        ),
+        fields=[
+            DataModelField(
+                name='VALUE_1000',
+                default='1000',
+                data_type=DataType(type='int'),
+                required=True,
+            ),
+            DataModelField(
+                name='VALUE_100',
+                default='100',
+                data_type=DataType(type='int'),
+                required=True,
+            ),
+            DataModelField(
+                name='VALUE_0',
+                default='0',
+                data_type=DataType(type='int'),
+                required=True,
+            ),
+        ],
+    )
+
+    # Test finding members with integer values
+    assert enum.find_member(1000).field.name == 'VALUE_1000'
+    assert enum.find_member(100).field.name == 'VALUE_100'
+    assert enum.find_member(0).field.name == 'VALUE_0'
+
+    # Test with string representations
+    assert enum.find_member('1000').field.name == 'VALUE_1000'
+    assert enum.find_member('100').field.name == 'VALUE_100'
+    assert enum.find_member('0').field.name == 'VALUE_0'
+
+    # Test with non-existent values
+    assert enum.find_member(999) is None
+    assert enum.find_member('999') is None
+
+
+def test_find_member_with_string_enum():
+    from datamodel_code_generator.model.enum import Enum
+    from datamodel_code_generator.model.pydantic.base_model import DataModelField
+    from datamodel_code_generator.reference import Reference
+    from datamodel_code_generator.types import DataType
+
+    enum = Enum(
+        reference=Reference(
+            path='test_path', original_name='TestEnum', name='TestEnum'
+        ),
+        fields=[
+            DataModelField(
+                name='VALUE_A',
+                default="'value_a'",
+                data_type=DataType(type='str'),
+                required=True,
+            ),
+            DataModelField(
+                name='VALUE_B',
+                default="'value_b'",
+                data_type=DataType(type='str'),
+                required=True,
+            ),
+        ],
+    )
+
+    member = enum.find_member('value_a')
+    assert member is not None
+    assert member.field.name == 'VALUE_A'
+
+    member = enum.find_member('value_b')
+    assert member is not None
+    assert member.field.name == 'VALUE_B'
+
+    member = enum.find_member("'value_a'")
+    assert member is not None
+    assert member.field.name == 'VALUE_A'
+
+
+def test_find_member_with_mixed_enum():
+    from datamodel_code_generator.model.enum import Enum
+    from datamodel_code_generator.model.pydantic.base_model import DataModelField
+    from datamodel_code_generator.reference import Reference
+    from datamodel_code_generator.types import DataType
+
+    enum = Enum(
+        reference=Reference(
+            path='test_path', original_name='TestEnum', name='TestEnum'
+        ),
+        fields=[
+            DataModelField(
+                name='INT_VALUE',
+                default='100',
+                data_type=DataType(type='int'),
+                required=True,
+            ),
+            DataModelField(
+                name='STR_VALUE',
+                default="'value_a'",
+                data_type=DataType(type='str'),
+                required=True,
+            ),
+        ],
+    )
+
+    member = enum.find_member(100)
+    assert member is not None
+    assert member.field.name == 'INT_VALUE'
+
+    member = enum.find_member('100')
+    assert member is not None
+    assert member.field.name == 'INT_VALUE'
+
+    member = enum.find_member('value_a')
+    assert member is not None
+    assert member.field.name == 'STR_VALUE'
+
+    member = enum.find_member("'value_a'")
+    assert member is not None
+    assert member.field.name == 'STR_VALUE'
+
+
 @pytest.fixture
 def escape_map() -> Dict[str, str]:
     return {