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

Wrapped Option type results in Json decoding failure missing required field #176

Open
kamilkloch opened this issue Oct 8, 2024 · 1 comment

Comments

@kamilkloch
Copy link

kamilkloch commented Oct 8, 2024

(Scala 2)

type WsTag = WsTag.Type

object WsTag extends NewtypeWrapped[Option[Long]] with DerivedCirceCodec

case class A(wsTag: WsTag, i: Int)

implicit val decoder: Decoder[A] = io.circe.generic.semiauto.deriveDecoder
println(io.circe.parser.parse("""{"i" : 1}""").value.as[A])

results in

Left(DecodingFailure at .wsTag: Missing required field)

Replacing WsTag with the underlying type Option[Long] in A gets rid of the error:

case class A(wsTag: Option[Long], i: Int)

println(io.circe.parser.parse("""{"i" : 1}""").value.as[A])

> Right(A(None,1))
@kamilkloch
Copy link
Author

Update: defining Decoder manually as follows:

implicit val decoder: Decoder[WsTag] = implicitly[Decoder[Option[Long]]].map(unsafeCoerce)

solves the issue. It looks that the special handling of Option by the decoder may be lost in

trait DerivedCirceDecoder {
  implicit def jsonDecoder[T, S](implicit 
    builder: HasBuilder.Aux[T, S],
    dec: Decoder[S],
  ): Decoder[T] = {
    jsonDecode(_)
  }
}

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