Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
greyblake opened this issue Aug 13, 2018 · 2 comments
Open

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

greyblake opened this issue Aug 13, 2018 · 2 comments

Comments

@greyblake
Copy link

greyblake commented Aug 13, 2018

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

@tjwp
Copy link
Collaborator

tjwp commented Aug 18, 2018

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?

@greyblake
Copy link
Author

greyblake commented Aug 20, 2018

@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.

@jturkel jturkel added this to the 2.0 milestone Oct 9, 2018
@jturkel jturkel removed this from the 2.0 milestone Oct 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants