Json Patch: System.NullReferenceException while trying to use copy operation from property with null value
I'm using 2.1.1 version of Microsoft.AspNetCore.JsonPatch package.
Steps to reproduce:
class Program
{
static void Main(string[] args)
{
var testObj = new MyClass()
{
Property1 = null,
Property2 = "test2"
};
var patchRequest = new JsonPatchDocument<MyClass>();
patchRequest.Operations.Add(new Operation<MyClass>(op: "copy", path: "Property2", from: "Property1"));
patchRequest.ApplyTo(testObj); // Throws System.NullReferenceException
Assert.Equal(testObj.Property1, testObj.Property2);
}
public class MyClass
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
}
Actual result:
System.NullReferenceException is being thrown.
Exception details:
System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=System.Private.CoreLib StackTrace: at System.Object.GetType() at Microsoft.AspNetCore.JsonPatch.Adapters.ObjectAdapter.Copy(Operation operation, Object objectToApplyTo) at Microsoft.AspNetCore.JsonPatch.JsonPatchDocument`1.ApplyTo(TModel objectToApplyTo, IObjectAdapter adapter) at JsonPatchCopyIssue.Program.Main(String[] args) in ...\JsonPatchCopyIssue\Program.cs:line 20
Expected result:
After brief overview didn't find any related details in https://tools.ietf.org/html/rfc6902, but I believe that value should be copied in destination property without throwing an exception.
Thanks for contacting us, @Iamcerba. Will you be interested in submitting a fix for this?
Hey, @mkArtakMSFT
I have created a pull request: https://github.com/aspnet/JsonPatch/pull/151 Please check.
Merged in https://github.com/aspnet/JsonPatch/commit/bf9fd0d10667e605d1df45f89e198be7f3bd1138. Thanks for the PR!