Error serialisation of Optional field and default value set is not None
See original GitHub issuefrom dataclasses import dataclass, field
from typing import List, Optional
import marshmallow_dataclass
import marshmallow.validate
@dataclass
class Building:
# field metadata is used to instantiate the marshmallow field
height: float = field(metadata={"validate": marshmallow.validate.Range(min=0)})
name: str = field(default="anonymous")
age: Optional[int] = field(default=0)
building_schema = marshmallow_dataclass.class_schema(Building)()
building_schema.load({"name": "Eiffel Tower", "height": 324, "age": None})
For above code I am getting this error:-
building_schema({"name": "Eiffel Tower", "height": 324, "age": None})
File "/XXX/.venv/lib/python3.7/site-packages/marshmallow_dataclass/__init__.py", line 544, in load
all_loaded = super().load(data, many=many, **kwargs)
File "/XXX/.venv/lib/python3.7/site-packages/marshmallow/schema.py", line 724, in load
data, many=many, partial=partial, unknown=unknown, postprocess=True
File "/XXX/.venv/lib/python3.7/site-packages/marshmallow/schema.py", line 911, in _do_load
raise exc
marshmallow.exceptions.ValidationError: {'age': ['Field may not be null.']}
Error is from marshmallow schema, but I am not able to find root cause of the error. Any help would be appreciated.
When age: Optional[int] = field(default=None)
above code works.
Python 3.7 marshmallow_dataclass tried on 8.0.0 and 8.1.0 marshmallow 3.8.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Setting default values to null fields when mapping with Jackson
After the mapping with Jackson, all the fields that are not set in the JSON object will have a null value in Java....
Read more >KIP-581: Value of optional null field which has default value
Generally, when an optional field which has default value is null , we can treat it as null or default value , it...
Read more >The Null and Drop Values — colander 1.8.3 documentation
If, during serialization, a value for the node is missing from the cstruct and the node does not possess an explicit default value,...
Read more >A Thorough Guide to Bond for C++ - Microsoft Open Source
Struct and bonded fields may not have an explicit default value. ... Optional fields set to nothing are usually omitted during serialization, just...
Read more >Model field reference - Django documentation
If True , Django will store empty values as NULL in the database. Default is False . Avoid using null on string-based fields...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Note that I’m afraid the fix I’m proposing in #121 might be considered as API-breaking, as some payloads that would have been rejected before will now be considered valid.
I still think this is the right way to go since this is how I expect
Optional
to behave, modeled on what it actually means for Python / MyPy: it’s actuallyUnion[T, None]
, which meansNone
is a valid value, including when explicitly passed as a value (vs. only as a default value).I’ll reopen this issue until this is solved