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

fix(json): Fix LocationInfo json decoding #1015

Merged
merged 3 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions core/src/main/scala-2/caliban/interop/play/play.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ object json {
private final case class ErrorDTO(
message: String,
extensions: Option[ResponseValue],
locations: Option[LocationInfo],
locations: Option[List[LocationInfo]],
path: Option[JsArray]
)

Expand All @@ -141,16 +141,16 @@ object json {

val errorValueWrites: Writes[CalibanError] = errorDTOWrites.contramap[CalibanError] {
case CalibanError.ParsingError(msg, locationInfo, _, extensions) =>
ErrorDTO(s"Parsing Error: $msg", extensions, locationInfo, None)
ErrorDTO(s"Parsing Error: $msg", extensions, locationInfo.map(List(_)), None)

case CalibanError.ValidationError(msg, _, locationInfo, extensions) =>
ErrorDTO(msg, extensions, locationInfo, None)
ErrorDTO(msg, extensions, locationInfo.map(List(_)), None)

case CalibanError.ExecutionError(msg, path, locationInfo, _, extensions) =>
ErrorDTO(
msg,
extensions,
locationInfo,
locationInfo.map(List(_)),
Some(path).collect {
case p if p.nonEmpty =>
JsArray(p.map {
Expand All @@ -177,7 +177,7 @@ object json {
case JsNumber(bd) => Right(bd.toInt)
case _ => throw new Exception("invalid json")
},
locationInfo = e.locations
locationInfo = e.locations.flatMap(_.lift(0))
)
)
}
Expand Down
29 changes: 25 additions & 4 deletions core/src/main/scala-2/caliban/interop/zio/zio.scala
Original file line number Diff line number Diff line change
Expand Up @@ -413,17 +413,38 @@ private[caliban] object ErrorZioJson {
private case class ErrorDTO(
message: String,
extensions: Option[ResponseValue],
locations: Option[LocationInfo],
locations: Option[List[LocationInfo]],
path: Option[List[Either[String, Int]]]
)
private object ErrorDTO {
import ValueZIOJson.responseValueDecoder
import ValueZIOJson.{ responseValueDecoder, Num, Str }
import zio.json.internal.{ RetractReader }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that still needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, it's not!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


implicit val locationInfoDecoder: JsonDecoder[LocationInfo] = DeriveJsonDecoder.gen[LocationInfo]
implicit val decoder: JsonDecoder[ErrorDTO] = DeriveJsonDecoder.gen[ErrorDTO]
implicit val pathDecoder: JsonDecoder[Either[String, Int]] =
ghostdogpr marked this conversation as resolved.
Show resolved Hide resolved
(trace: List[JsonDecoder.JsonError], in: RetractReader) => {
val c = in.nextNonWhitespace()
in.retract()
(c: @switch) match {
case '"' => Left(JsonDecoder.string.unsafeDecode(trace, in))
case '-' | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' =>
Right(JsonDecoder.int.unsafeDecode(trace, in))
case c =>
throw JsonDecoder.UnsafeJson(JsonDecoder.JsonError.Message(s"unexpected '$c'") :: trace)
}
}

implicit val decoder: JsonDecoder[ErrorDTO] = DeriveJsonDecoder.gen[ErrorDTO]
}

val errorValueDecoder: JsonDecoder[CalibanError] =
JsonDecoder[ErrorDTO].map(e => CalibanError.ExecutionError(e.message, e.path.getOrElse(List()), e.locations))
JsonDecoder[ErrorDTO].map(e =>
CalibanError.ExecutionError(
e.message,
e.path.getOrElse(List()),
e.locations.flatMap(locations => locations.lift(0))
)
)
}

private[caliban] object GraphQLResponseZioJson {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/caliban/interop/circe/circe.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ object json {
for {
message <- cursor.downField("message").as[String]
path <- cursor.downField("path").as[Option[List[Either[String, Int]]]]
locations <- cursor.downField("locations").as[Option[LocationInfo]]
locations <- cursor.downField("locations").downArray.as[Option[LocationInfo]]
} yield CalibanError.ExecutionError(
message,
path.getOrElse(Nil),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package caliban.interop.play
import caliban.CalibanError.ExecutionError
import caliban.GraphQLResponse
import caliban.ResponseValue.ObjectValue
import caliban.parsing.adt.LocationInfo
import caliban.Value.StringValue
import zio.test.Assertion._
import zio.test._
Expand Down Expand Up @@ -68,14 +69,17 @@ object GraphQLResponsePlaySpec extends DefaultRunnableSpec {
)
},
test("reads a graphql response [play]") {
val req = """{"data":{"value": 42},"errors":[{"message":"boom"}]}"""
val req =
"""{"data":{"value": 42},"errors":[{"message":"boom", "path": ["step", 0], "locations": [{"column": 1, "line": 2}]}]}"""

assert(Json.parse(req).validate[GraphQLResponse[CalibanError]].asEither)(
isRight(
equalTo(
GraphQLResponse(
data = ObjectValue(List("value" -> Value.IntValue("42"))),
errors = List(ExecutionError("boom"))
errors = List(
ExecutionError("boom", path = List(Left("step"), Right(0)), locationInfo = Some(LocationInfo(1, 2)))
)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package caliban.interop.zio
import caliban.GraphQLResponse
import zio.json._
import zio.test._
import caliban.parsing.adt.LocationInfo
import Assertion._
import caliban.CalibanError
import caliban.CalibanError.ExecutionError
Expand Down Expand Up @@ -50,14 +51,17 @@ object GraphQLResponseZIOSpec extends DefaultRunnableSpec {
assert(response.toJson)(equalTo("""{"data":"data"}"""))
},
test("can be parsed from JSON [zio]") {
val req = """{"data":{"value": 42},"errors":[{"message":"boom"}]}"""
val req =
"""{"data":{"value": 42},"errors":[{"message":"boom", "path": ["step", 0], "locations": [{"column": 1, "line": 2}]}]}"""

assert(req.fromJson[GraphQLResponse[CalibanError]])(
isRight(
equalTo(
GraphQLResponse(
data = ObjectValue(List("value" -> IntValue("42"))),
errors = List(ExecutionError("boom"))
errors = List(
ExecutionError("boom", path = List(Left("step"), Right(0)), locationInfo = Some(LocationInfo(1, 2)))
)
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import caliban.CalibanError
import caliban.CalibanError.ExecutionError
import caliban.GraphQLResponse
import caliban.ResponseValue.ObjectValue
import caliban.parsing.adt.LocationInfo
import caliban.Value.IntValue
import caliban.Value.StringValue
import caliban.Value.FloatValue
Expand Down Expand Up @@ -70,14 +71,17 @@ object GraphQLResponseCirceSpec extends DefaultRunnableSpec {
)
},
test("can be parsed from JSON [circe]") {
val req = """{"data":{"value": 42},"errors":[{"message":"boom"}]}"""
val req =
"""{"data":{"value": 42},"errors":[{"message":"boom", "path": ["step", 0], "locations": [{"column": 1, "line": 2}]}]}"""

assert(decode[GraphQLResponse[CalibanError]](req))(
equalTo(
Right(
GraphQLResponse(
data = ObjectValue(List("value" -> IntValue(BigInt(42)))),
errors = List(ExecutionError("boom"))
errors = List(
ExecutionError("boom", path = List(Left("step"), Right(0)), locationInfo = Some(LocationInfo(1, 2)))
)
)
)
)
Expand Down