Skip to content
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

Unexpected serializers.UUIDField behaviour #9555

Open
raqsilva8 opened this issue Oct 9, 2024 · 1 comment
Open

Unexpected serializers.UUIDField behaviour #9555

raqsilva8 opened this issue Oct 9, 2024 · 1 comment

Comments

@raqsilva8
Copy link

The issue

When the serializer receives a UUID that we might think is invalid, it gets "cleaned" up and considered valid, returning a completely different UUID.

How to replicate

from django.test import TestCase
from rest_framework import serializers

class TestSerializer(serializers.Serializer):
    uuid = serializers.UUIDField(format='hex_verbose')
    
class SerializerTest(TestCase):
    def test_serializer(self):
        self.assertTrue(TestSerializer(data={'uuid': '524e802a-061a-4778-b2f9-f5d3d7afd581'}).is_valid())
        serializer = TestSerializer(data={'uuid': '524e802_061a-4778-b2f9-f5d3d7afd581'})
        self.assertTrue(serializer.is_valid())
        self.assertEqual(str(serializer.validated_data['uuid']), '0524e802-061a-4778-b2f9-f5d3d7afd581')
        self.assertFalse(TestSerializer(data={'uuid': '524e802a_061a-4778-b2f9-f5d3d7afd581'}).is_valid())

A simple way to replicate is to actually use the uuid package:

import uuid
assert str(uuid.UUID(hex="524e802_061a-4778-b2f9-f5d3d7afd581")) == "0524e802-061a-4778-b2f9-f5d3d7afd581"

Note the differences 524e802_061a and 0524e802-061a

@sevdog
Copy link
Contributor

sevdog commented Oct 18, 2024

This is not a bug of DRF, it is a Python standardlib bug python/cpython#125651, the fact that you can provide an underscore to UUID constructor means that it is accepting invalid values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants