Skip to content

Commit

Permalink
Default allow_null=True for Nested fields
Browse files Browse the repository at this point in the history
[close #132]
  • Loading branch information
sloria committed Feb 19, 2015
1 parent 6e4a4c8 commit db0ffd7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Expand Up @@ -25,6 +25,7 @@ Deprecation/Removals:
Other changes:

- ``Marshaller``, ``Unmarshaller``, ``null``, and ``missing`` were moved to ``marshmallow.marshalling``. These should be considered private API (:issue:`129`).
- Make ``allow_null=True`` the default for ``Nested`` fields. This will make ``None`` serialize to ``None`` rather than a dictionary with empty values (:issue:`132`). Thanks :user:`nickrellack` for the suggestion.

1.2.2 (unreleased)
++++++++++++++++++
Expand Down
9 changes: 6 additions & 3 deletions marshmallow/fields.py
Expand Up @@ -316,13 +316,13 @@ class Nested(Field):
will be marshalled. If a field name (string) is given, only a single
value will be returned as output instead of a dictionary.
This parameter takes precedence over ``exclude``.
:param bool allow_null: Whether to return None instead of a dictionary
with null keys, if a nested dictionary has all-null keys
:param bool allow_null: Whether to return `None` instead of a dictionary
with null keys, if a nested dictionary has all-null keys.
:param bool many: Whether the field is a collection of objects.
:param kwargs: The same keyword arguments that :class:`Field` receives.
"""

def __init__(self, nested, default=null, exclude=tuple(), only=None, allow_null=False,
def __init__(self, nested, default=null, exclude=tuple(), only=None, allow_null=True,
many=False, **kwargs):
self.nested = nested
self.allow_null = allow_null
Expand Down Expand Up @@ -371,6 +371,9 @@ def schema(self):
return self.__schema

def _serialize(self, nested_obj, attr, obj):
# Load up the schema first. This allows a RegistryError to be raised
# if an invalid schema name was passed
schema = self.schema
if nested_obj is None:
if self.many:
return []
Expand Down
4 changes: 2 additions & 2 deletions tests/test_registry.py
Expand Up @@ -100,12 +100,12 @@ def test_multiple_classes_with_all():
def test_can_use_full_module_path_to_class():
from .foo_serializer import FooSerializer as FooSerializer1 # noqa
# Using full paths is ok
field = fields.Nested('tests.foo_serializer.FooSerializer')
field = fields.Nested('tests.foo_serializer.FooSerializer', allow_null=False)

# Note: The arguments here don't matter. What matters is that no
# error is raised
assert field.serialize('bar', {'foo': {'_id': 42}})

field2 = fields.Nested('tests.test_registry.FooSerializer')
field2 = fields.Nested('tests.test_registry.FooSerializer', allow_null=False)

assert field2.serialize('bar', {'foo': {'_id': 42}})
2 changes: 1 addition & 1 deletion tests/test_schema.py
Expand Up @@ -1420,7 +1420,7 @@ class MySchema2(Schema):

s = MySchema()
result = s.dump({'foo': None})
assert result.data['foo'] == {'bar': None}
assert result.data['foo'] is None

s2 = MySchema2()
result2 = s2.dump({'foo': None})
Expand Down

0 comments on commit db0ffd7

Please sign in to comment.