mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 03:23:59 +03:00
Correctly handle [] and {} as invalid inputs to BooleanField
This commit is contained in:
parent
a67eed1466
commit
284f9faa07
|
@ -608,10 +608,13 @@ class BooleanField(Field):
|
||||||
super(BooleanField, self).__init__(**kwargs)
|
super(BooleanField, self).__init__(**kwargs)
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
if data in self.TRUE_VALUES:
|
try:
|
||||||
return True
|
if data in self.TRUE_VALUES:
|
||||||
elif data in self.FALSE_VALUES:
|
return True
|
||||||
return False
|
elif data in self.FALSE_VALUES:
|
||||||
|
return False
|
||||||
|
except TypeError: # Input is an unhashable type
|
||||||
|
pass
|
||||||
self.fail('invalid', input=data)
|
self.fail('invalid', input=data)
|
||||||
|
|
||||||
def to_representation(self, value):
|
def to_representation(self, value):
|
||||||
|
|
|
@ -466,6 +466,18 @@ class TestBooleanField(FieldValues):
|
||||||
}
|
}
|
||||||
field = serializers.BooleanField()
|
field = serializers.BooleanField()
|
||||||
|
|
||||||
|
def test_disallow_unhashable_collection_types(self):
|
||||||
|
inputs = (
|
||||||
|
[],
|
||||||
|
{},
|
||||||
|
)
|
||||||
|
field = serializers.BooleanField()
|
||||||
|
for input_value in inputs:
|
||||||
|
with pytest.raises(serializers.ValidationError) as exc_info:
|
||||||
|
field.run_validation(input_value)
|
||||||
|
expected = ['"{0}" is not a valid boolean.'.format(input_value)]
|
||||||
|
assert exc_info.value.detail == expected
|
||||||
|
|
||||||
|
|
||||||
class TestNullBooleanField(FieldValues):
|
class TestNullBooleanField(FieldValues):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user