circe icon indicating copy to clipboard operation
circe copied to clipboard

Decoders derived with Configuration#strictDecoding fail fast at `decodeAccumulating`

Open bdmendes opened this issue 7 months ago • 0 comments

I'm expecting to get a comprehensive list of failures, as was the case in Scala 2 with https://github.com/circe/circe-generic-extras. Reproducible example with scala-cli:

//> using jvm 21.0.6
//> using scala 3.6.4
//> using dep io.circe::circe-core:0.14.13

import io.circe.*
import io.circe.derivation.*

case class Foo(a: String, b: Int, c: Int)

object Main {
  def main(args: Array[String]): Unit = {

    val fooDecoder: Decoder[Foo] = {
      given Configuration = Configuration.default.withStrictDecoding
      Decoder.derivedConfigured
    }

    val json = Json.obj(
      "a" -> Json.fromString("hello"),
      "b" -> Json.fromInt(42),
      "c" -> Json.fromString("100"), // This should cause a decoding error because c is expected to be an Int
      "d" -> Json.fromString("extra field") // This field is not in the case class Foo
    )

    val decoded = fooDecoder.decodeJson(json)
    val accDecoded = fooDecoder.decodeAccumulating(json.hcursor)

    println(decoded) // Left(DecodingFailure at : Strict decoding Foo - unexpected fields: d; valid fields: a, b, c.)
                     // ok

    println(accDecoded) // Invalid(NonEmptyList(DecodingFailure at : Strict decoding Foo - unexpected field: d; valid fields: a, b, c.))
                        // Expected to also get a DecodingFailure at .c
  }
}

bdmendes avatar Jun 07 '25 09:06 bdmendes