lidatong / dataclasses-json

Easily serialize Data Classes to and from JSON
MIT License
1.34k stars 150 forks source link

[BUG] dataclass with generic dataclass field is not loaded correctly #523

Closed PJCampi closed 2 months ago

PJCampi commented 2 months ago

Description

Consider the following class hierarchy:

@dataclass_json
@dataclass
class NestedClass(Generic[T]):
    value: T

@dataclass_json
@dataclass
class MyClass(Generic[T]):
    nested: NestedClass[T]

MyClass will not be loaded correctly from json because NestedClass[T] is a generic type and is_dataclass returns false for the type.

The issue can be fixed by operating on the origin of the generic type NestedClass[T] instead.

I have tried to submit a PR to fix the issue but I don't seem to be authorized to push code to the repo :(

Code snippet that reproduces the issue

a = MyClass(nested=NestedClass(value="value"))
assert MyClass.from_json(a.to_json()) == a

Describe the results you expected

I expect the assertion not to fail

Python version you are using

3.11

Environment description

attrs==23.1.0 black==22.12.0 certifi==2023.7.22 charset-normalizer==3.2.0 click==8.1.7 coverage==7.2.7 -e git+https://github.com/lidatong/dataclasses-json.git@da1f924d61240bd9aff60ed14622ed0bd2d6fccf#egg=dataclasses_json docstring-parser==0.15 falcon==2.0.0 flake8==5.0.4 ghp-import==2.1.0 gitdb==4.0.10 GitPython==3.1.32 hug==2.6.1 hypothesis==6.79.4 idna==3.4 importlib-metadata==4.2.0 iniconfig==2.0.0 Jinja2==3.1.2 livereload==2.6.3 Mako==1.2.4 Markdown==3.3.4 MarkupSafe==2.1.3 marshmallow==3.19.0 mccabe==0.7.0 mergedeep==1.3.4 mkdocs==1.2.4 mkdocs-material==7.3.0 mkdocs-material-extensions==1.1.1 mypy==1.4.1 mypy-extensions==1.0.0 packaging==23.1 pathspec==0.11.2 pdocs==1.2.0 platformdirs==3.10.0 pluggy==1.2.0 portray==1.7.0 pycodestyle==2.9.1 pyflakes==2.5.0 Pygments==2.16.1 pymdown-extensions==7.1 pytest==7.4.0 pytest-cov==2.12.1 python-dateutil==2.8.2 PyYAML==6.0.1 pyyaml_env_tag==0.1 requests==2.31.0 setuptools==69.5.1 simplejson==3.19.1 six==1.16.0 smmap==5.0.0 sortedcontainers==2.4.0 toml==0.10.2 tornado==6.2 typing-inspect==0.9.0 typing_extensions==4.7.1 urllib3==2.0.4 watchdog==3.0.0 yaspin==0.15.0 zipp==3.15.0

george-zubrienko commented 2 months ago

@PJCampi you need to fork the repo and create a PR to main, then I can review and we can release a new version if all good

PJCampi commented 2 months ago

here you go: https://github.com/lidatong/dataclasses-json/pull/525/files

george-zubrienko commented 2 months ago

@PJCampi released :) thanks a lot!