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

18 files changed

+183
-47
lines changed

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

+1-1
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

+1
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

+4-3
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

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

+1-1
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

+1
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

+4-3
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

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

+12
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

+4-4
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

openapi_python_client/templates/property_templates/datetime_property.pyi

+4-4
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 }})
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 }}))
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

openapi_python_client/templates/property_templates/dict_property.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
{% macro construct(property, source) %}
1+
{% macro construct(property, source, initial_value="None") %}
22
{% if property.required %}
33
{{ property.python_name }} = {{ source }}
44
{% else %}
5-
{{ property.python_name }} = None
5+
{{ property.python_name }} = {{ initial_value }}
66
if {{ source }} is not None:
77
{{ property.python_name }} = {{ source }}
88
{% endif %}
99
{% endmacro %}
1010

11-
{% macro transform(property, source, destination) %}
11+
{% macro transform(property, source, destination, declare_type=True) %}
1212
{% if property.nullable %}
1313
{{ destination }} = {{ source }} if {{ source }} else None
1414
{% else %}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
{% macro construct(property, source) %}
1+
{% macro construct(property, source, initial_value="None") %}
22
{% if property.required %}
33
{{ property.python_name }} = {{ property.reference.class_name }}({{ source }})
44
{% else %}
5-
{{ property.python_name }} = None
5+
{{ property.python_name }} = {{ initial_value }}
66
if {{ source }} is not None:
77
{{ property.python_name }} = {{ property.reference.class_name }}({{ source }})
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 }}.value if {{ source }} else None
1515
{% else %}
1616
{{ destination }} = {{ source }}.value
1717
{% endif %}
1818
{% else %}
19-
{{ destination }}: {{ property.get_type_string() }} = UNSET
19+
{{ destination }}{% if declare_type %}: {{ property.get_type_string() }}{% endif %} = UNSET
2020
if not isinstance({{ source }}, Unset):
2121
{% if property.nullable %}
22-
{{ destination }} = {{ source }}.value if {{ source }} else None
22+
{{ destination }} = {{ source }} if {{ source }} else None
2323
{% else %}
24-
{{ destination }} = {{ source }}.value
24+
{{ destination }} = {{ source }}
2525
{% endif %}
2626
{% endif %}
2727
{% endmacro %}

openapi_python_client/templates/property_templates/file_property.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
{% macro construct(property, source) %}
1+
{% macro construct(property, source, initial_value=None) %}
22
{{ property.python_name }} = File(
33
payload = BytesIO({{ source }})
44
)
55
{% endmacro %}
66

7-
{% macro transform(property, source, destination) %}
7+
{% macro transform(property, source, destination, declare_type=True) %}
88
{% if property.required %}
99
{% if property.nullable %}
1010
{{ destination }} = {{ source }}.to_tuple() if {{ source }} else None
1111
{% else %}
1212
{{ destination }} = {{ source }}.to_tuple()
1313
{% endif %}
1414
{% else %}
15-
{{ destination }}: {{ property.get_type_string() }} = UNSET
15+
{{ destination }}{% if declare_type %}: {{ property.get_type_string() }}{% endif %} = UNSET
1616
if not isinstance({{ source }}, Unset):
1717
{% if property.nullable %}
1818
{{ destination }} = {{ source }}.to_tuple() if {{ source }} else None

openapi_python_client/templates/property_templates/list_property.pyi

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{% macro construct(property, source) %}
1+
{% macro construct(property, source, initial_value="[]") %}
22
{% set inner_property = property.inner_property %}
33
{% if inner_property.template %}
44
{% set inner_source = inner_property.python_name + "_data" %}
5-
{{ property.python_name }} = []
5+
{{ property.python_name }} = {{ initial_value }}
66
{% if property.required %}
77
for {{ inner_source }} in ({{ source }}):
88
{% else %}
@@ -31,7 +31,7 @@ for {{ inner_source }} in {{ source }}:
3131
{% endmacro %}
3232

3333

34-
{% macro transform(property, source, destination) %}
34+
{% macro transform(property, source, destination, declare_type=True) %}
3535
{% set inner_property = property.inner_property %}
3636
{% if property.required %}
3737
{% if property.nullable %}
@@ -43,7 +43,7 @@ else:
4343
{{ _transform(property, source, destination) }}
4444
{% endif %}
4545
{% else %}
46-
{{ destination }}: Union[Unset, List[Any]] = UNSET
46+
{{ destination }}{% if declare_type %}: Union[Unset, List[Any]]{% endif %} = UNSET
4747
if not isinstance({{ source }}, Unset):
4848
{% if property.nullable %}
4949
if {{ source }} is None:

openapi_python_client/templates/property_templates/model_property.pyi

+4-4
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 }} = {{ property.reference.class_name }}.from_dict({{ source }})
44
{% else %}
5-
{{ property.python_name }} = None
5+
{{ property.python_name }} = {{ initial_value }}
66
if {{ source }} is not None:
77
{{ property.python_name }} = {{ property.reference.class_name }}.from_dict(cast(Dict[str, Any], {{ source }}))
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 }}.to_dict() if {{ source }} else None
1515
{% else %}
1616
{{ destination }} = {{ source }}.to_dict()
1717
{% endif %}
1818
{% else %}
19-
{{ destination }}: {{ property.get_type_string() }} = UNSET
19+
{{ destination }}{% if declare_type %}: {{ property.get_type_string() }}{% endif %} = UNSET
2020
if not isinstance({{ source }}, Unset):
2121
{% if property.nullable %}
2222
{{ destination }} = {{ source }}.to_dict() if {{ source }} else None

0 commit comments

Comments
 (0)