zio-json icon indicating copy to clipboard operation
zio-json copied to clipboard

Unable to locate the JsonDecoder for a type constructor containing a nested type.

Open wi101 opened this issue 8 months ago • 3 comments

Hello,

This issue appeared when updating to zio-json: 0.7.0. Here is an example:


final case class Id[A](value: A)
object Id {
  implicit def decoder[A: JsonDecoder]: JsonDecoder[Id[A]] = DeriveJsonDecoder.gen[Id[A]]
}

final case class Person(name: String) extends AnyVal
object Person {
  implicit val decoder: JsonDecoder[Person] = DeriveJsonDecoder.gen
}


final case class People(ids: Id[Person])
object People {
  implicit val decoder: JsonDecoder[People] = DeriveJsonDecoder.gen
}

Error:

magnolia: could not find JsonDecoder.Typeclass for type Id[Person]
[error]     in parameter 'ids' of product type People
[error]   implicit val decoder: JsonDecoder[People] = DeriveJsonDecoder.gen

People.decoder cannot find the decoder for Id[Person]. There is an issue on finding the Id.decoder ..

when we manually add the decoder to Id companion object:

object Id {
    implicit val decoderIds: JsonDecoder[Id[Person]] = DeriveJsonDecoder.gen
}

The same error appears. But it works only when we add that decoder to People companion.

object People {
  implicit val decoderIds: JsonDecoder[Id[Person]] = Id.decoder
  implicit val decoder: JsonDecoder[People] = DeriveJsonDecoder.gen
}

wi101 avatar Jun 20 '24 08:06 wi101