lidatong / dataclasses-json

Easily serialize Data Classes to and from JSON
MIT License
1.36k stars 153 forks source link

improve Union deserialization when "__type" field specifier is not present #478

Closed idbentley closed 1 year ago

idbentley commented 1 year ago

This pull request adds a best-effort deserialization to Union types when no __type field specifier is present.

In the case that no __type field is present, a warning is issued describing the risk of such unmarshalling attempt.

We attempt to decode ensuring that keys exist on the type, and the values are appropriate. If either of these conditions fails, we abandon that dataclass type, and move onto the next one.

If no type is found, a warning is issued, the value is not decoded, and is left as-is.

idbentley commented 1 year ago

@george-zubrienko and @pawelwilczewski Building on #464 I added simple support for iterating over available dataclasses when deserializing objects withou __type specifiers.

idbentley commented 1 year ago

I don't see what you mean @george-zubrienko . The logic change I added in core.py is adding an if check for each dataclass in the type_options, which catches potential decoding errors in order to test each dataclass, until a suitable one (or none is found).

pawelwilczewski commented 1 year ago

@idbentley See what the final PR looked like. As per @george-zubrienko suggestion, I refactored the file a bit before finalising the PR.

idbentley commented 1 year ago

Oh, I see! Sorry, I misunderstood your comment @george-zubrienko ! I've updated it to match recommended style!

idbentley commented 1 year ago

Hi @george-zubrienko . Would love to see this PR get a review as it will enable a feature to be completed on my team.

I updated the style (as you asked) last week, and and rebased from master today.

Thanks for your consideration!

george-zubrienko commented 1 year ago

Hi @george-zubrienko . Would love to see this PR get a review as it will enable a feature to be completed on my team.

I updated the style (as you asked) last week, and and rebased from master today.

Thanks for your consideration!

Hello :) no worries I'll review/merge this week. Github also made a mess recently, which impacts our ability to release to PyPI, thus I cannot guarantee a PyPI release this week. Sep 14 is when GH engineering should provide a solution - or so I was told. Stay tuned for updates.

george-zubrienko commented 1 year ago

Pleasant discussion, elaborate reasoning, all-around great PR. Time to merge!

idbentley commented 1 year ago

Great working with you @george-zubrienko ! Thanks for all the help.