Skip to content

RuntimeError: a null type in a union must be the first member #87

@greyblake

Description

@greyblake

Hi. thanks for the nice gem!

I'd like to report an issue.

Description

We have the following Avro schema:

{
  "type": "record",
  "namespace": "xyz",
  "name": "failureExample",
  "version": 1,
  "fields": [
    {
      "name": "position",
      "type": ["string", "null"],
      "default": "1",
      "logicalType": "standardDecimal"
    }
  ]
}

That results into the following error on attempt to build model from it:

RuntimeError: a null type in a union must be the first member

This exception comes from here: https://github.com/salsify/avromatic/blob/master/lib/avromatic/model/attributes.rb#L180

The problem is, that if we swap string and null types in the array, the the schema becomes invalid, because we need to use default value, and according to the avro specification default value type must match the first type in the union.

https://avro.apache.org/docs/1.8.2/spec.html#schema_record

default: A default value for this field, used when reading instances that lack this field (optional). Permitted values depend on the field's schema type, according to the table below. Default values for union fields correspond to the first schema in the union Default values for bytes and fixed fields are JSON strings, where Unicode code points 0-255 are mapped to unsigned 8-bit byte values 0-255.

Expected behaviour

The error should not be raised

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions