marshmallow icon indicating copy to clipboard operation
marshmallow copied to clipboard

Non-obvious error when using fields.Nested with schema instance and many

Open mmulqueen opened this issue 3 years ago • 0 comments

I discovered the hard way that fields.Nested silently ignores the many parameter when it's passed an instance. It's my fault, I can see in the docstring that this behaviour is described, but that didn't stop me wasting a load of time debugging the error. I see that using many=True is no longer recommended anyway, but it's not deprecated yet either - #779 may change that. The issue here isn't how Marshmallow behaves, but that it isn't obvious.

Actual behaviour:

Preferred, works: fields.List(fields.Nested(Child())) Old style with nested class, works: fields.Nested(Child, many=True) Old style with nested instance, broken: fields.Nested(Child(), many=True)

When trying the nested instance way, the error you get is unhelpful because it's the result of Marshmallow treating the nested schema as singular:

 {'_schema': ['Invalid input type.']}

Confusing things even further, many and a nested instance seem to work when dumping (even though it shouldn't), but not when loading (as described). Since the broken way half works, I'm sure there will be code out there that relies on it.

Pull request to follow.

mmulqueen avatar May 03 '22 17:05 mmulqueen