mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-12-01 22:14:06 +03:00
Fix for case of ListSerializer with single item (#4609)
This commit is contained in:
parent
0b346e94b1
commit
f1bdce17b5
|
@ -507,7 +507,7 @@ class Serializer(BaseSerializer):
|
||||||
@property
|
@property
|
||||||
def errors(self):
|
def errors(self):
|
||||||
ret = super(Serializer, self).errors
|
ret = super(Serializer, self).errors
|
||||||
if isinstance(ret, list) and len(ret) == 1 and ret[0].code == 'null':
|
if isinstance(ret, list) and len(ret) == 1 and getattr(ret[0], 'code', None) == 'null':
|
||||||
# Edge case. Provide a more descriptive error than
|
# Edge case. Provide a more descriptive error than
|
||||||
# "this field may not be null", when no data is passed.
|
# "this field may not be null", when no data is passed.
|
||||||
detail = ErrorDetail('No data provided', code='null')
|
detail = ErrorDetail('No data provided', code='null')
|
||||||
|
@ -705,7 +705,7 @@ class ListSerializer(BaseSerializer):
|
||||||
@property
|
@property
|
||||||
def errors(self):
|
def errors(self):
|
||||||
ret = super(ListSerializer, self).errors
|
ret = super(ListSerializer, self).errors
|
||||||
if isinstance(ret, list) and len(ret) == 1 and ret[0].code == 'null':
|
if isinstance(ret, list) and len(ret) == 1 and getattr(ret[0], 'code', None) == 'null':
|
||||||
# Edge case. Provide a more descriptive error than
|
# Edge case. Provide a more descriptive error than
|
||||||
# "this field may not be null", when no data is passed.
|
# "this field may not be null", when no data is passed.
|
||||||
detail = ErrorDetail('No data provided', code='null')
|
detail = ErrorDetail('No data provided', code='null')
|
||||||
|
|
|
@ -357,3 +357,16 @@ class TestSerializerValidationWithCompiledRegexField:
|
||||||
assert serializer.is_valid()
|
assert serializer.is_valid()
|
||||||
assert serializer.validated_data == {'name': '2'}
|
assert serializer.validated_data == {'name': '2'}
|
||||||
assert serializer.errors == {}
|
assert serializer.errors == {}
|
||||||
|
|
||||||
|
|
||||||
|
class Test4606Regression:
|
||||||
|
def setup(self):
|
||||||
|
class ExampleSerializer(serializers.Serializer):
|
||||||
|
name = serializers.CharField(required=True)
|
||||||
|
choices = serializers.CharField(required=True)
|
||||||
|
self.Serializer = ExampleSerializer
|
||||||
|
|
||||||
|
def test_4606_regression(self):
|
||||||
|
serializer = self.Serializer(data=[{"name": "liz"}], many=True)
|
||||||
|
with pytest.raises(serializers.ValidationError):
|
||||||
|
serializer.is_valid(raise_exception=True)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user