zio / zio-schema

Compositional, type-safe schema definitions, which enable auto-derivation of codecs and migrations.
https://zio.dev/zio-schema
Apache License 2.0
142 stars 162 forks source link

Scala 3 enum issue introduced in v0.4.14 #606

Closed m-kalai closed 1 year ago

m-kalai commented 1 year ago

We tried to upgrade to 0.4.14 and noticed our tests failing in places where enums were supposed to be decoded. Happens specifically for enums which are "not simple" i.e. they have parameters.

Example:

  enum Colour(val rgb: Int) {
    case Red extends Colour(0xff0000)
    case Green extends Colour(0x00ff00)
    case Blue extends Colour(0x0000ff)
  }

  case class ColourContainer(colour: Colour)

  object ColourContainer {
    given Schema[ColourContainer] = DeriveSchema.gen[ColourContainer]
  }

  val codec = summon[BinaryCodec[ColourContainer]]

  val cc = ColourContainer(Colour.Red)

  val encoded = codec.encode(cc)
  println(encoded.asString)

  val decoded = codec.decode(encoded)
  println(decoded)

v0.4.13 output

{"colour":"Red"}
Right(ColourContainer(Red))

v0.4.14 output

{"colour":{"Red":{}}}
Left(zio.schema.codec.DecodeError$ReadError: .colour(expected '"' got '{'))