avromatic icon indicating copy to clipboard operation
avromatic copied to clipboard

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

Open greyblake opened this issue 5 years ago • 2 comments

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

greyblake avatar Aug 13 '18 11:08 greyblake

Hi @greyblake,

Currently the gem is opinionated about this. If null is a member type within a union then usually people want it as the default, so I think this helps to catch a common error case. (The official avro ruby gem does not currently validate field defaults.)

While I think this should remain the default behavior of this gem, perhaps it could be made configurable to allow null anywhere within a union. Would you like to try opening a pull request to make this change?

tjwp avatar Aug 18 '18 15:08 tjwp

@tjwp Despite the opnioned behaviour, I have to admit that this helped us to discover a miscommunication problem in our team:)

Would you like to try opening a pull request to make this change?

Unfortunately I don't have much capacities.. But if you can guide me where to start I can give it a try. Otherwise please feel free to close the ticket.

greyblake avatar Aug 20 '18 16:08 greyblake