-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UUIDField validation does not catch AttributeError #3679
Comments
Looks to me like we should ensure data is a string when calling UUID(hex=data). |
Yes. Binary string prob also okay. |
And then only catch ValueError |
Alt: push upstream and ensure Django raises a TypeError in this case (it probably should, really) |
We may want to resolve both in REST framework, and upstream in Django. |
No, it's not. There's a bytes argument for binary. |
Got it! |
Correct me but uuid doesn't seem to be using Django's field for that one right ? |
Right yup, ignore my upstream comments! :p |
@xordoquy @tomchristie Sorry for commenting on an old issue, but I'm curious: why aren't |
@sloria I can't think of a case where we're get bytes as input to serializer. This issue was about deserialization while yours seems to be about serialization. |
The marshmallow PR affects both serialization and deserialization, since both call the same method which does the logic for handling different types: if isinstance(value, uuid.UUID):
return value
try:
if isinstance(value, bytes) and len(value) == 16:
return uuid.UUID(bytes=value)
else:
return uuid.UUID(value)
except (ValueError, AttributeError):
self.fail('invalid_uuid') Is there a reason not to handle bytes as input? |
@tomchristie @xordoquy Any thoughts on my comment above? Would it make sense to deserialize |
@sloria I don't think the parser would provide bytes. This is mostly why I'm curious about a use case where that would be required. |
The user who sent a PR to marshmallow said he was storing bytes in the database. Could this use case arise in Django as well? |
Yeah, I'm trying to sort out a custom |
I am using a UUIDField in a serializer as such:
If I make a request to this view with the parameter some_id mapped to a list, e.g.
{"some_id": [1, 2, 3]}
, I get anAttributeError: list object has no attribute replace
, even though the expected behavior is that the exception is caught byis_valid
and its return value isFalse
(since a list is clearly not valid input to a UUIDField)The
AttributeError
is thrown by Python's uuid.py (I am on Python 3.4.3) at the following line of UUID's__init__
method:I believe the issue lies in the
to_internal_value
method of UUIDField when it calls the UUID constructor to construct the field from the input data (code from https://github.com/tomchristie/django-rest-framework/blob/a8deb380ff70b5df8d3c62c9be981b60e363c7f9/rest_framework/fields.py#L767):Shouldn't
AttributeError
also be caught here before callingself.fail
(to register validation failure)?I an on djangorestframework version 3.3.1
Thanks for taking a look!
The text was updated successfully, but these errors were encountered: