Merge pull request #3393 from sloria/booleanfield-fix

Correctly handle [] and {} as invalid inputs to BooleanField
This commit is contained in:
Tom Christie 2015-09-12 07:20:56 +01:00
commit 31539e1a21
2 changed files with 19 additions and 4 deletions

View File

@ -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):

View File

@ -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):
"""