Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
1.11.1
-
$ pip freeze | grep avro avro==1.11.1 $ python --version Python 3.8.16
Description
Using logical type UUID for a nullable field breaks when writing `None`.
import io import uuid from avro.io import DatumWriter, BinaryEncoder import avro.schema schema = avro.schema.parse(""" { "name": "Metadata", "type": "record", "fields": [ { "name": "topic_id", "type": [ { "logicalType": "uuid", "type": "string" }, "null" ] } ] } """) with io.BytesIO() as buffer: writer = DatumWriter(schema) encoder = BinaryEncoder(buffer) # This works. writer.write({"topic_id": uuid.uuid4().hex}, encoder) # This breaks. writer.write({"topic_id": None}, encoder)
Results in:
Traceback (most recent call last): File "reproduce-avro-uuid.py", line 32, in <module> writer.write({"topic_id": None}, encoder) File "/Users/anton/.pyenv/versions/karapace/lib/python3.8/site-packages/avro/io.py", line 1013, in write validate(self.writers_schema, datum, raise_on_error=True) File "/Users/anton/.pyenv/versions/karapace/lib/python3.8/site-packages/avro/io.py", line 142, in validate validated_schema = current_node.schema.validate(current_node.datum) File "/Users/anton/.pyenv/versions/karapace/lib/python3.8/site-packages/avro/schema.py", line 801, in validate if branch.validate(datum) is not None: File "/Users/anton/.pyenv/versions/karapace/lib/python3.8/site-packages/avro/schema.py", line 1048, in validate val = uuid.UUID(datum) File "/Users/anton/.pyenv/versions/3.8.16/lib/python3.8/uuid.py", line 165, in __init__ raise TypeError('one of the hex, bytes, bytes_le, fields, ' TypeError: one of the hex, bytes, bytes_le, fields, or int arguments must be given
Attachments
Issue Links
- is duplicated by
-
AVRO-3750 Python Optional UUIDs are not allowed
- Resolved
- links to