Open sanderland opened 4 years ago
Hello,
I hit exactly the same issue with marshmallow 3.12.1 but with values
instead of items
(and I didn't try, but I think that the problem is there with any other builtin method, like keys
)
Edit: It seems to me that the problem only occurs only if the values/items key is missing in the dictionary... Only in that case the serialization is falling back to the extraction of the builtin method
For anyone who makes it here, the fix is to put the following:
items_ = fields.List(fields.Nested(ListItem), data_key='items')
values_ = fields.List(fields.Nested(ListItem), data_key='values')
The error message could be more descriptive to alert the user to these "special" keys.
I'll start with a simple example
The issue appears because on
utils.py
's function_get_value_for_key
it first tries to getattr, then obj[key] (without default), and finally getattr again, resulting in theitems
method for dict, rather than a default. This is extremely unexpected behaviour.A workaround is to override
get_attribute
to try.get
first on dictionaries.I am not sure what the prefered fix in marshmallow would be given the rather dizzying levels of indirection and cases in the get attribute chain.