Skip to content

Commit

Permalink
Ignoring none field if it is not required.
Browse files Browse the repository at this point in the history
  • Loading branch information
scenariobarros committed Mar 15, 2018
1 parent 56e5a27 commit db79da7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ docs/_build/

# Vim swap files
*.swp
/venv/
/.idea/
3 changes: 3 additions & 0 deletions serpy/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ def _serialize(self, instance, fields):
raise
else:
continue
if not required and not result:
continue

if required or result is not None:
if call:
result = result()
Expand Down
9 changes: 1 addition & 8 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class ASerializer(Serializer):

o = Obj(a=None)
data = ASerializer(o).data
self.assertIsNone(data['a'])
self.assertNotIn('a', data)

o = Obj(a='5')
data = ASerializer(o).data
Expand All @@ -176,9 +176,6 @@ class ASerializer(DictSerializer):
a = Field(required=False)

data = ASerializer({'a': None}).data
self.assertIsNone(data['a'])

data = ASerializer({}).data
self.assertNotIn('a', data)

class ASerializer(DictSerializer):
Expand All @@ -196,10 +193,6 @@ class ASerializer(Serializer):

o = Obj(a=None)
data = ASerializer(o).data
self.assertIsNone(data['a'])

o = Obj()
data = ASerializer(o).data
self.assertNotIn('a', data)

class ASerializer(Serializer):
Expand Down

0 comments on commit db79da7

Please sign in to comment.