-
Notifications
You must be signed in to change notification settings - Fork 1
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
add discriminator property support #214
Changes from all commits
0496ff8
b75adda
2941451
d189f77
de1ddf3
9071de0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
from typing import Any, Dict, List, Type, TypeVar, Union | ||
from typing import Any, Dict, List, Type, TypeVar | ||
|
||
from attrs import define as _attrs_define | ||
from attrs import field as _attrs_field | ||
|
||
from ..types import UNSET, Unset | ||
|
||
T = TypeVar("T", bound="ADiscriminatedUnionType1") | ||
|
||
|
||
@_attrs_define | ||
class ADiscriminatedUnionType1: | ||
""" | ||
Attributes: | ||
model_type (Union[Unset, str]): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes in this generated file and the next one are because I changed |
||
model_type (str): | ||
""" | ||
|
||
model_type: Union[Unset, str] = UNSET | ||
model_type: str | ||
additional_properties: Dict[str, Any] = _attrs_field(init=False, factory=dict) | ||
|
||
def to_dict(self) -> Dict[str, Any]: | ||
model_type = self.model_type | ||
|
||
field_dict: Dict[str, Any] = {} | ||
field_dict.update(self.additional_properties) | ||
field_dict.update({}) | ||
if model_type is not UNSET: | ||
field_dict["modelType"] = model_type | ||
field_dict.update( | ||
{ | ||
"modelType": model_type, | ||
} | ||
) | ||
|
||
return field_dict | ||
|
||
@classmethod | ||
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T: | ||
d = src_dict.copy() | ||
model_type = d.pop("modelType", UNSET) | ||
model_type = d.pop("modelType") | ||
|
||
a_discriminated_union_type_1 = cls( | ||
model_type=model_type, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,23 +59,42 @@ def _parse_discriminated_union( | |
return data | ||
if isinstance(data, Unset): | ||
return data | ||
try: | ||
if not isinstance(data, dict): | ||
raise TypeError() | ||
componentsschemas_a_discriminated_union_type_0 = ADiscriminatedUnionType1.from_dict(data) | ||
|
||
return componentsschemas_a_discriminated_union_type_0 | ||
except: # noqa: E722 | ||
pass | ||
try: | ||
if not isinstance(data, dict): | ||
raise TypeError() | ||
componentsschemas_a_discriminated_union_type_1 = ADiscriminatedUnionType2.from_dict(data) | ||
|
||
return componentsschemas_a_discriminated_union_type_1 | ||
except: # noqa: E722 | ||
pass | ||
return cast(Union["ADiscriminatedUnionType1", "ADiscriminatedUnionType2", None, Unset], data) | ||
if not isinstance(data, dict): | ||
raise TypeError() | ||
if "modelType" in data: | ||
_discriminator_value = data["modelType"] | ||
|
||
def _parse_1(data: object) -> ADiscriminatedUnionType1: | ||
if not isinstance(data, dict): | ||
raise TypeError() | ||
componentsschemas_a_discriminated_union_type_0 = ADiscriminatedUnionType1.from_dict(data) | ||
|
||
return componentsschemas_a_discriminated_union_type_0 | ||
|
||
def _parse_2(data: object) -> ADiscriminatedUnionType2: | ||
if not isinstance(data, dict): | ||
raise TypeError() | ||
componentsschemas_a_discriminated_union_type_1 = ADiscriminatedUnionType2.from_dict(data) | ||
|
||
return componentsschemas_a_discriminated_union_type_1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 68-72 here are boilerplate that comes from the generator's template macro for "call the deserializer for some type". The weird function name on line 67 is due to how the generator makes unique Python names for stuff in its internal data structure. |
||
|
||
def _parse_3(data: object) -> ADiscriminatedUnionType2: | ||
if not isinstance(data, dict): | ||
raise TypeError() | ||
componentsschemas_a_discriminated_union_type_1 = ADiscriminatedUnionType2.from_dict(data) | ||
|
||
return componentsschemas_a_discriminated_union_type_1 | ||
|
||
_discriminator_mapping = { | ||
"type1": _parse_1, | ||
"type2": _parse_2, | ||
"type2-another-value": _parse_3, | ||
} | ||
if _parse_fn := _discriminator_mapping.get(_discriminator_value): | ||
return cast( | ||
Union["ADiscriminatedUnionType1", "ADiscriminatedUnionType2", None, Unset], _parse_fn(data) | ||
) | ||
raise TypeError("unrecognized value for property modelType") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 81-89 are from my new custom template logic for discriminators. |
||
|
||
discriminated_union = _parse_discriminated_union(d.pop("discriminated_union", UNSET)) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is necessary in order for the discriminator in the spec to really be valid: a discriminator property must be a required property in all of the variant schemas. My current implementation wouldn't actually catch a mistake like this, but I figured it was best to have the test spec be valid.