Correctly handle [] and {} as invalid inputs to BooleanField

This commit is contained in:
Steven Loria 2015-09-11 23:01:18 -04:00
parent a67eed1466
commit 284f9faa07
2 changed files with 19 additions and 4 deletions

View File

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

View File

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