Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Misleading decoder errors when decoding non-objects on Scala 2 derived codecs #2297

@JD557

Description

@JD557

Using a derived codec on Scala 2, if one tries to decode an object from a non-object (e.g. a JSON string), the error message points to a "Missing required field", instead of pointing to the wrong type.

This is particularly annoying on objects with optional fields, as it wrongly says that an optional field as required :/

import io.circe._
import io.circe.generic.semiauto._

case class Foo(optionalField: Option[String] = None)
object Foo {
  val codec: Codec[Foo] = deriveCodec
}

// Decodes to Foo(Node), as expected
Foo.codec.decodeJson(Json.obj())

// Scala 2: DecodingFailure at .optionalField: Missing required field
// Scala 3: DecodingFailure at : Got value '""' with wrong type, expecting object
Foo.codec.decodeJson(Json.fromString(""))

Scastie link: https://scastie.scala-lang.org/nghQc65yQTGyvtIVWxIbcQ

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions