marshmallow-code / marshmallow-code/marshmallow
Inconsistent None items serialization between Nested(…, many=True) and List(Nested(…)) fields
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.2k
- Forks
- 738
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 7
Description
At this point, a `Nested` field with the `many=True` parameter and a `Nested` field wrapped in the `List` will serialize `None` items differently: the first one will return a list of empty dictionaries, the latter one will return a list of `None`.
It’s unclear if it’s intentional or not, but it’s frustrating to say at least because these two concepts presumably imply to be interchangeable. Of course, #779 exists not without reason, but it’s a more complicated matter, and it’ll celebrate its second birthday in a few days.
So the main question here is: should it stay inconsistent by design or one approach is provenly false, and needs to be fixed.
Just in case, it’s easy to reproduce with the following example:
```python
from marshmallow import Schema, fields
class N(Schema):
a = fields.String()
b = fields.String()
class S1(Schema):
nested = fields.Nested(N, many=True)
class S2(Schema):
nested = fields.List(fields.Nested(N))
schema1 = S1()
schema2 = S2()
data = {
"nested": [None],
}
result1 = schema1.dump(data)
result2 = schema2.dump(data)
assert result1.get("nested") == [{}]
assert result2.get("nested") == [None]
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided reproduction using fields.Nested(many=True) and fields.List(fields.Nested(...)), then trace how each field serializes a None item. Compare the resulting behavior and related issue #779; done means reaching a documented design decision and aligning the behavior if a fix is selected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100