aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Json Patch: System.NullReferenceException while trying to use copy operation from property with null value

Open Iamcerba opened this issue 8 years ago • 3 comments

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.

Iamcerba avatar Sep 26 '18 15:09 Iamcerba

Thanks for contacting us, @Iamcerba. Will you be interested in submitting a fix for this?

mkArtakMSFT avatar Nov 07 '18 23:11 mkArtakMSFT

Hey, @mkArtakMSFT

I have created a pull request: https://github.com/aspnet/JsonPatch/pull/151 Please check.

Iamcerba avatar Nov 08 '18 09:11 Iamcerba

Merged in https://github.com/aspnet/JsonPatch/commit/bf9fd0d10667e605d1df45f89e198be7f3bd1138. Thanks for the PR!

pranavkm avatar Nov 08 '18 22:11 pranavkm