Skip to content

Commit

Permalink
🐛 Fix handling of UUID values having UUID.version=None
Browse files Browse the repository at this point in the history
  • Loading branch information
lig committed Sep 21, 2023
1 parent 6d8a59b commit ba2c24c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 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
22 changes: 14 additions & 8 deletions tests/validators/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +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')),
# special cases from pydantic#7355 and pydantic#7537
# 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', 4, UUID('00000000-7fff-4000-7fff-000000000000')),
(UUID('00000000-7fff-4000-7fff-000000000000'), 4, UUID('00000000-7fff-4000-7fff-000000000000')),
('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')),
('b34b6755-f49c-3bd2-6f06-131a708c2bf3', 4, UUID('b34b6755-f49c-3bd2-6f06-131a708c2bf3')),
(UUID('b34b6755-f49c-3bd2-6f06-131a708c2bf3'), 4, UUID('b34b6755-f49c-3bd2-6f06-131a708c2bf3')),
('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 @@ -140,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 ba2c24c

Please sign in to comment.