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

Allow customized Schema _serialize and _deserialize to get custom data from load/dump calls #1726

Open
sirosen opened this issue Jan 22, 2021 · 0 comments · May be fixed by #1725
Open

Allow customized Schema _serialize and _deserialize to get custom data from load/dump calls #1726

sirosen opened this issue Jan 22, 2021 · 0 comments · May be fixed by #1725

Comments

@sirosen
Copy link
Contributor

sirosen commented Jan 22, 2021

Although this is contained in #1725, I'm going to try to distill this down to exactly the feature I want, plus the context for why I want it.


This issue originates in an interaction between marshmallow-oneofschema ("oneofschema") and marshmallow-sqlalchemy. Both of these are important to marshmallow, especially since oneofschema is the go-to answer for questions about polymorphic fields.

oneofschema overloads load and dump with special operations. This works for most simple use cases, and most features (e.g. partial, many) are supported.
However, it means that oneofschema doesn't support pre- and post-processing hooks built into marshmallow, and the only way to support them in the current implementation, in which oneofschema replaces load and dump, is to reimplement the marshmallow load and dump logic.

Looking into options for this, _serialize and _deserialize are in exactly the right place for oneofschema to overload them with its specialized behavior. As proven in marshmallow-code/marshmallow-oneofschema#130 , the result is smaller code for oneofschema with added support for hooks.

It seems a perfect fit. However...

In marshmallow-code/marshmallow-oneofschema#111 , oneofschema added support for passing through additional keyword arguments from load and dump to its child schemas. The reason is that marshmallow-sqlalchemy also customizes load and dump, but in a different way. For that library, additional arguments control added behaviors.

If oneofschema starts using _serialize and _deserialize, it will lose the ability to support the marshmallow-sqlalchemy use-case.

If marshmallow wants to support oneofschema in this, I believe it would be best to consider _serialize and _deserialize part of the public interface for the purpose of subclassing and customizing schemas. The parallel with these method names for Fields already suggests them as semi-public.

The most naive approach would be to allow arbitrary keyword arguments to load to go through _do_load and down to _deserialize (and likewise for dump). However, that allows load to accept and ignore arbitrary arguments like parttial=True -- it makes the experience for users worse.
In #1725 I proposed a specific argument, extra, a mapping which exists only to support extensions like oneofschema. I'm open to other approaches. A major version of oneofschema, but not of marshmallow, would be needed to implement the change.

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

Successfully merging a pull request may close this issue.

1 participant