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

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

Closed
atnoya opened this issue Sep 29, 2021 · 1 comment · Fixed by #812
Closed

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

atnoya opened this issue Sep 29, 2021 · 1 comment · Fixed by #812

Comments

@atnoya
Copy link
Contributor

atnoya commented Sep 29, 2021

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.)
@atnoya
Copy link
Contributor Author

atnoya commented Sep 29, 2021

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)

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

Successfully merging a pull request may close this issue.

1 participant