Hi there,
I connect to a Confluent schema registry to generate a Java avro model
object which I use to consume messages from a Kafka topic.
Following exception raise when trying to deserialize an avro message with a
schema containing a record type named `Builder`.
I'm experiencing the issue on a production project, for which I don't have
hand on the consumed topic, neither the schema.
So I can't ask for the "producer" to rename "Builder" for an other name.
I was able to reproduce the issue in a minimal project, in which I had 2
versions of producer, consumer, and schema.
One under the package 'org.example.not_working', with the schema
containing a record type named 'Builder'.
And an other one under the 'org.example.working', with same schema excepted
that I renamed 'Builder' to 'Details'.
The second version is working, proving that the issue comes from the use of
this specific name.
"Builder" is somehow a reserved word, used to provide Builders, as innner
class, for each generated class by the plugin.
Note that the plugin generates this 2 classes :
avro.not_working.AvroMessage
avro.not_working.Builder$
Here we can see that Builder is suffixed by the "$", which might be a way
to avoir reussing the 'Builder' name to name the generated classe.
But at the en of the day it doesn't work.
The exception :
````
Exception in thread "main"
org.apache.kafka.common.errors.SerializationException: Error deserializing
key/value for partition philavrotest2-0 at offset 1. If needed, please seek
past the record to continue consumption.
Caused by: org.apache.kafka.common.errors.SerializationException: Error
deserializing Avro message for id 101790
Caused by: java.lang.ClassCastException: class
org.apache.avro.generic.GenericData$Record cannot be cast to class
org.example.avrotest.avro.not_working.Builder$
(org.apache.avro.generic.GenericData$Record and
org.example.avrotest.avro.not_working.Builder$ are in unnamed module of
loader 'app')
````
Here is some context :
runtime :
java version : openjdk 21.0.4 2024-07-16
maven version : 3.8.6
dependencies :
io.confluent, kafka-streams-avro-serde, 6.1.1
org.apache.kafka, kafka-streams, 2.6.0
org.apache.kafka, kafka-clients, 2.6.0
maven plugins :
io.confluent, kafka-schema-registry-maven-plugin, 7.5.1
org.apache.avro, avro-maven-plugin, 1.10.2
And the schema
{
"type": "record",
"name": "AvroMessage",
"namespace": "org.example.avrotest.avro.not_working",
"fields": [
{
"name": "builder",
"type": [
{
"type": "record",
"name": "Builder",
"fields": [
{
"name": "infos",
"type": "string"
}
]
},
"null"
]
},
{
"name": "id",
"type": "int"
},
{
"name": "name",
"type": "string"
}
]
}
--
Philippe
VASCONCELOS
Staff Engineer
[email protected]
www.kpler.com
<https://cloud.letsignit.com/collect/bc/618bf9839d7723ee68af7902?p=nIfz9M4mVYcyv-t69SYVHKBVh1Mp-ijxwhfDdRv-B5z6VQfrZI6ESrPqc3gkwHkLp6K0p1dMXsvclTDlD3uuguU_z5ON46A5aOWtw6zRNVw8kv4KzOXa0xhH6XySfda1>
<https://cloud.letsignit.com/collect/bc/618bf9839d7723ee68af7902?p=nIfz9M4mVYcyv-t69SYVHKBVh1Mp-ijxwhfDdRv-B5z6VQfrZI6ESrPqc3gkwHkLp6K0p1dMXsvclTDlD3uuguU_z5ON46A5aOWtw6zRNVyvy0pX9yNRttpPsNkQHYPOJfbbtQ-vNrqGsmQXWazS-Q==>
[image: Twitter]
<https://cloud.letsignit.com/collect/bc/618bf9839d7723ee68af7902?p=nIfz9M4mVYcyv-t69SYVHKBVh1Mp-ijxwhfDdRv-B5z6VQfrZI6ESrPqc3gkwHkLp6K0p1dMXsvclTDlD3uugkeJLeCbA6VjiBw4QljAIyOlTmZ0DDmHfS-JREOXbBnQFBNOY9SMkZqJWdDoUo-I_A==>
<https://cloud.letsignit.com/collect/bc/618bf9839d7723ee68af7902?p=nIfz9M4mVYcyv-t69SYVHKBVh1Mp-ijxwhfDdRv-B5z6VQfrZI6ESrPqc3gkwHkLp6K0p1dMXsvclTDlD3uuguU_z5ON46A5aOWtw6zRNVz0Aaiq9696kd3Whqci1jTX3PQpjzSkPsO4FvHD6OlAVq8U-TmlvnN27_c7uQRHayw=>