Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mberndt123 opened this issue Jan 24, 2024 · 0 comments
Open

Comments

@mberndt123
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant