playframework / playframework/play-json

updateNullable

Open
#414 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

It would be great if JsPath supported the ability to update a path if it exists. Currently, if you try to define an update on a path that doesn't exist, you'll get an error. Here's a helper that does this:

private implicit class PathUpdateNullable(path: JsPath) {
    def updateNullable[A <: JsValue](reads: Reads[A]) =
      Reads[JsObject] {
        case o: JsObject =>
          path.asSingleJson(o) match {
            case JsDefined(jsv) =>
              val updated = reads.reads(jsv).repath(path)
              updated.map { updatedJsv =>
                o.deepMerge(JsPath.createObj(path -> updatedJsv))
              }
            case _ => JsSuccess(o)
          }
        case _ =>
          JsError(JsPath, JsonValidationError("error.expected.jsobject"))
      }
  }

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 with the JsPath update behavior and compare it with the updateNullable helper shown in the issue. Trace how a missing path is handled and identify the existing JsPath entry points involved. Done means updates succeed when the path exists and leave the object unchanged when it does not.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
backend-api-design
Issue type
Feature
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.