Anton Agestam created AVRO-3761:
-----------------------------------

             Summary: UUID validation breaks nullable field
                 Key: AVRO-3761
                 URL: https://issues.apache.org/jira/browse/AVRO-3761
             Project: Apache Avro
          Issue Type: Bug
          Components: python
    Affects Versions: 1.11.1
         Environment: {code:perl}
$ pip freeze | grep avro
avro==1.11.1
$ python --version
Python 3.8.16
{code}
            Reporter: Anton Agestam


Using logical type UUID for a nullable field breaks when writing `None`.


{code:python}
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)
{code}

Results in:

{code:perl}
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
{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to