Skip to content

fix: Fix deserialization of non-required properties #334

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

Merged
merged 4 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[
if response.status_code == 422:
response_422 = HTTPValidationError.from_dict(response.json())



Comment on lines +27 to +28
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Odd, I'd think that black would have removed that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I couldn't figure out why that got added by my PR...

return response_422
return None

Expand Down
25 changes: 18 additions & 7 deletions end_to_end_tests/golden-record-custom/custom_e2e/models/a_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
from typing import Any, Dict, List, Optional, Type, TypeVar, Union, cast
from typing import Any, Dict, List, Optional, Type, TypeVar, Union

import attr
from dateutil.parser import isoparse
Expand Down Expand Up @@ -28,6 +28,7 @@ class AModel:
required_nullable: Optional[str]
nullable_model: Optional[AModelNullableModel]
nested_list_of_enums: Union[Unset, List[List[DifferentEnum]]] = UNSET
a_not_required_date: Union[Unset, datetime.date] = UNSET
attr_1_leading_digit: Union[Unset, str] = UNSET
not_required_nullable: Union[Unset, Optional[str]] = UNSET
not_required_not_nullable: Union[Unset, str] = UNSET
Expand Down Expand Up @@ -60,6 +61,10 @@ def to_dict(self) -> Dict[str, Any]:
nested_list_of_enums.append(nested_list_of_enums_item)

a_nullable_date = self.a_nullable_date.isoformat() if self.a_nullable_date else None
a_not_required_date: Union[Unset, str] = UNSET
if not isinstance(self.a_not_required_date, Unset):
a_not_required_date = self.a_not_required_date.isoformat()

attr_1_leading_digit = self.attr_1_leading_digit
required_nullable = self.required_nullable
not_required_nullable = self.not_required_nullable
Expand Down Expand Up @@ -91,6 +96,8 @@ def to_dict(self) -> Dict[str, Any]:
)
if nested_list_of_enums is not UNSET:
field_dict["nested_list_of_enums"] = nested_list_of_enums
if a_not_required_date is not UNSET:
field_dict["a_not_required_date"] = a_not_required_date
if attr_1_leading_digit is not UNSET:
field_dict["1_leading_digit"] = attr_1_leading_digit
if not_required_nullable is not UNSET:
Expand Down Expand Up @@ -145,7 +152,12 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat
a_nullable_date = None
_a_nullable_date = d.pop("a_nullable_date")
if _a_nullable_date is not None:
a_nullable_date = isoparse(cast(str, _a_nullable_date)).date()
a_nullable_date = isoparse(_a_nullable_date).date()

a_not_required_date: Union[Unset, datetime.date] = UNSET
_a_not_required_date = d.pop("a_not_required_date", UNSET)
if not isinstance(_a_not_required_date, Unset):
a_not_required_date = isoparse(_a_not_required_date).date()

attr_1_leading_digit = d.pop("1_leading_digit", UNSET)

Expand All @@ -158,19 +170,17 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat
nullable_model = None
_nullable_model = d.pop("nullable_model")
if _nullable_model is not None:
nullable_model = AModelNullableModel.from_dict(cast(Dict[str, Any], _nullable_model))
nullable_model = AModelNullableModel.from_dict(_nullable_model)

not_required_model: Union[AModelNotRequiredModel, Unset] = UNSET
_not_required_model = d.pop("not_required_model", UNSET)
if not isinstance(_not_required_model, Unset):
not_required_model = AModelNotRequiredModel.from_dict(cast(Dict[str, Any], _not_required_model))
not_required_model = AModelNotRequiredModel.from_dict(_not_required_model)

not_required_nullable_model = None
_not_required_nullable_model = d.pop("not_required_nullable_model", UNSET)
if _not_required_nullable_model is not None and not isinstance(_not_required_nullable_model, Unset):
not_required_nullable_model = AModelNotRequiredNullableModel.from_dict(
cast(Dict[str, Any], _not_required_nullable_model)
)
not_required_nullable_model = AModelNotRequiredNullableModel.from_dict(_not_required_nullable_model)

a_model = cls(
an_enum_value=an_enum_value,
Expand All @@ -180,6 +190,7 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat
model=model,
nested_list_of_enums=nested_list_of_enums,
a_nullable_date=a_nullable_date,
a_not_required_date=a_not_required_date,
attr_1_leading_digit=attr_1_leading_digit,
required_nullable=required_nullable,
not_required_nullable=not_required_nullable,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, Type, TypeVar, Union, cast
from typing import Any, Dict, List, Type, TypeVar, Union

import attr

Expand Down Expand Up @@ -36,9 +36,7 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
a_date_holder: Union[ModelWithPrimitiveAdditionalPropertiesADateHolder, Unset] = UNSET
_a_date_holder = d.pop("a_date_holder", UNSET)
if not isinstance(_a_date_holder, Unset):
a_date_holder = ModelWithPrimitiveAdditionalPropertiesADateHolder.from_dict(
cast(Dict[str, Any], _a_date_holder)
)
a_date_holder = ModelWithPrimitiveAdditionalPropertiesADateHolder.from_dict(_a_date_holder)

model_with_primitive_additional_properties = cls(
a_date_holder=a_date_holder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
try:
a_property = UNSET
_a_property = data
if _a_property is not None and _a_property is not UNSET:
if not isinstance(_a_property, Unset):
a_property = AnEnum(_a_property)

return a_property
except: # noqa: E722
pass
a_property = UNSET
_a_property = data
if _a_property is not None and _a_property is not UNSET:
if not isinstance(_a_property, Unset):
a_property = AnIntEnum(_a_property)

return a_property
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
from typing import Any, Dict, List, Optional, Type, TypeVar, Union, cast
from typing import Any, Dict, List, Optional, Type, TypeVar, Union

import attr
from dateutil.parser import isoparse
Expand Down Expand Up @@ -28,6 +28,7 @@ class AModel:
required_nullable: Optional[str]
nullable_model: Optional[AModelNullableModel]
nested_list_of_enums: Union[Unset, List[List[DifferentEnum]]] = UNSET
a_not_required_date: Union[Unset, datetime.date] = UNSET
attr_1_leading_digit: Union[Unset, str] = UNSET
not_required_nullable: Union[Unset, Optional[str]] = UNSET
not_required_not_nullable: Union[Unset, str] = UNSET
Expand Down Expand Up @@ -60,6 +61,10 @@ def to_dict(self) -> Dict[str, Any]:
nested_list_of_enums.append(nested_list_of_enums_item)

a_nullable_date = self.a_nullable_date.isoformat() if self.a_nullable_date else None
a_not_required_date: Union[Unset, str] = UNSET
if not isinstance(self.a_not_required_date, Unset):
a_not_required_date = self.a_not_required_date.isoformat()

attr_1_leading_digit = self.attr_1_leading_digit
required_nullable = self.required_nullable
not_required_nullable = self.not_required_nullable
Expand Down Expand Up @@ -91,6 +96,8 @@ def to_dict(self) -> Dict[str, Any]:
)
if nested_list_of_enums is not UNSET:
field_dict["nested_list_of_enums"] = nested_list_of_enums
if a_not_required_date is not UNSET:
field_dict["a_not_required_date"] = a_not_required_date
if attr_1_leading_digit is not UNSET:
field_dict["1_leading_digit"] = attr_1_leading_digit
if not_required_nullable is not UNSET:
Expand Down Expand Up @@ -145,7 +152,12 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat
a_nullable_date = None
_a_nullable_date = d.pop("a_nullable_date")
if _a_nullable_date is not None:
a_nullable_date = isoparse(cast(str, _a_nullable_date)).date()
a_nullable_date = isoparse(_a_nullable_date).date()

a_not_required_date: Union[Unset, datetime.date] = UNSET
_a_not_required_date = d.pop("a_not_required_date", UNSET)
if not isinstance(_a_not_required_date, Unset):
a_not_required_date = isoparse(_a_not_required_date).date()

attr_1_leading_digit = d.pop("1_leading_digit", UNSET)

Expand All @@ -158,19 +170,17 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat
nullable_model = None
_nullable_model = d.pop("nullable_model")
if _nullable_model is not None:
nullable_model = AModelNullableModel.from_dict(cast(Dict[str, Any], _nullable_model))
nullable_model = AModelNullableModel.from_dict(_nullable_model)

not_required_model: Union[AModelNotRequiredModel, Unset] = UNSET
_not_required_model = d.pop("not_required_model", UNSET)
if not isinstance(_not_required_model, Unset):
not_required_model = AModelNotRequiredModel.from_dict(cast(Dict[str, Any], _not_required_model))
not_required_model = AModelNotRequiredModel.from_dict(_not_required_model)

not_required_nullable_model = None
_not_required_nullable_model = d.pop("not_required_nullable_model", UNSET)
if _not_required_nullable_model is not None and not isinstance(_not_required_nullable_model, Unset):
not_required_nullable_model = AModelNotRequiredNullableModel.from_dict(
cast(Dict[str, Any], _not_required_nullable_model)
)
not_required_nullable_model = AModelNotRequiredNullableModel.from_dict(_not_required_nullable_model)

a_model = cls(
an_enum_value=an_enum_value,
Expand All @@ -180,6 +190,7 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat
model=model,
nested_list_of_enums=nested_list_of_enums,
a_nullable_date=a_nullable_date,
a_not_required_date=a_not_required_date,
attr_1_leading_digit=attr_1_leading_digit,
required_nullable=required_nullable,
not_required_nullable=not_required_nullable,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, Type, TypeVar, Union, cast
from typing import Any, Dict, List, Type, TypeVar, Union

import attr

Expand Down Expand Up @@ -36,9 +36,7 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
a_date_holder: Union[ModelWithPrimitiveAdditionalPropertiesADateHolder, Unset] = UNSET
_a_date_holder = d.pop("a_date_holder", UNSET)
if not isinstance(_a_date_holder, Unset):
a_date_holder = ModelWithPrimitiveAdditionalPropertiesADateHolder.from_dict(
cast(Dict[str, Any], _a_date_holder)
)
a_date_holder = ModelWithPrimitiveAdditionalPropertiesADateHolder.from_dict(_a_date_holder)

model_with_primitive_additional_properties = cls(
a_date_holder=a_date_holder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
try:
a_property = UNSET
_a_property = data
if _a_property is not None and _a_property is not UNSET:
if not isinstance(_a_property, Unset):
a_property = AnEnum(_a_property)

return a_property
except: # noqa: E722
pass
a_property = UNSET
_a_property = data
if _a_property is not None and _a_property is not UNSET:
if not isinstance(_a_property, Unset):
a_property = AnIntEnum(_a_property)

return a_property
Expand Down
5 changes: 5 additions & 0 deletions end_to_end_tests/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,11 @@
"format": "date",
"nullable": true
},
"a_not_required_date": {
"title": "A Nullable Date",
"type": "string",
"format": "date",
},
"1_leading_digit": {
"title": "Leading Digit",
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{% macro construct(property, source, initial_value="None") %}
{% if property.required and not property.nullable %}
{{ property.python_name }} = isoparse({{ source }}).date()
{% else %}
{{ property.python_name }} = {{ initial_value }}
_{{ property.python_name }} = {{ source }}
if _{{ property.python_name }} is not None:
{{ property.python_name }} = isoparse(cast(str, _{{ property.python_name }})).date()
{% endif %}
{% macro construct_function(property, source) %}
isoparse({{ source }}).date()
{% endmacro %}

{% from "property_templates/property_macros.py.jinja" import construct_template %}

{% macro construct(property, source, initial_value=None) %}
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
{% endmacro %}

{% macro transform(property, source, destination, declare_type=True) %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
{% macro construct(property, source, initial_value="None") %}
{% if property.required %}
{% if property.nullable %}
{{ property.python_name }} = {{ source }}
{{ property.python_name }} = isoparse({{ property.python_name }}) if {{ property.python_name }} else None
{% else %}
{{ property.python_name }} = isoparse({{ source }})
{% endif %}
{% else %}
{{ property.python_name }} = {{ initial_value }}
_{{ property.python_name }} = {{ source }}
if _{{ property.python_name }} is not None:
{{ property.python_name }} = isoparse(cast(str, _{{ property.python_name }}))
{% endif %}
{% macro construct_function(property, source) %}
isoparse({{ source }})
{% endmacro %}

{% from "property_templates/property_macros.py.jinja" import construct_template %}

{% macro construct(property, source, initial_value=None) %}
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
{% endmacro %}

{% macro transform(property, source, destination, declare_type=True) %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{% macro construct(property, source, initial_value="None") %}
{% if property.required %}
{{ property.python_name }} = {{ property.reference.class_name }}({{ source }})
{% else %}
{{ property.python_name }} = {{ initial_value }}
_{{ property.python_name }} = {{ source }}
if _{{ property.python_name }} is not None and _{{ property.python_name }} is not UNSET:
{{ property.python_name }} = {{ property.reference.class_name }}(_{{ property.python_name }})
{% endif %}
{% macro construct_function(property, source) %}
{{ property.reference.class_name }}({{ source }})
{% endmacro %}

{% from "property_templates/property_macros.py.jinja" import construct_template %}

{% macro construct(property, source, initial_value=None) %}
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
{% endmacro %}

{% macro transform(property, source, destination, declare_type=True) %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
{% macro construct(property, source, initial_value=None) %}
{{ property.python_name }} = File(
{% macro construct_function(property, source) %}
File(
payload = BytesIO({{ source }})
)
{% endmacro %}

{% from "property_templates/property_macros.py.jinja" import construct_template %}

{% macro construct(property, source, initial_value=None) %}
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
{% endmacro %}

{% macro transform(property, source, destination, declare_type=True) %}
{% if property.required %}
{% if property.nullable %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% set inner_source = inner_property.python_name + "_data" %}
{{ property.python_name }} = {{ initial_value }}
_{{ property.python_name }} = {{ source }}
{% if property.required %}
{% if property.required and not property.nullable %}
for {{ inner_source }} in (_{{ property.python_name }}):
{% else %}
for {{ inner_source }} in (_{{ property.python_name }} or []):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
{% macro construct_function(property, source) %}
{{ property.reference.class_name }}.from_dict({{ source }})
{% endmacro %}

{% from "property_templates/property_macros.py.jinja" import construct_template %}

{% macro construct(property, source, initial_value=None) %}
{% if property.required and not property.nullable %}
{{ property.python_name }} = {{ property.reference.class_name }}.from_dict({{ source }})
{% else %}
{% if initial_value != None %}
{{ property.python_name }} = {{ initial_value }}
{% elif property.nullable %}
{{ property.python_name }} = None
{% else %}
{{ property.python_name }}: {{ property.get_type_string() }} = UNSET
{% endif %}
_{{ property.python_name }} = {{source}}
if {% if property.nullable %}_{{ property.python_name }} is not None{% endif %}{% if property.nullable and not property.required %} and {% endif %}{% if not property.required %}not isinstance(_{{ property.python_name }}, Unset){% endif %}:
{{ property.python_name }} = {{ property.reference.class_name }}.from_dict(cast(Dict[str, Any], _{{ property.python_name }}))
{% endif %}
{{ construct_template(construct_function, property, source, initial_value=initial_value) }}
{% endmacro %}

{% macro transform(property, source, destination, declare_type=True) %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% macro construct_template(construct_function, property, source, initial_value=None) %}
{% if property.required and not property.nullable %}
{{ property.python_name }} = {{ construct_function(property, source) }}
{% else %}
{% if initial_value != None %}
{{ property.python_name }} = {{ initial_value }}
{% elif property.nullable %}
{{ property.python_name }} = None
{% else %}
{{ property.python_name }}: {{ property.get_type_string() }} = UNSET
{% endif %}
_{{ property.python_name }} = {{ source }}
if {% if property.nullable %}_{{ property.python_name }} is not None{% endif %}{% if property.nullable and not property.required %} and {% endif %}{% if not property.required %}not isinstance(_{{ property.python_name }}, Unset){% endif %}:
{{ property.python_name }} = {{ construct_function(property, "_" + property.python_name) }}
{% endif %}
{% endmacro %}
Loading