Skip to content

fix: Fix deserialization of unions BNCH-20706 #41

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 12 commits into from
Jan 28, 2021
185 changes: 178 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
Expand Up @@ -10,6 +10,8 @@
from ..models.a_model_nullable_model import AModelNullableModel
from ..models.an_enum import AnEnum
from ..models.different_enum import DifferentEnum
from ..models.free_form_model import FreeFormModel
from ..models.model_with_union_property import ModelWithUnionProperty
from ..types import UNSET, Unset


Expand All @@ -22,15 +24,19 @@ class AModel:
a_date: datetime.date
required_not_nullable: str
model: AModelModel
one_of_models: Union[FreeFormModel, ModelWithUnionProperty]
a_nullable_date: Optional[datetime.date]
required_nullable: Optional[str]
nullable_model: Optional[AModelNullableModel]
nullable_one_of_models: Union[None, FreeFormModel, ModelWithUnionProperty]
nested_list_of_enums: Union[Unset, List[List[DifferentEnum]]] = UNSET
attr_1_leading_digit: Union[Unset, str] = UNSET
not_required_nullable: Union[Unset, None, str] = UNSET
not_required_not_nullable: Union[Unset, str] = UNSET
not_required_model: Union[Unset, AModelNotRequiredModel] = UNSET
not_required_nullable_model: Union[Unset, None, AModelNotRequiredNullableModel] = UNSET
not_required_one_of_models: Union[Unset, FreeFormModel, ModelWithUnionProperty] = UNSET
not_required_nullable_one_of_models: Union[Unset, None, FreeFormModel, ModelWithUnionProperty, str] = UNSET

def to_dict(self) -> Dict[str, Any]:
an_enum_value = self.an_enum_value.value
Expand All @@ -45,6 +51,12 @@ def to_dict(self) -> Dict[str, Any]:
required_not_nullable = self.required_not_nullable
model = self.model.to_dict()

if isinstance(self.one_of_models, FreeFormModel):
one_of_models = self.one_of_models.to_dict()

else:
one_of_models = self.one_of_models.to_dict()

nested_list_of_enums: Union[Unset, List[Any]] = UNSET
if not isinstance(self.nested_list_of_enums, Unset):
nested_list_of_enums = []
Expand Down Expand Up @@ -74,6 +86,48 @@ def to_dict(self) -> Dict[str, Any]:
self.not_required_nullable_model.to_dict() if self.not_required_nullable_model else None
)

nullable_one_of_models: Union[None, Dict[str, Any]]
if isinstance(self.nullable_one_of_models, Unset):
nullable_one_of_models = UNSET
if self.nullable_one_of_models is None:
nullable_one_of_models = None
elif isinstance(self.nullable_one_of_models, FreeFormModel):
nullable_one_of_models = self.nullable_one_of_models.to_dict()

else:
nullable_one_of_models = self.nullable_one_of_models.to_dict()

not_required_one_of_models: Union[Unset, Dict[str, Any]]
if isinstance(self.not_required_one_of_models, Unset):
not_required_one_of_models = UNSET
elif isinstance(self.not_required_one_of_models, FreeFormModel):
not_required_one_of_models = UNSET
if not isinstance(self.not_required_one_of_models, Unset):
not_required_one_of_models = self.not_required_one_of_models.to_dict()

else:
not_required_one_of_models = UNSET
if not isinstance(self.not_required_one_of_models, Unset):
not_required_one_of_models = self.not_required_one_of_models.to_dict()

not_required_nullable_one_of_models: Union[Unset, None, Dict[str, Any], str]
if isinstance(self.not_required_nullable_one_of_models, Unset):
not_required_nullable_one_of_models = UNSET
elif self.not_required_nullable_one_of_models is None:
not_required_nullable_one_of_models = None
elif isinstance(self.not_required_nullable_one_of_models, FreeFormModel):
not_required_nullable_one_of_models = UNSET
if not isinstance(self.not_required_nullable_one_of_models, Unset):
not_required_nullable_one_of_models = self.not_required_nullable_one_of_models.to_dict()

elif isinstance(self.not_required_nullable_one_of_models, ModelWithUnionProperty):
not_required_nullable_one_of_models = UNSET
if not isinstance(self.not_required_nullable_one_of_models, Unset):
not_required_nullable_one_of_models = self.not_required_nullable_one_of_models.to_dict()

else:
not_required_nullable_one_of_models = self.not_required_nullable_one_of_models

field_dict: Dict[str, Any] = {}
field_dict.update(
{
Expand All @@ -82,9 +136,11 @@ def to_dict(self) -> Dict[str, Any]:
"a_date": a_date,
"required_not_nullable": required_not_nullable,
"model": model,
"one_of_models": one_of_models,
"a_nullable_date": a_nullable_date,
"required_nullable": required_nullable,
"nullable_model": nullable_model,
"nullable_one_of_models": nullable_one_of_models,
}
)
if nested_list_of_enums is not UNSET:
Expand All @@ -99,6 +155,10 @@ def to_dict(self) -> Dict[str, Any]:
field_dict["not_required_model"] = not_required_model
if not_required_nullable_model is not UNSET:
field_dict["not_required_nullable_model"] = not_required_nullable_model
if not_required_one_of_models is not UNSET:
field_dict["not_required_one_of_models"] = not_required_one_of_models
if not_required_nullable_one_of_models is not UNSET:
field_dict["not_required_nullable_one_of_models"] = not_required_nullable_one_of_models

return field_dict

Expand All @@ -107,15 +167,18 @@ def from_dict(src_dict: Dict[str, Any]) -> "AModel":
d = src_dict.copy()
an_enum_value = AnEnum(d.pop("an_enum_value"))

def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.date]:
data = None if isinstance(data, Unset) else data
def _parse_a_camel_date_time(data: Union[str]) -> Union[datetime.datetime, datetime.date]:
a_camel_date_time: Union[datetime.datetime, datetime.date]
try:
if not isinstance(data, str):
raise TypeError()
a_camel_date_time = isoparse(data)

return a_camel_date_time
except: # noqa: E722
pass
if not isinstance(data, str):
raise TypeError()
a_camel_date_time = isoparse(data).date()

return a_camel_date_time
Expand All @@ -128,6 +191,24 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat

model = AModelModel.from_dict(d.pop("model"))

def _parse_one_of_models(data: Union[Dict[str, Any]]) -> Union[FreeFormModel, ModelWithUnionProperty]:
one_of_models: Union[FreeFormModel, ModelWithUnionProperty]
try:
if not isinstance(data, dict):
raise TypeError()
one_of_models = FreeFormModel.from_dict(data)

return one_of_models
except: # noqa: E722
pass
if not isinstance(data, dict):
raise TypeError()
one_of_models = ModelWithUnionProperty.from_dict(data)

return one_of_models

one_of_models = _parse_one_of_models(d.pop("one_of_models"))

nested_list_of_enums = []
_nested_list_of_enums = d.pop("nested_list_of_enums", UNSET)
for nested_list_of_enums_item_data in _nested_list_of_enums or []:
Expand Down Expand Up @@ -156,26 +237,113 @@ 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[Unset, AModelNotRequiredModel] = 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)

def _parse_nullable_one_of_models(
data: Union[None, Dict[str, Any]]
) -> Union[None, FreeFormModel, ModelWithUnionProperty]:
nullable_one_of_models: Union[None, FreeFormModel, ModelWithUnionProperty]
if data is None:
return data
try:
if not isinstance(data, dict):
raise TypeError()
nullable_one_of_models = FreeFormModel.from_dict(data)

return nullable_one_of_models
except: # noqa: E722
pass
if not isinstance(data, dict):
raise TypeError()
nullable_one_of_models = ModelWithUnionProperty.from_dict(data)

return nullable_one_of_models

nullable_one_of_models = _parse_nullable_one_of_models(d.pop("nullable_one_of_models"))

def _parse_not_required_one_of_models(
data: Union[Unset, Dict[str, Any]]
) -> Union[Unset, FreeFormModel, ModelWithUnionProperty]:
not_required_one_of_models: Union[Unset, FreeFormModel, ModelWithUnionProperty]
if isinstance(data, Unset):
return data
try:
if not isinstance(data, dict):
raise TypeError()
not_required_one_of_models = UNSET
_not_required_one_of_models = data
if not isinstance(_not_required_one_of_models, Unset):
not_required_one_of_models = FreeFormModel.from_dict(_not_required_one_of_models)

return not_required_one_of_models
except: # noqa: E722
pass
if not isinstance(data, dict):
raise TypeError()
not_required_one_of_models = UNSET
_not_required_one_of_models = data
if not isinstance(_not_required_one_of_models, Unset):
not_required_one_of_models = ModelWithUnionProperty.from_dict(_not_required_one_of_models)

return not_required_one_of_models

not_required_one_of_models = _parse_not_required_one_of_models(d.pop("not_required_one_of_models", UNSET))

def _parse_not_required_nullable_one_of_models(
data: Union[Unset, None, Dict[str, Any], str]
) -> Union[Unset, None, FreeFormModel, ModelWithUnionProperty, str]:
not_required_nullable_one_of_models: Union[Unset, None, FreeFormModel, ModelWithUnionProperty, str]
if data is None:
return data
if isinstance(data, Unset):
return data
try:
if not isinstance(data, dict):
raise TypeError()
not_required_nullable_one_of_models = UNSET
_not_required_nullable_one_of_models = data
if not isinstance(_not_required_nullable_one_of_models, Unset):
not_required_nullable_one_of_models = FreeFormModel.from_dict(_not_required_nullable_one_of_models)

return not_required_nullable_one_of_models
except: # noqa: E722
pass
try:
if not isinstance(data, dict):
raise TypeError()
not_required_nullable_one_of_models = UNSET
_not_required_nullable_one_of_models = data
if not isinstance(_not_required_nullable_one_of_models, Unset):
not_required_nullable_one_of_models = ModelWithUnionProperty.from_dict(
_not_required_nullable_one_of_models
)

return not_required_nullable_one_of_models
except: # noqa: E722
pass
return cast(Union[Unset, None, FreeFormModel, ModelWithUnionProperty, str], data)

not_required_nullable_one_of_models = _parse_not_required_nullable_one_of_models(
d.pop("not_required_nullable_one_of_models", UNSET)
)

a_model = AModel(
an_enum_value=an_enum_value,
a_camel_date_time=a_camel_date_time,
a_date=a_date,
required_not_nullable=required_not_nullable,
model=model,
one_of_models=one_of_models,
nested_list_of_enums=nested_list_of_enums,
a_nullable_date=a_nullable_date,
attr_1_leading_digit=attr_1_leading_digit,
Expand All @@ -185,6 +353,9 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat
nullable_model=nullable_model,
not_required_model=not_required_model,
not_required_nullable_model=not_required_nullable_model,
nullable_one_of_models=nullable_one_of_models,
not_required_one_of_models=not_required_one_of_models,
not_required_nullable_one_of_models=not_required_nullable_one_of_models,
)

return a_model
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ def to_dict(self) -> Dict[str, Any]:
def from_dict(src_dict: Dict[str, Any]) -> "AModelModel":
d = src_dict.copy()

def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
data = None if isinstance(data, Unset) else data
def _parse_a_property(data: Union[Unset, int]) -> Union[Unset, AnEnum, AnIntEnum]:
a_property: Union[Unset, AnEnum, AnIntEnum]
if isinstance(data, Unset):
return data
try:
if not (isinstance(data, int) or isinstance(data, str)):
raise TypeError()
a_property = UNSET
_a_property = data
if _a_property is not None:
Expand All @@ -52,6 +55,8 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
return a_property
except: # noqa: E722
pass
if not (isinstance(data, int) or isinstance(data, str)):
raise TypeError()
a_property = UNSET
_a_property = data
if _a_property is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ def to_dict(self) -> Dict[str, Any]:
def from_dict(src_dict: Dict[str, Any]) -> "AModelNotRequiredModel":
d = src_dict.copy()

def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
data = None if isinstance(data, Unset) else data
def _parse_a_property(data: Union[Unset, int]) -> Union[Unset, AnEnum, AnIntEnum]:
a_property: Union[Unset, AnEnum, AnIntEnum]
if isinstance(data, Unset):
return data
try:
if not (isinstance(data, int) or isinstance(data, str)):
raise TypeError()
a_property = UNSET
_a_property = data
if _a_property is not None:
Expand All @@ -52,6 +55,8 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
return a_property
except: # noqa: E722
pass
if not (isinstance(data, int) or isinstance(data, str)):
raise TypeError()
a_property = UNSET
_a_property = data
if _a_property is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ def to_dict(self) -> Dict[str, Any]:
def from_dict(src_dict: Dict[str, Any]) -> "AModelNotRequiredNullableModel":
d = src_dict.copy()

def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
data = None if isinstance(data, Unset) else data
def _parse_a_property(data: Union[Unset, int]) -> Union[Unset, AnEnum, AnIntEnum]:
a_property: Union[Unset, AnEnum, AnIntEnum]
if isinstance(data, Unset):
return data
try:
if not (isinstance(data, int) or isinstance(data, str)):
raise TypeError()
a_property = UNSET
_a_property = data
if _a_property is not None:
Expand All @@ -52,6 +55,8 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
return a_property
except: # noqa: E722
pass
if not (isinstance(data, int) or isinstance(data, str)):
raise TypeError()
a_property = UNSET
_a_property = data
if _a_property is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ def to_dict(self) -> Dict[str, Any]:
def from_dict(src_dict: Dict[str, Any]) -> "AModelNullableModel":
d = src_dict.copy()

def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
data = None if isinstance(data, Unset) else data
def _parse_a_property(data: Union[Unset, int]) -> Union[Unset, AnEnum, AnIntEnum]:
a_property: Union[Unset, AnEnum, AnIntEnum]
if isinstance(data, Unset):
return data
try:
if not (isinstance(data, int) or isinstance(data, str)):
raise TypeError()
a_property = UNSET
_a_property = data
if _a_property is not None:
Expand All @@ -52,6 +55,8 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
return a_property
except: # noqa: E722
pass
if not (isinstance(data, int) or isinstance(data, str)):
raise TypeError()
a_property = UNSET
_a_property = data
if _a_property is not None:
Expand Down
Loading