You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2023/06/16 07:27:50 UTC
[avro] branch branch-1.11 updated: AVRO-3761: [python] Fix broken validation of nullable UUID field (#2249)
This is an automated email from the ASF dual-hosted git repository.
rskraba pushed a commit to branch branch-1.11
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/branch-1.11 by this push:
new d76688847 AVRO-3761: [python] Fix broken validation of nullable UUID field (#2249)
d76688847 is described below
commit d766888476764d8befb953225d22597d6b043340
Author: Anton Agestam <an...@aiven.io>
AuthorDate: Fri Jun 16 08:57:18 2023 +0200
AVRO-3761: [python] Fix broken validation of nullable UUID field (#2249)
* AVRO-3761: [python] Fix broken validation of nullable UUID field
* Use constant zero
---------
Co-authored-by: Ryan Skraba <ry...@skraba.com>
---
lang/py/avro/schema.py | 6 ++----
lang/py/avro/test/test_io.py | 18 ++++++++++++++++++
2 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/lang/py/avro/schema.py b/lang/py/avro/schema.py
index 3bc0c27af..8a64cb315 100644
--- a/lang/py/avro/schema.py
+++ b/lang/py/avro/schema.py
@@ -1042,10 +1042,8 @@ class UUIDSchema(LogicalSchema, PrimitiveSchema):
def validate(self, datum):
try:
- val = uuid.UUID(datum)
- except ValueError:
- # If it's a value error, then the string
- # is not a valid hex code for a UUID.
+ uuid.UUID(datum)
+ except (ValueError, TypeError):
return None
return self
diff --git a/lang/py/avro/test/test_io.py b/lang/py/avro/test/test_io.py
index b77c17fb9..36a85b3e5 100644
--- a/lang/py/avro/test/test_io.py
+++ b/lang/py/avro/test/test_io.py
@@ -24,6 +24,7 @@ import io
import itertools
import json
import unittest
+import uuid
import warnings
from typing import BinaryIO, Collection, Dict, List, Optional, Tuple, Union, cast
@@ -185,6 +186,23 @@ SCHEMAS_TO_VALIDATE = tuple(
{"type": "record", "name": "ns.long", "fields": [{"name": "value", "type": "int"}, {"name": "next", "type": ["null", "ns.long"]}]},
{"value": 0, "next": {"value": 1, "next": None}},
),
+ # Optional logical types.
+ (
+ [{"logicalType": "uuid", "type": "string"}, "null"],
+ None,
+ ),
+ (
+ [{"logicalType": "uuid", "type": "string"}, "null"],
+ uuid.uuid4().hex,
+ ),
+ (
+ [{"type": "long", "logicalType": "timestamp-millis"}, "null"],
+ datetime.datetime(1000, 1, 1, 0, 0, 0, 0, tzinfo=avro.timezones.utc),
+ ),
+ (
+ [{"type": "long", "logicalType": "timestamp-millis"}, "null"],
+ None,
+ ),
)
)