playframework / playframework/play-json

JsPath.prune should not repath on success

Open
#171 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
375
Forks
140
Avg merge
1d 5h
Merged PRs (30d)
28

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the JsPath.prune implementation and reproduce the composed Reads behavior shown in the issue. Add a regression test for pruning a and b before picking a missing d, confirming the error path is /d while prune errors retain their own paths; run the relevant Play JSON tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.