Skip to content

Commit 9a679af

Browse files
committed
Update union properties handling of props with and without template
1 parent 0521e36 commit 9a679af

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

openapi_python_client/parser/properties/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import builtins
12
from itertools import chain
23
from typing import Any, ClassVar, Dict, Generic, Iterable, List, Optional, Set, Tuple, TypeVar, Union
34

@@ -187,6 +188,14 @@ def get_imports(self, *, prefix: str) -> Set[str]:
187188
imports.add("from typing import Union")
188189
return imports
189190

191+
@builtins.property
192+
def inner_properties_with_template(self) -> List[Property]:
193+
return [prop for prop in self.inner_properties if prop.template]
194+
195+
@builtins.property
196+
def inner_properties_without_template(self) -> List[Property]:
197+
return [prop for prop in self.inner_properties if not prop.template]
198+
190199

191200
def _string_based_property(
192201
name: str, required: bool, data: oai.Schema

openapi_python_client/templates/property_templates/union_property.pyi

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22
def _parse_{{ property.python_name }}(data: Any) -> {{ property.get_type_string() }}:
33
data = None if isinstance(data, Unset) else data
44
{{ property.python_name }}: {{ property.get_type_string() }}
5-
{% for inner_property in property.inner_properties %}
6-
{% if inner_property.template and not loop.last %}
5+
{% for inner_property in property.inner_properties_with_template %}
6+
{% if not loop.last or property.inner_properties_without_template %}
77
try:
88
{% from "property_templates/" + inner_property.template import construct %}
99
{{ construct(inner_property, "data", initial_value="UNSET") | indent(8) }}
1010
return {{ property.python_name }}
1111
except: # noqa: E722
1212
pass
13-
{% elif inner_property.template and loop.last %}{# Don't do try/except for the last one #}
13+
{% else %}{# Don't do try/except for the last one #}
1414
{% from "property_templates/" + inner_property.template import construct %}
1515
{{ construct(inner_property, "data", initial_value="UNSET") | indent(4) }}
1616
return {{ property.python_name }}
17-
{% else %}
18-
return cast({{ inner_property.get_type_string() }}, data)
1917
{% endif %}
2018
{% endfor %}
19+
{% if property.inner_properties_without_template %}
20+
{# Doesn't really matter what we cast it to as this type will be erased, so cast to one of the options #}
21+
return cast({{ property.inner_properties_without_template[0].get_type_string() }}, data)
22+
{% endif %}
2123

2224
{{ property.python_name }} = _parse_{{ property.python_name }}({{ source }})
2325
{% endmacro %}
@@ -37,19 +39,22 @@ elif {{ source }} is None:
3739
{% endif %}
3840
{{ destination }}{% if declare_type %}: {{ property.get_type_string() }}{% endif %} = None
3941
{% endif %}
40-
{% for inner_property in property.inner_properties %}
42+
{% for inner_property in property.inner_properties_with_template %}
4143
{% if loop.first and property.required and not property.nullable %}{# No if UNSET or if None statement before this #}
4244
if isinstance({{ source }}, {{ inner_property.get_instance_type_string() }}):
43-
{% elif not loop.last %}
45+
{% elif not loop.last or property.inner_properties_without_template %}
4446
elif isinstance({{ source }}, {{ inner_property.get_instance_type_string() }}):
4547
{% else %}
4648
else:
4749
{% endif %}
48-
{% if inner_property.template %}
4950
{% from "property_templates/" + inner_property.template import transform %}
5051
{{ transform(inner_property, source, destination, declare_type=False) | indent(4) }}
51-
{% else %}
52+
{% endfor %}
53+
{% if property.inner_properties_without_template and (property.inner_properties_with_template or not property.required)%}
54+
else:
5255
{{ destination }} = {{ source }}
56+
{% elif property.inner_properties_without_template %}
57+
{{ destination }} = {{ source }}
5358
{% endif %}
54-
{% endfor %}
59+
5560
{% endmacro %}

0 commit comments

Comments
 (0)