Skip to content
This repository has been archived by the owner on Aug 19, 2023. It is now read-only.

Verify that literal values are valid when decoding #163

Open
roshcagra opened this issue Jun 7, 2021 · 2 comments
Open

Verify that literal values are valid when decoding #163

roshcagra opened this issue Jun 7, 2021 · 2 comments

Comments

@roshcagra
Copy link

I have some fields in my dataclasses that are large unions, so I've tried setting single-value literals on them to help the decoder discriminate between them, but it looks like while literals are supported, the values aren't validated during decoding.

For example, if you have two dataclasses:

@dataclass
class Foo(JsonSchemaMixin):
     common_field: int
     name: Literal['Foo'] = 'Foo'

@dataclass
class Bar(JsonSchemaMixin):
     common_field: int
     name: Literal['Bar'] = 'Bar'
     other_field: Optional[int] = None

and a dataclass that uses them as a union type,

@dataclass
class Baz(JsonSchemaMixin):
     my_foo_bar: Union[Bar, Foo]

Then

Baz.from_dict(
   Baz(my_foo_bar=
      Foo(common_field=1)
   ).to_dict()
)

will result in

Baz(my_foor_bar=Bar(common_field=1))

even though the literals should help disciminate.

@roshcagra
Copy link
Author

I've got a fix for this! Will submit the PR ASAP

@s-knibbs
Copy link
Owner

This is pretty closely related with #170

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants