-
Notifications
You must be signed in to change notification settings - Fork 239
Open
Labels
Description
Consider this example:
//> using scala 3.3.5
//> using dep com.sksamuel.avro4s::avro4s-core:5.0.14
import com.sksamuel.avro4s.*
import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
import scala.util.{Using, Try}
sealed trait ExampleWithSealed derives Decoder, Encoder, SchemaFor
object ExampleWithSealed {
case class Foo(value: String) extends ExampleWithSealed
case object Bar extends ExampleWithSealed
}
enum ExampleWithEnum derives Decoder, Encoder, SchemaFor {
case Foo(value: String)
case Bar
}
import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
def roundTrip[A: Decoder: Encoder: SchemaFor](value: A): Try[A] = {
for {
encoded <- Using(new ByteArrayOutputStream()) { baos =>
val aos = AvroOutputStream.json[A].to(baos).build()
aos.write(value)
aos.close()
aos.flush()
baos.toByteArray
}
decoded <- Using(new ByteArrayInputStream(encoded)) { bais =>
AvroInputStream
.json[A]
.from(bais)
.build(SchemaFor[A].schema)
.asInstanceOf[AvroJsonInputStream[A]]
.singleEntity
}.flatten
} yield decoded
}
println(roundTrip[ExampleWithSealed](ExampleWithSealed.Foo("example")))
println(roundTrip[ExampleWithEnum](ExampleWithEnum.Foo("example")))I'd expect both println to show exactly the same behavior. Meanwhile, it produces:
Success(Foo(example))
Failure(java.lang.IllegalArgumentException: requirement failed)
Changing Scala from 3.3.5 to 3.6.3 doesn't change the result.
Removing derives and using automatic derivation, doesn't change it either.
Reactions are currently unavailable