Skip to content

Commit 7459155

Browse files
Allof enum and string or int
1 parent 032a4a4 commit 7459155

File tree

7 files changed

+75
-7
lines changed

7 files changed

+75
-7
lines changed

end_to_end_tests/golden-record/my_test_api_client/models/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .an_enum import AnEnum
77
from .an_int_enum import AnIntEnum
88
from .another_all_of_sub_model import AnotherAllOfSubModel
9+
from .another_all_of_sub_model_type import AnotherAllOfSubModelType
910
from .body_upload_file_tests_upload_post import BodyUploadFileTestsUploadPost
1011
from .different_enum import DifferentEnum
1112
from .free_form_model import FreeFormModel

end_to_end_tests/golden-record/my_test_api_client/models/all_of_sub_model.py

+7
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@ class AllOfSubModel:
1212
""" """
1313

1414
a_sub_property: Union[Unset, str] = UNSET
15+
type: Union[Unset, str] = UNSET
1516
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
1617

1718
def to_dict(self) -> Dict[str, Any]:
1819
a_sub_property = self.a_sub_property
20+
type = self.type
1921

2022
field_dict: Dict[str, Any] = {}
2123
field_dict.update(self.additional_properties)
2224
field_dict.update({})
2325
if a_sub_property is not UNSET:
2426
field_dict["a_sub_property"] = a_sub_property
27+
if type is not UNSET:
28+
field_dict["type"] = type
2529

2630
return field_dict
2731

@@ -30,8 +34,11 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
3034
d = src_dict.copy()
3135
a_sub_property = d.pop("a_sub_property", UNSET)
3236

37+
type = d.pop("type", UNSET)
38+
3339
all_of_sub_model = cls(
3440
a_sub_property=a_sub_property,
41+
type=type,
3542
)
3643

3744
all_of_sub_model.additional_properties = d

end_to_end_tests/golden-record/my_test_api_client/models/another_all_of_sub_model.py

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import attr
44

5+
from ..models.another_all_of_sub_model_type import AnotherAllOfSubModelType
56
from ..types import UNSET, Unset
67

78
T = TypeVar("T", bound="AnotherAllOfSubModel")
@@ -12,16 +13,22 @@ class AnotherAllOfSubModel:
1213
""" """
1314

1415
another_sub_property: Union[Unset, str] = UNSET
16+
type: Union[Unset, AnotherAllOfSubModelType] = UNSET
1517
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
1618

1719
def to_dict(self) -> Dict[str, Any]:
1820
another_sub_property = self.another_sub_property
21+
type: Union[Unset, str] = UNSET
22+
if not isinstance(self.type, Unset):
23+
type = self.type.value
1924

2025
field_dict: Dict[str, Any] = {}
2126
field_dict.update(self.additional_properties)
2227
field_dict.update({})
2328
if another_sub_property is not UNSET:
2429
field_dict["another_sub_property"] = another_sub_property
30+
if type is not UNSET:
31+
field_dict["type"] = type
2532

2633
return field_dict
2734

@@ -30,8 +37,14 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
3037
d = src_dict.copy()
3138
another_sub_property = d.pop("another_sub_property", UNSET)
3239

40+
type: Union[Unset, AnotherAllOfSubModelType] = UNSET
41+
_type = d.pop("type", UNSET)
42+
if not isinstance(_type, Unset):
43+
type = AnotherAllOfSubModelType(_type)
44+
3345
another_all_of_sub_model = cls(
3446
another_sub_property=another_sub_property,
47+
type=type,
3548
)
3649

3750
another_all_of_sub_model.additional_properties = d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from enum import Enum
2+
3+
4+
class AnotherAllOfSubModelType(str, Enum):
5+
SUBMODEL = "submodel"
6+
7+
def __str__(self) -> str:
8+
return str(self.value)

end_to_end_tests/golden-record/my_test_api_client/models/model_from_all_of.py

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import attr
44

5+
from ..models.another_all_of_sub_model_type import AnotherAllOfSubModelType
56
from ..types import UNSET, Unset
67

78
T = TypeVar("T", bound="ModelFromAllOf")
@@ -12,18 +13,25 @@ class ModelFromAllOf:
1213
""" """
1314

1415
a_sub_property: Union[Unset, str] = UNSET
16+
type: Union[Unset, AnotherAllOfSubModelType] = UNSET
1517
another_sub_property: Union[Unset, str] = UNSET
1618
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
1719

1820
def to_dict(self) -> Dict[str, Any]:
1921
a_sub_property = self.a_sub_property
22+
type: Union[Unset, str] = UNSET
23+
if not isinstance(self.type, Unset):
24+
type = self.type.value
25+
2026
another_sub_property = self.another_sub_property
2127

2228
field_dict: Dict[str, Any] = {}
2329
field_dict.update(self.additional_properties)
2430
field_dict.update({})
2531
if a_sub_property is not UNSET:
2632
field_dict["a_sub_property"] = a_sub_property
33+
if type is not UNSET:
34+
field_dict["type"] = type
2735
if another_sub_property is not UNSET:
2836
field_dict["another_sub_property"] = another_sub_property
2937

@@ -34,10 +42,16 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
3442
d = src_dict.copy()
3543
a_sub_property = d.pop("a_sub_property", UNSET)
3644

45+
type: Union[Unset, AnotherAllOfSubModelType] = UNSET
46+
_type = d.pop("type", UNSET)
47+
if not isinstance(_type, Unset):
48+
type = AnotherAllOfSubModelType(_type)
49+
3750
another_sub_property = d.pop("another_sub_property", UNSET)
3851

3952
model_from_all_of = cls(
4053
a_sub_property=a_sub_property,
54+
type=type,
4155
another_sub_property=another_sub_property,
4256
)
4357

end_to_end_tests/openapi.json

+7
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,9 @@
11731173
"properties": {
11741174
"a_sub_property": {
11751175
"type": "string"
1176+
},
1177+
"type": {
1178+
"type": "string"
11761179
}
11771180
}
11781181
},
@@ -1182,6 +1185,10 @@
11821185
"properties": {
11831186
"another_sub_property": {
11841187
"type": "string"
1188+
},
1189+
"type": {
1190+
"type": "string",
1191+
"enum": ["submodel"]
11851192
}
11861193
}
11871194
},

openapi_python_client/parser/properties/model_property.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ... import schema as oai
88
from ... import utils
99
from ..errors import ParseError, PropertyError
10+
from .enum_property import EnumProperty
1011
from .property import Property
1112
from .schemas import Class, Schemas, parse_reference_path
1213

@@ -49,15 +50,32 @@ def get_imports(self, *, prefix: str) -> Set[str]:
4950

5051

5152
def _merge_properties(first: Property, second: Property) -> Union[Property, PropertyError]:
52-
if first.__class__ != second.__class__:
53-
return PropertyError(header="Cannot merge properties", detail="Properties are two different types")
5453
nullable = first.nullable and second.nullable
5554
required = first.required or second.required
56-
first = attr.evolve(first, nullable=nullable, required=required)
57-
second = attr.evolve(second, nullable=nullable, required=required)
58-
if first != second:
59-
return PropertyError(header="Cannot merge properties", detail="Properties has conflicting values")
60-
return first
55+
56+
if first.__class__ == second.__class__:
57+
first = attr.evolve(first, nullable=nullable, required=required)
58+
second = attr.evolve(second, nullable=nullable, required=required)
59+
if first != second:
60+
return PropertyError(header="Cannot merge properties", detail="Properties has conflicting values")
61+
return first
62+
elif first.__class__.__name__ == "StringProperty" and second.__class__ == EnumProperty and second.value_type == str:
63+
second = attr.evolve(second, nullable=nullable, required=required)
64+
return second
65+
elif second.__class__.__name__ == "StringProperty" and first.__class__ == EnumProperty and first.value_type == str:
66+
first = attr.evolve(first, nullable=nullable, required=required)
67+
return first
68+
elif first.__class__.__name__ == "IntProperty" and second.__class__ == EnumProperty and second.value_type == int:
69+
second = attr.evolve(second, nullable=nullable, required=required)
70+
return second
71+
elif second.__class__.__name__ == "IntProperty" and first.__class__ == EnumProperty and first.value_type == int:
72+
first = attr.evolve(first, nullable=nullable, required=required)
73+
return first
74+
else:
75+
return PropertyError(
76+
header="Cannot merge properties",
77+
detail=f"{first.__class__}, {second.__class__}Properties have incompatible types",
78+
)
6179

6280

6381
class _PropertyData(NamedTuple):

0 commit comments

Comments
 (0)