Skip to content

Commit 598478b

Browse files
Support enums
1 parent 49753fa commit 598478b

File tree

8 files changed

+92
-15
lines changed

8 files changed

+92
-15
lines changed

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

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

33
from .a_model import AModel
44
from .all_of_sub_model import AllOfSubModel
5+
from .all_of_sub_model_type_enum import AllOfSubModelTypeEnum
56
from .an_all_of_enum import AnAllOfEnum
67
from .an_enum import AnEnum
78
from .an_int_enum import AnIntEnum
89
from .another_all_of_sub_model import AnotherAllOfSubModel
910
from .another_all_of_sub_model_type import AnotherAllOfSubModelType
11+
from .another_all_of_sub_model_type_enum import AnotherAllOfSubModelTypeEnum
1012
from .body_upload_file_tests_upload_post import BodyUploadFileTestsUploadPost
1113
from .different_enum import DifferentEnum
1214
from .free_form_model import FreeFormModel

end_to_end_tests/golden-record/my_test_api_client/models/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.all_of_sub_model_type_enum import AllOfSubModelTypeEnum
56
from ..types import UNSET, Unset
67

78
T = TypeVar("T", bound="AllOfSubModel")
@@ -13,11 +14,15 @@ class AllOfSubModel:
1314

1415
a_sub_property: Union[Unset, str] = UNSET
1516
type: Union[Unset, str] = UNSET
17+
type_enum: Union[Unset, AllOfSubModelTypeEnum] = 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
2022
type = self.type
23+
type_enum: Union[Unset, int] = UNSET
24+
if not isinstance(self.type_enum, Unset):
25+
type_enum = self.type_enum.value
2126

2227
field_dict: Dict[str, Any] = {}
2328
field_dict.update(self.additional_properties)
@@ -26,6 +31,8 @@ def to_dict(self) -> Dict[str, Any]:
2631
field_dict["a_sub_property"] = a_sub_property
2732
if type is not UNSET:
2833
field_dict["type"] = type
34+
if type_enum is not UNSET:
35+
field_dict["type_enum"] = type_enum
2936

3037
return field_dict
3138

@@ -36,9 +43,15 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
3643

3744
type = d.pop("type", UNSET)
3845

46+
type_enum: Union[Unset, AllOfSubModelTypeEnum] = UNSET
47+
_type_enum = d.pop("type_enum", UNSET)
48+
if not isinstance(_type_enum, Unset):
49+
type_enum = AllOfSubModelTypeEnum(_type_enum)
50+
3951
all_of_sub_model = cls(
4052
a_sub_property=a_sub_property,
4153
type=type,
54+
type_enum=type_enum,
4255
)
4356

4457
all_of_sub_model.additional_properties = d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from enum import IntEnum
2+
3+
4+
class AllOfSubModelTypeEnum(IntEnum):
5+
VALUE_0 = 0
6+
VALUE_1 = 1
7+
8+
def __str__(self) -> str:
9+
return str(self.value)

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

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import attr
44

55
from ..models.another_all_of_sub_model_type import AnotherAllOfSubModelType
6+
from ..models.another_all_of_sub_model_type_enum import AnotherAllOfSubModelTypeEnum
67
from ..types import UNSET, Unset
78

89
T = TypeVar("T", bound="AnotherAllOfSubModel")
@@ -14,6 +15,7 @@ class AnotherAllOfSubModel:
1415

1516
another_sub_property: Union[Unset, str] = UNSET
1617
type: Union[Unset, AnotherAllOfSubModelType] = UNSET
18+
type_enum: Union[Unset, AnotherAllOfSubModelTypeEnum] = UNSET
1719
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
1820

1921
def to_dict(self) -> Dict[str, Any]:
@@ -22,13 +24,19 @@ def to_dict(self) -> Dict[str, Any]:
2224
if not isinstance(self.type, Unset):
2325
type = self.type.value
2426

27+
type_enum: Union[Unset, int] = UNSET
28+
if not isinstance(self.type_enum, Unset):
29+
type_enum = self.type_enum.value
30+
2531
field_dict: Dict[str, Any] = {}
2632
field_dict.update(self.additional_properties)
2733
field_dict.update({})
2834
if another_sub_property is not UNSET:
2935
field_dict["another_sub_property"] = another_sub_property
3036
if type is not UNSET:
3137
field_dict["type"] = type
38+
if type_enum is not UNSET:
39+
field_dict["type_enum"] = type_enum
3240

3341
return field_dict
3442

@@ -42,9 +50,15 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
4250
if not isinstance(_type, Unset):
4351
type = AnotherAllOfSubModelType(_type)
4452

53+
type_enum: Union[Unset, AnotherAllOfSubModelTypeEnum] = UNSET
54+
_type_enum = d.pop("type_enum", UNSET)
55+
if not isinstance(_type_enum, Unset):
56+
type_enum = AnotherAllOfSubModelTypeEnum(_type_enum)
57+
4558
another_all_of_sub_model = cls(
4659
another_sub_property=another_sub_property,
4760
type=type,
61+
type_enum=type_enum,
4862
)
4963

5064
another_all_of_sub_model.additional_properties = d
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from enum import IntEnum
2+
3+
4+
class AnotherAllOfSubModelTypeEnum(IntEnum):
5+
VALUE_0 = 0
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
@@ -3,6 +3,7 @@
33
import attr
44

55
from ..models.another_all_of_sub_model_type import AnotherAllOfSubModelType
6+
from ..models.another_all_of_sub_model_type_enum import AnotherAllOfSubModelTypeEnum
67
from ..types import UNSET, Unset
78

89
T = TypeVar("T", bound="ModelFromAllOf")
@@ -14,6 +15,7 @@ class ModelFromAllOf:
1415

1516
a_sub_property: Union[Unset, str] = UNSET
1617
type: Union[Unset, AnotherAllOfSubModelType] = UNSET
18+
type_enum: Union[Unset, AnotherAllOfSubModelTypeEnum] = UNSET
1719
another_sub_property: Union[Unset, str] = UNSET
1820
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
1921

@@ -23,6 +25,10 @@ def to_dict(self) -> Dict[str, Any]:
2325
if not isinstance(self.type, Unset):
2426
type = self.type.value
2527

28+
type_enum: Union[Unset, int] = UNSET
29+
if not isinstance(self.type_enum, Unset):
30+
type_enum = self.type_enum.value
31+
2632
another_sub_property = self.another_sub_property
2733

2834
field_dict: Dict[str, Any] = {}
@@ -32,6 +38,8 @@ def to_dict(self) -> Dict[str, Any]:
3238
field_dict["a_sub_property"] = a_sub_property
3339
if type is not UNSET:
3440
field_dict["type"] = type
41+
if type_enum is not UNSET:
42+
field_dict["type_enum"] = type_enum
3543
if another_sub_property is not UNSET:
3644
field_dict["another_sub_property"] = another_sub_property
3745

@@ -47,11 +55,17 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
4755
if not isinstance(_type, Unset):
4856
type = AnotherAllOfSubModelType(_type)
4957

58+
type_enum: Union[Unset, AnotherAllOfSubModelTypeEnum] = UNSET
59+
_type_enum = d.pop("type_enum", UNSET)
60+
if not isinstance(_type_enum, Unset):
61+
type_enum = AnotherAllOfSubModelTypeEnum(_type_enum)
62+
5063
another_sub_property = d.pop("another_sub_property", UNSET)
5164

5265
model_from_all_of = cls(
5366
a_sub_property=a_sub_property,
5467
type=type,
68+
type_enum=type_enum,
5569
another_sub_property=another_sub_property,
5670
)
5771

end_to_end_tests/openapi.json

+8
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,10 @@
11761176
},
11771177
"type": {
11781178
"type": "string"
1179+
},
1180+
"type_enum": {
1181+
"type": "int",
1182+
"enum": [0, 1]
11791183
}
11801184
}
11811185
},
@@ -1189,6 +1193,10 @@
11891193
"type": {
11901194
"type": "string",
11911195
"enum": ["submodel"]
1196+
},
1197+
"type_enum": {
1198+
"type": "int",
1199+
"enum": [0]
11921200
}
11931201
}
11941202
},

openapi_python_client/parser/properties/model_property.py

+24-15
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,43 @@ def get_imports(self, *, prefix: str) -> Set[str]:
4848
return imports
4949

5050

51+
def _is_string_enum(prop: Property) -> bool:
52+
return prop.__class__.__name__ == "EnumProperty" and prop.value_type == str
53+
54+
55+
def _is_int_enum(prop: Property) -> bool:
56+
return prop.__class__.__name__ == "EnumProperty" and prop.value_type == int
57+
58+
5159
def _is_subtype(first: Property, second: Property) -> bool:
52-
return (
53-
first.__class__.__name__ == "EnumProperty"
54-
and first.value_type == str
55-
and second.__class__.__name__ == "StringProperty"
56-
) or (
57-
first.__class__.__name__ == "EnumProperty"
58-
and first.value_type == int
59-
and second.__class__.__name__ == "IntProperty"
60+
return any(
61+
[
62+
_is_string_enum(first) and second.__class__.__name__ == "StringProperty",
63+
_is_int_enum(first) and second.__class__.__name__ == "IntProperty",
64+
_is_string_enum(first)
65+
and _is_string_enum(second)
66+
and set(first.values.items()) <= set(second.values.items()),
67+
_is_int_enum(first) and _is_int_enum(second) and set(first.values.items()) <= set(second.values.items()),
68+
]
6069
)
6170

6271

6372
def _merge_properties(first: Property, second: Property) -> Union[Property, PropertyError]:
6473
nullable = first.nullable and second.nullable
6574
required = first.required or second.required
6675

67-
if first.__class__ == second.__class__:
68-
first = attr.evolve(first, nullable=nullable, required=required)
69-
second = attr.evolve(second, nullable=nullable, required=required)
70-
if first != second:
71-
return PropertyError(header="Cannot merge properties", detail="Properties has conflicting values")
72-
return first
73-
elif _is_subtype(first, second):
76+
if _is_subtype(first, second):
7477
first = attr.evolve(first, nullable=nullable, required=required)
7578
return first
7679
elif _is_subtype(second, first):
7780
second = attr.evolve(second, nullable=nullable, required=required)
7881
return second
82+
elif first.__class__ == second.__class__:
83+
first = attr.evolve(first, nullable=nullable, required=required)
84+
second = attr.evolve(second, nullable=nullable, required=required)
85+
if first != second:
86+
return PropertyError(header="Cannot merge properties", detail="Properties has conflicting values")
87+
return first
7988
else:
8089
return PropertyError(
8190
header="Cannot merge properties",

0 commit comments

Comments
 (0)