mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 09:36:49 +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)
|
||||
|
||||
def to_internal_value(self, data):
|
||||
if data in self.TRUE_VALUES:
|
||||
return True
|
||||
elif data in self.FALSE_VALUES:
|
||||
return False
|
||||
try:
|
||||
if data in self.TRUE_VALUES:
|
||||
return True
|
||||
elif data in self.FALSE_VALUES:
|
||||
return False
|
||||
except TypeError: # Input is an unhashable type
|
||||
pass
|
||||
self.fail('invalid', input=data)
|
||||
|
||||
def to_representation(self, value):
|
||||
|
|
|
@ -466,6 +466,18 @@ class TestBooleanField(FieldValues):
|
|||
}
|
||||
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):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user