Skip to content

Commit f4b074f

Browse files
authored
fix: skip non-model types in __change_field_name (#2717)
1 parent 3e7b16b commit f4b074f

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

src/datamodel_code_generator/parser/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,8 @@ def __change_field_name(
18051805
for model in models:
18061806
if "Enum" in model.base_class:
18071807
continue
1808+
if not model.BASE_CLASS:
1809+
continue
18081810

18091811
for field in model.fields:
18101812
filed_name = field.name
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# generated by datamodel-codegen:
2+
# filename: union.graphql
3+
# timestamp: 2019-07-26T00:00:00+00:00
4+
5+
from __future__ import annotations
6+
7+
from typing import Literal, Union
8+
9+
from pydantic import BaseModel, Field
10+
from typing_extensions import TypeAliasType
11+
12+
Boolean = TypeAliasType("Boolean", bool)
13+
"""
14+
The `Boolean` scalar type represents `true` or `false`.
15+
"""
16+
17+
18+
ID = TypeAliasType("ID", str)
19+
"""
20+
The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID.
21+
"""
22+
23+
24+
Int = TypeAliasType("Int", int)
25+
"""
26+
The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.
27+
"""
28+
29+
30+
String = TypeAliasType("String", str)
31+
"""
32+
The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
33+
"""
34+
35+
36+
class IResource(BaseModel):
37+
id: ID
38+
typename__: Literal['IResource'] | None = Field('IResource', alias='__typename')
39+
40+
41+
class Car(IResource):
42+
id: ID
43+
passenger_capacity: Int = Field(..., alias='passengerCapacity')
44+
typename__: Literal['Car'] | None = Field('Car', alias='__typename')
45+
46+
47+
class Employee(IResource):
48+
first_name: String | None = Field(None, alias='firstName')
49+
id: ID
50+
last_name: String | None = Field(None, alias='lastName')
51+
typename__: Literal['Employee'] | None = Field('Employee', alias='__typename')
52+
53+
54+
Resource = TypeAliasType(
55+
"Resource",
56+
Union[
57+
'Car',
58+
'Employee',
59+
],
60+
)
61+
62+
63+
TechnicalResource = TypeAliasType("TechnicalResource", Car)

tests/main/graphql/test_main_graphql.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,15 @@ def test_main_graphql_dataclass_frozen_keyword_only(output_file: Path) -> None:
601601
"3.10",
602602
],
603603
)
604+
605+
606+
def test_main_graphql_union_snake_case_field(output_file: Path) -> None:
607+
"""Test that union type references are not converted to snake_case."""
608+
run_main_and_assert(
609+
input_path=GRAPHQL_DATA_PATH / "union.graphql",
610+
output_path=output_file,
611+
input_file_type="graphql",
612+
assert_func=assert_file_content,
613+
expected_file="union_snake_case_field.py",
614+
extra_args=["--snake-case-field", "--output-model-type", "pydantic_v2.BaseModel"],
615+
)

0 commit comments

Comments
 (0)