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

Avro4s fails to decode enums #867

Open
MateuszKubuszok opened this issue Feb 18, 2025 · 0 comments
Open

Avro4s fails to decode enums #867

MateuszKubuszok opened this issue Feb 18, 2025 · 0 comments

Comments

@MateuszKubuszok
Copy link
Contributor

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.

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