Skip to content

Commit 103acab

Browse files
authored
Fixes to union properties (#241)
* Fix indentation in union property template when calling out to the template of an inner property * Update templates construct to allow different initial value, use in union property
1 parent b61cbe6 commit 103acab

File tree

18 files changed

+183
-47
lines changed

18 files changed

+183
-47
lines changed

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/defaults_tests_defaults_post.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def httpx_request(
7777

7878
json_enum_prop: Union[Unset, AnEnum] = UNSET
7979
if not isinstance(enum_prop, Unset):
80-
json_enum_prop = enum_prop.value
80+
json_enum_prop = enum_prop
8181

8282
params: Dict[str, Any] = {}
8383
if string_prop is not UNSET:

end_to_end_tests/golden-record-custom/custom_e2e/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .body_upload_file_tests_upload_post import BodyUploadFileTestsUploadPost
77
from .different_enum import DifferentEnum
88
from .http_validation_error import HTTPValidationError
9+
from .model_with_union_property import ModelWithUnionProperty
910
from .test_inline_objectsjson_body import TestInlineObjectsjsonBody
1011
from .test_inline_objectsresponse_200 import TestInlineObjectsresponse_200
1112
from .validation_error import ValidationError

end_to_end_tests/golden-record-custom/custom_e2e/models/a_model.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,16 @@ def to_dict(self) -> Dict[str, Any]:
7474
def from_dict(d: Dict[str, Any]) -> "AModel":
7575
an_enum_value = AnEnum(d["an_enum_value"])
7676

77-
def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime.datetime, datetime.date]:
77+
def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.date]:
78+
data = None if isinstance(data, Unset) else data
7879
a_camel_date_time: Union[datetime.datetime, datetime.date]
7980
try:
80-
a_camel_date_time = isoparse(d["aCamelDateTime"])
81+
a_camel_date_time = isoparse(data)
8182

8283
return a_camel_date_time
8384
except: # noqa: E722
8485
pass
85-
a_camel_date_time = isoparse(d["aCamelDateTime"]).date()
86+
a_camel_date_time = isoparse(data).date()
8687

8788
return a_camel_date_time
8889

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from typing import Any, Dict, Union
2+
3+
import attr
4+
5+
from ..models.an_enum import AnEnum
6+
from ..models.an_int_enum import AnIntEnum
7+
from ..types import UNSET, Unset
8+
9+
10+
@attr.s(auto_attribs=True)
11+
class ModelWithUnionProperty:
12+
""" """
13+
14+
a_property: Union[Unset, AnEnum, AnIntEnum] = UNSET
15+
16+
def to_dict(self) -> Dict[str, Any]:
17+
a_property: Union[Unset, AnEnum, AnIntEnum]
18+
if isinstance(self.a_property, Unset):
19+
a_property = UNSET
20+
elif isinstance(self.a_property, AnEnum):
21+
a_property = UNSET
22+
if not isinstance(self.a_property, Unset):
23+
a_property = self.a_property
24+
25+
else:
26+
a_property = UNSET
27+
if not isinstance(self.a_property, Unset):
28+
a_property = self.a_property
29+
30+
field_dict = {}
31+
if a_property is not UNSET:
32+
field_dict["a_property"] = a_property
33+
34+
return field_dict
35+
36+
@staticmethod
37+
def from_dict(d: Dict[str, Any]) -> "ModelWithUnionProperty":
38+
def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
39+
data = None if isinstance(data, Unset) else data
40+
a_property: Union[Unset, AnEnum, AnIntEnum]
41+
try:
42+
a_property = UNSET
43+
if data is not None:
44+
a_property = AnEnum(data)
45+
46+
return a_property
47+
except: # noqa: E722
48+
pass
49+
a_property = UNSET
50+
if data is not None:
51+
a_property = AnIntEnum(data)
52+
53+
return a_property
54+
55+
a_property = _parse_a_property(d.get("a_property", UNSET))
56+
57+
return ModelWithUnionProperty(
58+
a_property=a_property,
59+
)

end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def _get_kwargs(
5353

5454
json_enum_prop: Union[Unset, AnEnum] = UNSET
5555
if not isinstance(enum_prop, Unset):
56-
json_enum_prop = enum_prop.value
56+
json_enum_prop = enum_prop
5757

5858
params: Dict[str, Any] = {}
5959
if string_prop is not UNSET:

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .body_upload_file_tests_upload_post import BodyUploadFileTestsUploadPost
77
from .different_enum import DifferentEnum
88
from .http_validation_error import HTTPValidationError
9+
from .model_with_union_property import ModelWithUnionProperty
910
from .test_inline_objectsjson_body import TestInlineObjectsjsonBody
1011
from .test_inline_objectsresponse_200 import TestInlineObjectsresponse_200
1112
from .validation_error import ValidationError

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,16 @@ def to_dict(self) -> Dict[str, Any]:
7474
def from_dict(d: Dict[str, Any]) -> "AModel":
7575
an_enum_value = AnEnum(d["an_enum_value"])
7676

77-
def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime.datetime, datetime.date]:
77+
def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.date]:
78+
data = None if isinstance(data, Unset) else data
7879
a_camel_date_time: Union[datetime.datetime, datetime.date]
7980
try:
80-
a_camel_date_time = isoparse(d["aCamelDateTime"])
81+
a_camel_date_time = isoparse(data)
8182

8283
return a_camel_date_time
8384
except: # noqa: E722
8485
pass
85-
a_camel_date_time = isoparse(d["aCamelDateTime"]).date()
86+
a_camel_date_time = isoparse(data).date()
8687

8788
return a_camel_date_time
8889

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from typing import Any, Dict, Union
2+
3+
import attr
4+
5+
from ..models.an_enum import AnEnum
6+
from ..models.an_int_enum import AnIntEnum
7+
from ..types import UNSET, Unset
8+
9+
10+
@attr.s(auto_attribs=True)
11+
class ModelWithUnionProperty:
12+
""" """
13+
14+
a_property: Union[Unset, AnEnum, AnIntEnum] = UNSET
15+
16+
def to_dict(self) -> Dict[str, Any]:
17+
a_property: Union[Unset, AnEnum, AnIntEnum]
18+
if isinstance(self.a_property, Unset):
19+
a_property = UNSET
20+
elif isinstance(self.a_property, AnEnum):
21+
a_property = UNSET
22+
if not isinstance(self.a_property, Unset):
23+
a_property = self.a_property
24+
25+
else:
26+
a_property = UNSET
27+
if not isinstance(self.a_property, Unset):
28+
a_property = self.a_property
29+
30+
field_dict = {}
31+
if a_property is not UNSET:
32+
field_dict["a_property"] = a_property
33+
34+
return field_dict
35+
36+
@staticmethod
37+
def from_dict(d: Dict[str, Any]) -> "ModelWithUnionProperty":
38+
def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
39+
data = None if isinstance(data, Unset) else data
40+
a_property: Union[Unset, AnEnum, AnIntEnum]
41+
try:
42+
a_property = UNSET
43+
if data is not None:
44+
a_property = AnEnum(data)
45+
46+
return a_property
47+
except: # noqa: E722
48+
pass
49+
a_property = UNSET
50+
if data is not None:
51+
a_property = AnIntEnum(data)
52+
53+
return a_property
54+
55+
a_property = _parse_a_property(d.get("a_property", UNSET))
56+
57+
return ModelWithUnionProperty(
58+
a_property=a_property,
59+
)

end_to_end_tests/openapi.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,18 @@
727727
"type": "string"
728728
}
729729
}
730+
},
731+
"ModelWithUnionProperty": {
732+
"title": "ModelWithUnionProperty",
733+
"type": "object",
734+
"properties": {
735+
"a_property": {
736+
"oneOf": [
737+
{"$ref": "#/components/schemas/AnEnum"},
738+
{"$ref": "#/components/schemas/AnIntEnum"}
739+
]
740+
}
741+
}
730742
}
731743
}
732744
}

openapi_python_client/templates/property_templates/date_property.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
{% macro construct(property, source) %}
1+
{% macro construct(property, source, initial_value="None") %}
22
{% if property.required %}
33
{{ property.python_name }} = isoparse({{ source }}).date()
44
{% else %}
5-
{{ property.python_name }} = None
5+
{{ property.python_name }} = {{ initial_value }}
66
if {{ source }} is not None:
77
{{ property.python_name }} = isoparse(cast(str, {{ source }})).date()
88
{% endif %}
99
{% endmacro %}
1010

11-
{% macro transform(property, source, destination) %}
11+
{% macro transform(property, source, destination, declare_type=True) %}
1212
{% if property.required %}
1313
{% if property.nullable %}
1414
{{ destination }} = {{ source }}.isoformat() if {{ source }} else None
1515
{% else %}
1616
{{ destination }} = {{ source }}.isoformat()
1717
{% endif %}
1818
{% else %}
19-
{{ destination }}: Union[Unset, str] = UNSET
19+
{{ destination }}{% if declare_type %}: Union[Unset, str]{% endif %} = UNSET
2020
if not isinstance({{ source }}, Unset):
2121
{% if property.nullable %}
2222
{{ destination }} = {{ source }}.isoformat() if {{ source }} else None

0 commit comments

Comments
 (0)