Skip to content

Commit c153bcb

Browse files
author
bphillips
committed
Added validation to UUIDField to properly catch invalid input types (lists, tuples, etc).
1 parent d2f90fd commit c153bcb

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

rest_framework/fields.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,9 +769,11 @@ def to_internal_value(self, data):
769769
try:
770770
if isinstance(data, six.integer_types):
771771
return uuid.UUID(int=data)
772-
else:
772+
elif isinstance(data, six.string_types):
773773
return uuid.UUID(hex=data)
774-
except (ValueError, TypeError):
774+
else:
775+
self.fail('invalid', value=data)
776+
except (ValueError):
775777
self.fail('invalid', value=data)
776778
return data
777779

tests/test_fields.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,8 @@ class TestUUIDField(FieldValues):
609609
284758210125106368185219588917561929842: uuid.UUID('d63a6fb6-88d5-40c7-a91c-9edf73283072')
610610
}
611611
invalid_inputs = {
612-
'825d7aeb-05a9-45b5-a5b7': ['"825d7aeb-05a9-45b5-a5b7" is not a valid UUID.']
612+
'825d7aeb-05a9-45b5-a5b7': ['"825d7aeb-05a9-45b5-a5b7" is not a valid UUID.'],
613+
(1, 2, 3): ['"(1, 2, 3)" is not a valid UUID.']
613614
}
614615
outputs = {
615616
uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda'): '825d7aeb-05a9-45b5-a5b7-05df87923cda'

0 commit comments

Comments
 (0)