question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error serialisation of Optional field and default value set is not None

See original GitHub issue
from 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:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
noirbeecommented, Jan 8, 2021

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 actually Union[T, None], which means None is a valid value, including when explicitly passed as a value (vs. only as a default value).

1reaction
lovasoacommented, Nov 17, 2020

I’ll reopen this issue until this is solved

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found