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

Serializers with many=True should return empty list rather than empty dict #3476

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 17 additions & 8 deletions rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def __init__(self, instance=None, data=empty, **kwargs):
self.initial_data = data
self.partial = kwargs.pop('partial', False)
self._context = kwargs.pop('context', {})
kwargs.pop('many', None)
self.many = kwargs.pop('many', None)
super(BaseSerializer, self).__init__(**kwargs)

def __new__(cls, *args, **kwargs):
Expand Down Expand Up @@ -212,10 +212,10 @@ def is_valid(self, raise_exception=False):
try:
self._validated_data = self.run_validation(self.initial_data)
except ValidationError as exc:
self._validated_data = {}
self._validated_data = [] if self.many else {}
self._errors = exc.detail
else:
self._errors = {}
self._errors = [] if self.many else {}

if self._errors and raise_exception:
raise ValidationError(self.errors)
Expand Down Expand Up @@ -669,18 +669,27 @@ def __repr__(self):
# Include a backlink to the serializer class on return objects.
# Allows renderers such as HTMLFormRenderer to get the full field info.

@property
def data(self):
ret = super(ListSerializer, self).data
return ReturnList(ret, serializer=self)

@property
def errors(self):
ret = super(ListSerializer, self).errors

if ret == {}:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be ret == {} and self.many ?

return ReturnList([], serializer=self)

if isinstance(ret, dict):
return ReturnDict(ret, serializer=self)

return ReturnList(ret, serializer=self)

@property
def validated_data(self):
validated_data = super(ListSerializer, self).validated_data

if validated_data == {}:
return ReturnList([], serializer=self)

return validated_data


# ModelSerializer & HyperlinkedModelSerializer
# --------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions tests/test_serializer_bulk_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_bulk_create_success(self):
serializer = self.BookSerializer(data=data, many=True)
self.assertEqual(serializer.is_valid(), True)
self.assertEqual(serializer.validated_data, data)
self.assertEqual(serializer.errors, [])

def test_bulk_create_errors(self):
"""
Expand Down Expand Up @@ -76,6 +77,7 @@ def test_bulk_create_errors(self):
serializer = self.BookSerializer(data=data, many=True)
self.assertEqual(serializer.is_valid(), False)
self.assertEqual(serializer.errors, expected_errors)
self.assertEqual(serializer.validated_data, [])

def test_invalid_list_datatype(self):
"""
Expand Down