Search code examples
eventsavro

Multiple non-null types in Avro field type union


I've seen many examples of Avro schema fields having unions allowing a type OR null, e.g:

{
  "name": "MyMessageType",
  "type": "record",
  "fields": [
    {
      "name": "userId",
      "type": [
        "null",
        "string"
      ],
      "default": null,
    },
    { ... more fields ... }
  ]
}

I've never seen an example showing multiple non-null types, e.g:

{
  "name": "MyMessageType",
  "type": "record",
  "fields": [
    {
      "name": "userId",
      "type": [
        "null",
        "string",
        "int"
      ],
      "default": null,
    },
    { ... more fields ... }
  ]
}

The docs don't mention this either way - is it possible?

And is it recommended? Has anyone used this format before?


Solution

  • This is definitely supported. There is no recommendation for or against. It really just depends on the needs of your schema.