Hi all,
I am trying to understand the schema resolution rules:
https://avro.apache.org/docs/1.7.7/spec.html#Schema+Resolution.
Especially the following rule for union types:
*if both are unions:*
The first schema in the reader's union that matches the selected writer's
union schema is recursively resolved against it. if none match, an error is
signaled.
Let's say I have the following schema as my writer schema:
{
"schema": "{\"type\":\"record\",\"name\":\"RtbEvent\",\"namespace\":\"
com.gumgum.avro.rtb\",\"doc\":\"Schema defining an RTB
event.\",\"fields\":[{\"name\":\"eventMetadata\",\"type\":[\
"null\",{\"type\":\"record\",\"name\":\"EventMetadata\",\"doc\":\"Event
metadata.\",\"fields\":[{\"name\":\"metroCode\",\"type\":
[\"null\",\"int\"],\"doc\":\"Visitor's Metro code
location.\"}]}],\"doc\":\"Event
metadata.\"}]}"
}
And my reader schema is:
{
"schema": "{\"type\":\"record\",\"name\":\"RtbEvent\",\"namespace\":\"
com.gumgum.avro.rtb\",\"doc\":\"Schema defining an RTB
event.\",\"fields\":[{\"name\":\"eventMetadata\",\"type\":[\
"null\",{\"type\":\"record\",\"name\":\"EventMetadata\",\"doc\":\"Event
metadata.\",\"fields\":[{\"name\":\"metroCode\",\"type\":
[\"null\",\"string\"],\"doc\":\"Visitor's Metro code
location.\"}]}],\"doc\":\"Event metadata.\"}]}"
}
The only difference between these two schemas are on the metroCode field
where the writer type is the following union: [\"null\",\"int\"] and the
reader type is the following union: [\"null\",\"string\"].
As far as I understand the union rule, these two schemas match because null
match with null. However when trying to deserialize an Avro event to a Java
generated class built with the reader schema it will fail if metro code is
a string. If that's the case why would these two schemas be considered to
be matching ?
Here is the link of one my post in the confluent platform user group:
https://groups.google.com/forum/#!msg/confluent-platform/A05NZwfJa6Q/J9nkUsijDgAJ
We are trying to understand how this should be handled in terms of schema
compatibility when using a schema registry.
Thanks!
--
*Guillaume Torche*
*Big Data Engineer - GumGum - Online advertising*
*Professional email:* *[email protected] <[email protected]>*
*Personal email: [email protected] <[email protected]>*
*310 254 8151*