'Why gogen-avro generated container fails to deal with optional fields?
I have a Go consumer and producer. And the following schema:
{
"namespace" : "lets.see.how.it.works",
"type" : "record",
"name" : "DataItem",
"fields" : [ {
"name" : "basics",
"type": {
"type" : "record",
"name" : "Basics",
"fields" : [
{"name" : "id", "type" : "string"},
{"name" : "value", "type" : ["null" , "string"]},
{"name" : "alwaysNil", "type" : ["null" , "string"]}
]
}
}, {
"name" : "extra",
"type": ["null", {
"type" : "record",
"name" : "Extra",
"fields" : [
{"name" : "foo", "type" : ["null" , "string"], "default": "null"},
{"name" : "boo", "type" : ["null" , "string"], "default": "null"}
]
}],
"default": "null"
}, {
"name" : "payload",
"type": {
"type": "record",
"name": "Payload",
"fields": [
{"name" : "val1", "type" : [ "null", "double" ], "default": "null"},
{"name" : "val2", "type" : [ "null", "double" ], "default": "null"}
]
}
} ]
}
Then I generated classes using gogen-avro:
gogen-avro --package=avro ./internal/avro ./internal/avro/data-item-two.avsc
When I send a message without some fields that are optional my consumer with the same schema fails to parse it. For example (I see this message in kcat):
{
"basics": {
"id": "2022-04-12T06:12:31Z",
"value": {"string": "50"},
"alwaysNil": null
},
"extra": {
"Extra": {
"foo": {"string": "Foo"},
"boo": null
}
},
"payload": {
"val1": {"double": 0.5},
"val2": null
}
}
When consumer gets the message I try do deserialise it using DeserializeDataItem() I get an error EOF. What is also suspicious is that when it compiles decoder basing on schema compiler.CompileSchemaBytes returns deserialiser with errors Unsupported type for union. But returns nil.
Is there any ideas what's wrong with this schema?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
