Hi,

  I'm trying to serialize a Python3 dictionary into its Avro representation
but the DatumWriter says that my representation doesn't match the schema
for a union field. I don't know if this is a bug or I'm doing something
wrong, but this exact JSON with the avro-tools jsontofrag works flawlessly
with libraries in the JVM.

The example that is failing is:

import avro.io
import avro.schema
import io


SCHEMA="""
{
  "namespace": "com.example",
  "type": "record",
  "name": "UnionMessage",
  "fields": [
    {
      "name": "content",
      "type": [
        {
          "namespace": "com.example",
          "type": "record",
          "name": "Option1",
          "fields": [
            {
              "type": "int",
              "name": "hasAnInt"
            }
          ]
        },
        {
          "namespace": "com.example",
          "name": "Option2",
          "type": "record",
          "fields": [
            {
              "type": "string",
              "name": "hasAString"
            }
          ]
        }
      ]
    }
  ]
}
"""

schema = avro.schema.Parse(SCHEMA)
rec_writer = avro.io.DatumWriter(schema)
bytes_writer = io.BytesIO()
encoder = avro.io.BinaryEncoder(bytes_writer)

msg = { "content": {"Option2": {"hasAString": "Hello"}}}
rec_writer.write(msg, encoder)

And I get back:

avro.io.AvroTypeException: The datum {'content': {'Option2': {'hasAString':
'Hello'}}} is not an example of the schema {
.....


Any ideas of what could be going wrong here?
monsieurdrive.com

Reply via email to