play-json icon indicating copy to clipboard operation
play-json copied to clipboard

JsPath.prune should not repath on success

Open steinybot opened this issue 5 years ago • 4 comments

Play JSON Version (2.5.x / etc)

2.6.9

API (Scala / Java / Neither / Both)

Scala

Operating System (Ubuntu 15.10 / MacOS 10.10 / Windows 10)

macOS 10.13.6

JDK (Oracle 1.8.0_72, OpenJDK 1.8.x, Azul Zing)

java version "1.8.0_172" Java(TM) SE Runtime Environment (build 1.8.0_172-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)

Library Dependencies

N/A

Expected Behavior

scala> :paste
// Entering paste mode (ctrl-D to finish)

import play.api.libs.json._

val js = Json.parse("""{
                       |  "a": 1,
                       |  "b": 2,
                       |  "c": 3
                       |}""".stripMargin)
val removeAAndB = (__ \ 'a).json.prune andThen (__ \ 'b).json.prune
val removeAAndBPickD = removeAAndB andThen (__ \ 'd).json.pick

// Exiting paste mode, now interpreting.

import play.api.libs.json._
js: play.api.libs.json.JsValue = {"a":1,"b":2,"c":3}
removeAAndB: play.api.libs.json.Reads[play.api.libs.json.JsObject] = play.api.libs.json.Reads$$anon$8@29d080fc
removeAAndBPickD: play.api.libs.json.Reads[play.api.libs.json.JsValue] = play.api.libs.json.Reads$$anon$8@1ed855bd

scala> removeAAndB.reads(js)
res0: play.api.libs.json.JsResult[play.api.libs.json.JsObject] = JsSuccess({"c":3},/a/b)

scala> removeAAndBPickD.reads(js)
res1: play.api.libs.json.JsResult[play.api.libs.json.JsValue] = JsError(List((/a/b/d,List(JsonValidationError(List(error.path.missing),WrappedArray())))))

Should evaluate to:

JsError(List((/d,List(JsonValidationError(List(error.path.missing),WrappedArray())))))

Actual Behavior

It evaluates to:

JsError(List((/a/b/d,List(JsonValidationError(List(error.path.missing),WrappedArray())))))

The issue is that prune is effectively repathing the result and so it does not compose properly with subsequent Reads. It makes sense to use the path in the error cases but not success.

Reproducible Test Case

See above.

steinybot avatar Jul 31 '18 00:07 steinybot

A similar combination can be achieved with update and copyFrom although the solution is less obvious:

scala> :paste
// Entering paste mode (ctrl-D to finish)

val duplicateAAndB = __.json.update((__ \ 'aa).json.copyFrom((__ \ 'a).json.pick)) andThen __.json.update((__ \ 'bb).json.copyFrom((__ \ 'b).json.pick))
val duplicateAAndBPickD = duplicateAAndB andThen (__ \ 'd).json.pick

// Exiting paste mode, now interpreting.

duplicateAAndB: play.api.libs.json.Reads[play.api.libs.json.JsObject] = play.api.libs.json.Reads$$anon$8@3f28e6d4
duplicateAAndBPickD: play.api.libs.json.Reads[play.api.libs.json.JsValue] = play.api.libs.json.Reads$$anon$8@5fa6ae08

scala> duplicateAAndB.reads(js)
res3: play.api.libs.json.JsResult[play.api.libs.json.JsObject] = JsSuccess({"a":1,"b":2,"c":3,"aa":1,"bb":2},/a/b)

scala> duplicateAAndBPickD.reads(js)
res4: play.api.libs.json.JsResult[play.api.libs.json.JsValue] = JsError(List((/a/b/d,List(JsonValidationError(List(error.path.missing),WrappedArray())))))

steinybot avatar Jul 31 '18 00:07 steinybot

On second thought I think that the real issue is that andThen should not be concatenating the paths. The only time that concatenation is correct is if the first Reads has picked a value from a path and not combined that with anything else (e.g. only trivial pick or copyFrom). Assuming that this works in other cases seems like the greater of two evils.

steinybot avatar Jul 31 '18 01:07 steinybot

Better to first discuss it, until then this one can hardly be actionable. Best regards.

cchantep avatar Aug 04 '18 10:08 cchantep

Good idea. https://discuss.lightbend.com/t/should-reads-compose-and-andthen-repath/1829

steinybot avatar Aug 09 '18 10:08 steinybot