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 · 1 comment

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

@sinha-abhi
Copy link

sinha-abhi commented Aug 22, 2024

unfortunately, this is actually not limited to higher kinded types. here's an even smaller example:

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

object EncodeJsonSpec extends ZIOSpecDefault {
  case class A()
  case class B(a: A)

  implicit val bEncoder: JsonEncoder[B] = DeriveJsonEncoder.gen

  implicit val aEncoder: JsonEncoder[A] = DeriveJsonEncoder.gen

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

causes a null pointer exception inside the macro:

    Exception in thread "zio-fiber-77" java.lang.NullPointerException: null
    	at zio.json.DeriveJsonEncoder$$anon$6.unsafeEncode(macros.scala:560)
    	at zio.json.JsonEncoder.encodeJson(JsonEncoder.scala:75)
    	at zio.json.JsonEncoder.encodeJson$(JsonEncoder.scala:73)
    	at zio.json.DeriveJsonEncoder$$anon$6.encodeJson(macros.scala:531)
    	at zio.json.package$EncoderOps$.toJson$extension(package.scala:22)

versions:

  • scala 2.12.13
  • zio 2.0.15
  • zio-json 0.6.2

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

2 participants