Skip to content

Commit

Permalink
🐛 Fix handling of UUID values having UUID.version=None (#981)
Browse files Browse the repository at this point in the history
Co-authored-by: Serge Matveenko <lig@pydantic.dev>
Co-authored-by: David Hewitt <1939362+davidhewitt@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 21, 2023
1 parent 4c84ed8 commit 33a7cc0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/validators/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl From<u8> for Version {
#[derive(Debug, Clone)]
pub struct UuidValidator {
strict: bool,
version: Option<Version>,
version: Option<usize>,
}

impl BuildValidator for UuidValidator {
Expand All @@ -71,10 +71,11 @@ impl BuildValidator for UuidValidator {
_definitions: &mut DefinitionsBuilder<CombinedValidator>,
) -> PyResult<CombinedValidator> {
let py = schema.py();
// Note(lig): let's keep this conversion through the Version enum just for the sake of validation
let version = schema.get_as::<u8>(intern!(py, "version"))?.map(Version::from);
Ok(Self {
strict: is_strict(schema, config)?,
version,
version: version.map(usize::from),
}
.into())
}
Expand All @@ -92,9 +93,11 @@ impl Validator for UuidValidator {
let class = get_uuid_type(py)?;
if let Some(py_input) = input.input_is_instance(class) {
if let Some(expected_version) = self.version {
let py_input_version: usize = py_input.getattr(intern!(py, "version"))?.extract()?;
let expected_version = usize::from(expected_version);
if expected_version != py_input_version {
let py_input_version: Option<usize> = py_input.getattr(intern!(py, "version"))?.extract()?;
if !match py_input_version {
Some(py_input_version) => py_input_version == expected_version,
None => false,
} {
return Err(ValError::new(
ErrorType::UuidVersion {
expected_version,
Expand Down Expand Up @@ -179,7 +182,6 @@ impl UuidValidator {

if let Some(expected_version) = self.version {
let v1 = uuid.get_version_num();
let expected_version = usize::from(expected_version);
if v1 != expected_version {
return Err(ValError::new(
ErrorType::UuidVersion {
Expand Down
18 changes: 17 additions & 1 deletion tests/validators/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
('6ba7b810-9dad-11d1-80b4-00c04fd430c8', UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')),
('886313e1-3b8a-5372-9b90-0c9aee199e5d', UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')),
('c0a8f9a8-aa5e-482b-a067-9cb3a51f5c11', UUID('c0a8f9a8-aa5e-482b-a067-9cb3a51f5c11')),
('00000000-8000-4000-8000-000000000000', UUID('00000000-8000-4000-8000-000000000000')),
(b'\x12\x34\x56\x78' * 4, UUID('12345678-1234-5678-1234-567812345678')),
(b'\x00\x00\x00\x00' * 4, UUID('00000000-0000-0000-0000-000000000000')),
(b'ebcdab58-6eb8-46fb-a190-d07a33e9eac8', UUID('ebcdab58-6eb8-46fb-a190-d07a33e9eac8')),
Expand Down Expand Up @@ -118,6 +119,16 @@ def test_uuid_strict(input_value, expected):
(UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7'), 4, UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7')),
('0e7ac198-9acd-4c0c-b4b4-761974bf71d7', 4, UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7')),
(UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7'), 4, UUID('0e7ac198-9acd-4c0c-b4b4-761974bf71d7')),
# Cases from pydantic#7355 and pydantic#7537
# `UUID.version` makes sense for RFC 4122 UUIDs only. For non RFC 4122 UUIDs Python uses `UUID.version=None`
('00000000-8000-4000-8000-000000000000', 4, UUID('00000000-8000-4000-8000-000000000000')),
(UUID('00000000-8000-4000-8000-000000000000'), 4, UUID('00000000-8000-4000-8000-000000000000')),
('00000000-7fff-4000-7fff-000000000000', None, UUID('00000000-7fff-4000-7fff-000000000000')),
(UUID('00000000-7fff-4000-7fff-000000000000'), None, UUID('00000000-7fff-4000-7fff-000000000000')),
(UUID('00000000-7fff-4000-7fff-000000000000'), 4, Err('UUID version 4 expected')),
('b34b6755-f49c-3bd2-6f06-131a708c2bf3', None, UUID('b34b6755-f49c-3bd2-6f06-131a708c2bf3')),
(UUID('b34b6755-f49c-3bd2-6f06-131a708c2bf3'), None, UUID('b34b6755-f49c-3bd2-6f06-131a708c2bf3')),
(UUID('b34b6755-f49c-3bd2-6f06-131a708c2bf3'), 4, Err('UUID version 4 expected')),
# Invalid UUIDs
('a6cc5730-2261-11ee-9c43-2eb5a363657c', 5, Err('UUID version 5 expected')),
(UUID('a6cc5730-2261-11ee-9c43-2eb5a363657c'), 5, Err('UUID version 5 expected')),
Expand All @@ -130,7 +141,12 @@ def test_uuid_strict(input_value, expected):
],
)
def test_uuid_version(input_value, version, expected):
v = SchemaValidator({'type': 'uuid', 'version': version})
schema = {'type': 'uuid'}
if version is not None:
schema['version'] = version

v = SchemaValidator(schema)

if isinstance(expected, Err):
with pytest.raises(ValidationError, match=re.escape(expected.message)):
v.validate_python(input_value)
Expand Down

0 comments on commit 33a7cc0

Please sign in to comment.