Skip to content

Commit 9b7863d

Browse files
authored
Fix enum default values to use short names in nested package paths (#2644)
1 parent b9cf6f9 commit 9b7863d

5 files changed

Lines changed: 87 additions & 1 deletion

File tree

src/datamodel_code_generator/model/enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,4 @@ def __init__(self, enum: Enum, field: DataModelFieldBase) -> None:
156156

157157
def __repr__(self) -> str:
158158
"""Return string representation of enum member."""
159-
return f"{self.alias or self.enum.name}.{self.field.name}"
159+
return f"{self.alias or self.enum.class_name}.{self.field.name}"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# generated by datamodel-codegen:
2+
# filename: nested_package_enum_default.json
3+
# timestamp: 1985-10-26T08:21:00+00:00
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# generated by datamodel-codegen:
2+
# filename: nested_package_enum_default.json
3+
# timestamp: 1985-10-26T08:21:00+00:00
4+
5+
from __future__ import annotations
6+
7+
from dataclasses import dataclass
8+
from enum import Enum
9+
from typing import Optional
10+
11+
12+
class Resolution(Enum):
13+
Required = 'Required'
14+
Optional = 'Optional'
15+
16+
17+
class Policy(Enum):
18+
Allow = 'Allow'
19+
Deny = 'Deny'
20+
21+
22+
@dataclass
23+
class BucketSpec:
24+
resolution: Optional[Resolution] = Resolution.Required
25+
policy: Optional[Policy] = Policy.Allow
26+
name: Optional[str] = None
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "Test API",
5+
"version": "1.0.0"
6+
},
7+
"paths": {},
8+
"components": {
9+
"schemas": {
10+
"io.example.api.v1.Resolution": {
11+
"type": "string",
12+
"enum": ["Required", "Optional"],
13+
"default": "Required"
14+
},
15+
"io.example.api.v1.Policy": {
16+
"type": "string",
17+
"enum": ["Allow", "Deny"],
18+
"default": "Allow"
19+
},
20+
"io.example.api.v1.BucketSpec": {
21+
"type": "object",
22+
"properties": {
23+
"resolution": {
24+
"allOf": [
25+
{"$ref": "#/components/schemas/io.example.api.v1.Resolution"}
26+
],
27+
"default": "Required"
28+
},
29+
"policy": {
30+
"allOf": [
31+
{"$ref": "#/components/schemas/io.example.api.v1.Policy"}
32+
],
33+
"default": "Allow"
34+
},
35+
"name": {
36+
"type": "string"
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}

tests/main/openapi/test_main_openapi.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3318,3 +3318,18 @@ def test_main_allof_enum_ref(output_file: Path) -> None:
33183318
input_file_type=None,
33193319
assert_func=assert_file_content,
33203320
)
3321+
3322+
3323+
def test_main_nested_package_enum_default(output_dir: Path) -> None:
3324+
"""Test enum default values use short names in same module with nested package paths."""
3325+
with freeze_time(TIMESTAMP):
3326+
run_main_and_assert(
3327+
input_path=OPEN_API_DATA_PATH / "nested_package_enum_default.json",
3328+
output_path=output_dir,
3329+
expected_directory=EXPECTED_OPENAPI_PATH / "nested_package_enum_default",
3330+
extra_args=[
3331+
"--output-model-type",
3332+
"dataclasses.dataclass",
3333+
"--set-default-enum-member",
3334+
],
3335+
)

0 commit comments

Comments
 (0)