Skip to content

Commit 237ef2b

Browse files
committed
Fix for case of ListSerializer with single item
1 parent e3686ac commit 237ef2b

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

rest_framework/serializers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ def data(self):
507507
@property
508508
def errors(self):
509509
ret = super(Serializer, self).errors
510-
if isinstance(ret, list) and len(ret) == 1 and ret[0].code == 'null':
510+
if isinstance(ret, list) and len(ret) == 1 and getattr(ret[0], 'code', None) == 'null':
511511
# Edge case. Provide a more descriptive error than
512512
# "this field may not be null", when no data is passed.
513513
detail = ErrorDetail('No data provided', code='null')
@@ -705,7 +705,7 @@ def data(self):
705705
@property
706706
def errors(self):
707707
ret = super(ListSerializer, self).errors
708-
if isinstance(ret, list) and len(ret) == 1 and ret[0].code == 'null':
708+
if isinstance(ret, list) and len(ret) == 1 and getattr(ret[0], 'code', None) == 'null':
709709
# Edge case. Provide a more descriptive error than
710710
# "this field may not be null", when no data is passed.
711711
detail = ErrorDetail('No data provided', code='null')

tests/test_serializer.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,16 @@ def test_validation_success(self):
357357
assert serializer.is_valid()
358358
assert serializer.validated_data == {'name': '2'}
359359
assert serializer.errors == {}
360+
361+
362+
class Test4606Regression:
363+
def setup(self):
364+
class ExampleSerializer(serializers.Serializer):
365+
name = serializers.CharField(required=True)
366+
choices = serializers.CharField(required=True)
367+
self.Serializer = ExampleSerializer
368+
369+
def test_4606_regression(self):
370+
serializer = self.Serializer(data=[{"name": "liz"}], many=True)
371+
with pytest.raises(serializers.ValidationError):
372+
serializer.is_valid(raise_exception=True)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy