aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKseniya Tomskikh <ktomskih@datamonsters.co>2018-04-12 13:59:43 +0700
committerGitHub <noreply@github.com>2018-04-12 13:59:43 +0700
commit4a769764836a14ca3afa4d6a6f7f09943e290ec2 (patch)
tree113cf2991823c93b784cc4fa72d8f116b2e13be5
parent4cfa6a09c6f6692f5513ff6e31f261a020d07068 (diff)
downloaddriver-core-4a769764836a14ca3afa4d6a6f7f09943e290ec2.tar.gz
driver-core-4a769764836a14ca3afa4d6a6f7f09943e290ec2.tar.bz2
driver-core-4a769764836a14ca3afa4d6a6f7f09943e290ec2.zip
Fixed merge JsValues when field is absent in old value (#153)v1.8.21
-rw-r--r--src/main/scala/xyz/driver/core/rest/PatchDirectives.scala8
-rw-r--r--src/test/scala/xyz/driver/core/rest/PatchDirectivesTest.scala9
2 files changed, 13 insertions, 4 deletions
diff --git a/src/main/scala/xyz/driver/core/rest/PatchDirectives.scala b/src/main/scala/xyz/driver/core/rest/PatchDirectives.scala
index 256358c..f33bf9d 100644
--- a/src/main/scala/xyz/driver/core/rest/PatchDirectives.scala
+++ b/src/main/scala/xyz/driver/core/rest/PatchDirectives.scala
@@ -53,10 +53,10 @@ trait PatchDirectives extends Directives with SprayJsonSupport {
}
protected def mergeObjects(oldObj: JsObject, newObj: JsObject, maxLevels: Option[Int] = None): JsObject = {
- JsObject(oldObj.fields.map({
- case (key, oldValue) =>
- val newValue = newObj.fields.get(key).fold(oldValue)(mergeJsValues(oldValue, _, maxLevels.map(_ - 1)))
- key -> newValue
+ JsObject((oldObj.fields.keys ++ newObj.fields.keys).map({ key =>
+ val oldValue = oldObj.fields.getOrElse(key, JsNull)
+ val newValue = newObj.fields.get(key).fold(oldValue)(mergeJsValues(oldValue, _, maxLevels.map(_ - 1)))
+ key -> newValue
})(collection.breakOut): _*)
}
diff --git a/src/test/scala/xyz/driver/core/rest/PatchDirectivesTest.scala b/src/test/scala/xyz/driver/core/rest/PatchDirectivesTest.scala
index 6a6b035..987717d 100644
--- a/src/test/scala/xyz/driver/core/rest/PatchDirectivesTest.scala
+++ b/src/test/scala/xyz/driver/core/rest/PatchDirectivesTest.scala
@@ -72,6 +72,15 @@ class PatchDirectivesTest
}
}
+ it should "handle optional values correctly when old value is null" in {
+ val fooRetrieve = Future.successful(Some(testFoo.copy(bar = None)))
+
+ Patch("/api/v1/foos/1", jsonEntity("""{"bar": {"name": "My Bar","size":10}}""")) ~> route(fooRetrieve) ~> check {
+ handled shouldBe true
+ responseAs[Foo] shouldBe testFoo.copy(bar = Some(Bar(Name("My Bar"), 10)))
+ }
+ }
+
it should "return a 400 for nulls on non-optional values" in {
val fooRetrieve = Future.successful(Some(testFoo))