UUIDField not being validated for retrieval #7755
Replies: 6 comments
-
My feeling here is the url regex is left too permissive to allow invalid uuid. |
Beta Was this translation helpful? Give feedback.
-
I can confirm the issue, for different reasons. In Django 1.11, UUIDField's def to_python(self, value):
if value is not None and not isinstance(value, uuid.UUID):
try:
return uuid.UUID(value)
except (AttributeError, ValueError):
raise exceptions.ValidationError(
self.error_messages['invalid'],
code='invalid',
> params={'value': value},
)
E django.core.exceptions.ValidationError: ["'nope' is not a valid UUID."]
../../env/lib/python3.4/site-packages/django/db/models/fields/__init__.py:2401: ValidationError |
Beta Was this translation helpful? Give feedback.
-
Should be resolved in 3.6.3 - #5126 |
Beta Was this translation helpful? Give feedback.
-
Ah right, that'd fix the view-lookup, but not the serializer fields, I think? |
Beta Was this translation helpful? Give feedback.
-
I'm going to de-milestone this for now. I think (?) as reported this was solved by #5126. But the same issue should come up in related fields. e.g. ... django-rest-framework/rest_framework/relations.py Lines 246 to 253 in d8da6bb This should be an easy enough fix. It's just putting together the right test cases. Happy to see a PR for that! |
Beta Was this translation helpful? Give feedback.
-
Thanks for the help so far, guys :-) |
Beta Was this translation helpful? Give feedback.
-
Checklist
master
branch of Django REST framework.Steps to reproduce
Hi,
I'm a newcomer to DRF, and an issue I'm having is that I'm using Django's
UUIDField
in some models and using them as thelookup_field
s in my serializers. They work fine when I send a request with a correctly-formatted UUID, but DRF simply fails with a server error if the request sends an invalid string (such as "invalid") - instead of responding with HTTP 400 or 404, which was what I would expect. I tried debugging the code a bit, and it doesn't seem to be validated anywhere when callingretrieve()
from theRetrieveModelMixin
. Then I receive adjango.core.exceptions.ValidationError
which is bubbled up until the application layer, instead of being handled by DRF gracefully, since it's a case of user error and not application error.I believe this is a bug, but, if it's not and if it's up to the developers to code the validation themselves, please let me know.
So the steps to reproduce are pretty simple:
Expected behavior
Either an HTTP 400 or 404 response
Actual behavior
The application fails and lets the low-level validation error bubble up, responding with an HTTP 500 response to the client
Beta Was this translation helpful? Give feedback.
All reactions