Skip to content

Consistency with allow_none with serialization and deserialization #2836

@matejsp

Description

@matejsp

I saw already comments how to workaround this issue:
#229 (comment)
#229 (comment)

But still I would like to marshmallow to be consistent with itself.

Basically you have a schema with allow_none=False and after dumping it you cannot load it back (without manually fixing the output that is not correct according to schema).

import marshmallow

class TransferSchema(marshmallow.Schema):
    id = marshmallow.fields.Int(allow_none=False)
    str = marshmallow.fields.Str(allow_none=False)


x = TransferSchema()
dumped = x.dump({
    'id': None,
    'str': None,
})

print(dumped)
loaded = x.load(dumped)
print(loaded)

error:

marshmallow.exceptions.ValidationError: {'id': ['Field may not be null.'], 'str': ['Field may not be null.']}

I think allow_none should be respected for both on serialize and deserialize.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions