Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Marshmallow ext: Name should be set to dump_to even if load_from is not specified #178
At the moment, name is changed if both load_from and dump_to is set and are equal, as per this code:
This behaviour causes schema to look bad and does not make sense.
Related to marshmallow-code/marshmallow#717.
I think this is a dark corner case.
I don't know how to expose a situation where a field is used for dump and load and the key differ in both dump and load. I'd use two schemas. Now, assuming the schema or the field is dump_only, it makes sense to only specify dump_to and expect it to appear in the spec. Conversely, if a schema is load_only, it should be exposed correctly by just specifying load_only.
AFAIK, OpenAPI does not provide the option for write_only fields, so dump_only might be the only real-life use case here in practice for now.
I suppose we could write
# use getattr in case we're running against older versions of marshmallow. dump_to = getattr(field, 'dump_to', None) return dump_to or load_from or name
and cover most if not all legit cases. Right?
Yet, I don't really see a proper way to solve this and this is one of the motivations of marshmallow-code/marshmallow#717.