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

Automatic derivation of codecs fails for nested HKT if implicits are defined in wrong order #1015

Open
p3et opened this issue Sep 13, 2023 · 0 comments

Comments

@p3et
Copy link

p3et commented Sep 13, 2023

Hello. I'm pretty new to Scala and ZIO so I hope my report is qualified. Here is a minimal example to reproduce a suspected bug, that I coincidentally discovered in a more complex scenario:

import zio.ZIO
import zio.json.{DeriveJsonEncoder, EncoderOps, JsonEncoder}
import zio.test._

object EncodeJsonSpec extends ZIOSpecDefault {
  case class A()
  case class B(option: Option[A])

  implicit val aEncoder: JsonEncoder[A] = DeriveJsonEncoder.gen
  implicit val bEncoder: JsonEncoder[B] = DeriveJsonEncoder.gen
  implicit val aOptionEncoder: JsonEncoder[Option[A]] = DeriveJsonEncoder.gen

  def spec =
    suite("JsonSpec")(
      test("Encode JSON") {
        for {
          json <- ZIO.attempt(B(option = Some(A())).toJson)
        } yield assertTrue(json != null)
      }
    )
}

The test can be easily fixed by defining the implicits in the order aEncoder, aOptionEncoder, bEncoder. However, the problem seems to be specifically related to HKT because using aOptionEncoder, bEncoder, aEncoder won't cause a problem although A is nested in B.

I would expect that, if the order of definitions matters, there will be a compile or build error. However, the error occurs only at runtime.

Scala 2.13.11
zio 2.0.16
zio-json 0.6.1

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