Skip to content

Fix deserialization of unions #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor
  • Loading branch information
forest-benchling committed Jan 28, 2021
commit 8e88fa49e113ac5e726d9698237ac873704ca27a
39 changes: 22 additions & 17 deletions openapi_python_client/parser/properties/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,38 +169,43 @@ def __attrs_post_init__(self) -> None:
self, "has_properties_without_templates", any(prop.template is None for prop in self.inner_properties)
)

def _get_inner_prop_string(self, json: bool = False) -> str:
def _get_inner_type_strings(self, json: bool = False) -> List[str]:
inner_types = [p.get_type_string(no_optional=True, json=json) for p in self.inner_properties]
unique_inner_types = list(dict.fromkeys(inner_types))
return ", ".join(unique_inner_types)
return unique_inner_types

def get_base_type_string(self, json: bool = False) -> str:
return f"Union[{self._get_inner_prop_string(json=json)}]"
return f"Union[{', '.join(self._get_inner_type_strings(json=json))}]"

def get_type_string(self, no_optional: bool = False, query_parameter: bool = False, json: bool = False) -> str:
"""
Get a string representation of type that should be used when declaring this property.

This implementation differs slightly from `Property.get_type_string` in order to collapse
nested union types.
"""
type_string = self.get_base_type_string(json=json)
def get_type_strings_in_union(self, no_optional: bool = False, query_parameter: bool = False, json: bool = False) -> List[str]:
type_strings = self._get_inner_type_strings(json=json)
if no_optional:
return type_string
return type_strings
if self.required:
if self.nullable:
return f"Union[None, {self._get_inner_prop_string(json=json)}]"
return ["None"] + type_strings
else:
return type_string
return type_strings
else:
if self.nullable:
return f"Union[Unset, None, {self._get_inner_prop_string(json=json)}]"
return ["Unset", "None"] + type_strings
else:
if query_parameter:
# For query parameters, None has the same meaning as Unset
return f"Union[Unset, None, {self._get_inner_prop_string(json=json)}]"
return ["Unset", "None"] + type_strings
else:
return f"Union[Unset, {self._get_inner_prop_string(json=json)}]"
return ["Unset"] + type_strings

def get_type_string(self, no_optional: bool = False, query_parameter: bool = False, json: bool = False) -> str:
"""
Get a string representation of type that should be used when declaring this property.

This implementation differs slightly from `Property.get_type_string` in order to collapse
nested union types.
"""
return (
f"Union[{', '.join(self.get_type_strings_in_union(no_optional=no_optional, query_parameter=query_parameter, json=json))}]"
)

def get_imports(self, *, prefix: str) -> Set[str]:
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{% macro construct(property, source, initial_value=None) %}
def _parse_{{ property.python_name }}(data: {{ property.get_type_string(json=True) }}) -> {{ property.get_type_string() }}:
{{ property.python_name }}: {{ property.get_type_string() }}
{% if "None" in property.get_type_string(json=True) %}
{% if "None" in property.get_type_strings_in_union(json=True) %}
if data is None:
return data
{% endif %}
{% if "Unset" in property.get_type_string(json=True) %}
{% if "Unset" in property.get_type_strings_in_union(json=True) %}
if isinstance(data, Unset):
return data
{% endif %}
Expand Down