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

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

Open
wi101 opened this issue Jun 20, 2024 · 6 comments

Comments

@wi101
Copy link

wi101 commented Jun 20, 2024

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
}
@jdegoes
Copy link
Member

jdegoes commented Jun 28, 2024

/bounty $200

Copy link

algora-pbc bot commented Jun 28, 2024

💎 $200 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #1128 with your implementation plan
  2. Submit work: Create a pull request including /claim #1128 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio-json!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🔴 @Anshgrover23 Jun 28, 2024, 4:11:00 PM WIP
🔴 @omarnagy91 Jul 9, 2024, 4:58:42 AM WIP

@Anshgrover23
Copy link

Anshgrover23 commented Jun 28, 2024

/attempt #1128

@987Nabil
Copy link
Contributor

987Nabil commented Jul 8, 2024

@wi101 I am unable to reproduce What Scala version are you using? Are the definitions all in the same file? Are you sure you have no conflicting versions in your project?

@omarnagy91
Copy link

/attempt #1128

@wi101
Copy link
Author

wi101 commented Jul 10, 2024

@987Nabil

This is the full example (the same file)

import zio.json.{DeriveJsonDecoder, JsonDecoder}

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
}

scalaVersion: 2.13.14
zioJsonVersion: 0.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants