Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignoring none field on serialization if it is not required. #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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