zio/zio-json

Decoding from ast.Json.Obj to A loses the property names on errors

atnoya opened this issue · 1 comments

When decoding a case class from a Json AST, if there are errors for any of the properties the error message does not contain the failing property names.

See example below.

scala> case class Test(a: NonEmptyString, b: String, c: Int)
class Test

scala> import zio.json.compat.refined._
            import zio.json._
            import eu.timepit.refined.types.all._
import zio.json.compat.refined._
import zio.json._
import eu.timepit.refined.types.all._

scala> implicit val dec: JsonDecoder[Test] = DeriveJsonDecoder.gen
val dec: zio.json.JsonDecoder[Test] = zio.json.DeriveJsonDecoder$$anon$2@7d8a9b95

//From String to A, rightly signalled that the error is in the property "a"
scala> """{"a": "", "b": "", "c": 1}""".fromJson[Test] 
val res2: Either[String,Test] = Left(.a(Predicate isEmpty() did not fail.))

//From String to Json.Obj
scala> """{"a": "", "b": "", "c": 1}""".fromJson[ast.Json.Obj]
val res3: Either[String,zio.json.ast.Json.Obj] = Right({"a":"","b":"","c":1})

//From Json.Obj to A, missing the property name "a" in the error message
scala> res3.flatMap(_.as[Test])
val res5: scala.util.Either[String,Test] = Left(Predicate isEmpty() did not fail.)

Tested in the last develop branch, and the problem is the same:

scala> implicit val dec: JsonDecoder[Test] = DeriveJsonDecoder.gen[Test]
val dec: zio.json.JsonDecoder[Test] = zio.json.DeriveJsonDecoder$$anon$3@139c649

scala> """{"a": 1, "b": ""}""".fromJson[Test]                               
val res0: Either[String, Test] = Left(.a(expected '"' got '1'))

scala> """{"a": 1, "b": ""}""".fromJson[Json.Obj]
val res1: Either[String, zio.json.ast.Json.Obj] = Right({"a":1,"b":""})

scala> res1.flatMap(_.as[Test])                                                                                                       
val res2: Either[String, Test] = Left(Not a string value, Unexpected end of input)