Skip to content
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

visit @dataclass hierarchy instead of subtyping? #109

Closed
froh opened this issue May 12, 2023 · 2 comments · Fixed by #131
Closed

visit @dataclass hierarchy instead of subtyping? #109

froh opened this issue May 12, 2023 · 2 comments · Fixed by #131
Labels
enhancement New feature or request

Comments

@froh
Copy link

froh commented May 12, 2023

Given a class hierarchy as generated by xsdata, I'd like to use mashumaro to (de)serialize it.

I can not inject the DataClass...Mixins, the class hierarchy is "as-is". however the class hierarchy is plain and simple and stand-alone python.

The solution I'd like
I'd love to see a mashumaro "visitor" which given an arbitrary @dataclass C recursively walks the fields fields(C) and uses that information to compile a serializer and a deserializer.

Alternatives I've considered
I've tried to find ways to inject one of the mixins into the generated classes. however that xsdata strongly opines against modifying the generated model in any shape or form.

Additional context
I think having the possibility to pass mashumaro any dataclass and have it examine the class and it's fields recursively without the need for mixins would be amazing. It would also open the door to serialize and deserialize into different formats. I love the idea of mashumaro to precompile the parser and the serializer.

@Fatal1ty
Copy link
Owner

Thanks for opening this issue and for a good use case.

The reason why we currently check the mixin subclassing is the dependence on the presence of from_* / to_* methods in nested dataclasses. If we were using is_dataclass for nested dataclasses we wouldn't deserialize them because we need from_dict method which will be missing. On the other hand, I understand the importance of supporting dataclasses without the mixin, so I was thinking of injecting __mashumaro_from_* / __mashumaro_to_* methods to the nested dataclasses by the visitor and use them appropriately. The visitor is already doing this with the from_* / to_* methods if it sees a nested DataClassDictMixin subclass without an appropriate method for the first time. I'm going to make this improvement in one of the next releases.

@Fatal1ty
Copy link
Owner

Hi @froh

I made some changes so that inner dataclasses can be (de)serialized without mixins:

@dataclass
class Foo:
    x: int

@dataclass
class Bar(DataClassDictMixin):
    foo: Foo

print(Bar.from_dict({"foo": {"x": 42}}))  # Bar(foo=Foo(x=42))
print(Bar(Foo(42)).to_dict())  # {'foo': {'x': 42}}

I hope it will help integrating with xsdata. The root dataclass still needs a mixin but it's another issue that will be resolved here and here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants