Bladeren bron

Handle null values when applying JSON Patch to a JObject (#29410)

If value null then call JValue.CreateNull instead of JToken.FromObject
Addresses #25431
gadenton 5 jaren geleden
bovenliggende
commit
460d4d95da

+ 3 - 3
src/Features/JsonPatch/src/Internal/JObjectAdapter.cs

@@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
         {
         {
             var obj = (JObject) target;
             var obj = (JObject) target;
 
 
-            obj[segment] = JToken.FromObject(value);
+            obj[segment] = value != null ? JToken.FromObject(value) : JValue.CreateNull();
 
 
             errorMessage = null;
             errorMessage = null;
             return true;
             return true;
@@ -77,7 +77,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
                 return false;
                 return false;
             }
             }
 
 
-            obj[segment] = JToken.FromObject(value);
+            obj[segment] = value != null ? JToken.FromObject(value) : JValue.CreateNull();
 
 
             errorMessage = null;
             errorMessage = null;
             return true;
             return true;
@@ -137,4 +137,4 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
             return true;
             return true;
         }
         }
     }
     }
-}
+}

+ 32 - 0
src/Features/JsonPatch/test/JsonPatchDocumentJObjectTest.cs

@@ -125,6 +125,22 @@ namespace Microsoft.AspNetCore.JsonPatch
             Assert.Equal("Foo", model.CustomData["Name"].Value<string>());
             Assert.Equal("Foo", model.CustomData["Name"].Value<string>());
         }
         }
 
 
+        [Fact]
+        public void ApplyTo_Model_Add_Null()
+        {
+            // Arrange
+            var model = new ObjectWithJObject();
+            var patch = new JsonPatchDocument<ObjectWithJObject>();
+
+            patch.Operations.Add(new Operation<ObjectWithJObject>("add", "/CustomData/Name", null, null));
+
+            // Act
+            patch.ApplyTo(model);
+
+            // Assert
+            Assert.Equal(JTokenType.Null, model.CustomData["Name"].Type);
+        }
+
         [Fact]
         [Fact]
         public void ApplyTo_Model_Replace()
         public void ApplyTo_Model_Replace()
         {
         {
@@ -140,5 +156,21 @@ namespace Microsoft.AspNetCore.JsonPatch
             // Assert
             // Assert
             Assert.Equal("[email protected]", model.CustomData["Email"].Value<string>());
             Assert.Equal("[email protected]", model.CustomData["Email"].Value<string>());
         }
         }
+
+        [Fact]
+        public void ApplyTo_Model_Replace_Null()
+        {
+            // Arrange
+            var model = new ObjectWithJObject { CustomData = JObject.FromObject(new { Email = "[email protected]", Name = "Bar" }) };
+            var patch = new JsonPatchDocument<ObjectWithJObject>();
+
+            patch.Operations.Add(new Operation<ObjectWithJObject>("replace", "/CustomData/Email", null, null));
+
+            // Act
+            patch.ApplyTo(model);
+
+            // Assert
+            Assert.Equal(JTokenType.Null, model.CustomData["Email"].Type);
+        }
     }
     }
 }
 }