lerouxrgd / rsgen-avro

Command line and library for generating Rust types from Avro schemas
MIT License
36 stars 29 forks source link

Union types should allow "null" in any position #39

Closed chupaty closed 1 year ago

chupaty commented 2 years ago

Using the following schema:

{
    "type": "record",
    "name": "test",
    "fields": [
        {"name": "a", "type": ["long","null"] }
    ]
}

rsgen-avro errors with:

Templating error: Invalid Schema::Null not in first position on an UnionSchema variants

I don't see a requirement in the avro spec about this (we do have additional requirements when we have a default value, but this doesn't apply here).

martin-g commented 2 years ago

I think the reason for the current implementation is the following text from the spec: Thus, for unions containing “null”, the “null” is usually listed first, since the default value of such unions is typically null.

But I agree that it could be improved to support your use case. And the spec says usually, not mandatory.

I will send a PR!

lerouxrgd commented 1 year ago

This crate will make the conservative approach of using "null" only in first position as otherwise things get really too complicated.

lerouxrgd commented 1 year ago

(Although this can be reconsidered later, when the spec is clearer, and when it's supported in apache-avro etc)