Commit 31539e1a by Tom Christie

Merge pull request #3393 from sloria/booleanfield-fix

Correctly handle [] and {} as invalid inputs to BooleanField
parents a67eed14 284f9faa
......@@ -608,10 +608,13 @@ class BooleanField(Field):
super(BooleanField, self).__init__(**kwargs)
def to_internal_value(self, data):
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):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment