Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-3761

UUID validation breaks nullable field

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.11.1
    • 1.11.2
    • python
    • $ 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

          Activity

            People

              antonagestam-aiven Anton Agestam
              antonagestam-aiven Anton Agestam
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m