sksamuel / avro4s

Avro schema generation and serialization / deserialization for Scala
Apache License 2.0
719 stars 238 forks source link

`derives` clause for generic types leads to invalid `SchemaFor` instances #818

Open mberndt123 opened 10 months ago

mberndt123 commented 10 months ago

Here's a trivial example to demonstrate the problem:

import com.sksamuel.avro4s.*
case class Foo[A](a: List[A]) derives SchemaFor
case class Bar(f1: Foo[Int], f2: Foo[String]) derives SchemaFor

@main def main = println(AvroSchema[Bar])

This crashes with the following error:

[error] org.apache.avro.SchemaParseException: Can't redefine: Foo__A
[error]         at org.apache.avro.Schema$Names.put(Schema.java:1604)

The problem is that when a SchemaFor instance is derived for a generic type, it doesn't know about the concrete type that is going to be substituted for A, and so it just inserts A. So you get the name Foo__A twice (because there are two Foo fields in Bar, and Avro doesn't like that.

This is not an easy problem to solve because to insert the correct Avro typename into the schema for Foo, we would need access to the SchemaFor[A] instance for A. The derives clause actually gives that to us, but Magnolia doesn't, it only gives you the SchemaFor[List[A]] instance.

Removing the derives clause for Foo fixes the problem, but obviously that is also not ideal.