Skip to content
This repository was archived by the owner on Nov 15, 2018. It is now read-only.

class property setters with null checks makes Typed JsonPatchDocument tests fail. #32

Closed
wannado opened this issue Jul 27, 2016 · 12 comments
Assignees

Comments

@wannado
Copy link

wannado commented Jul 27, 2016

class property setters with null checks like the following makes tests fail.
Eg. I added a backing field for SimpleDTO.StringProperty with a null check like the following.
These tests appear in NestedObjectsTests and ObjectAdapterTests class.

    public class SimpleDTO
    {
        public List<int> IntegerList { get; set; }
        public IList<int> IntegerIList { get; set; }
        public int IntegerValue { get; set; }
        string stringProperty;

        public string StringProperty
        {
            get
            {
                return stringProperty;
            }

            set
            {
                if (value == null) {
                    throw new ArgumentNullException();
                }
                stringProperty = value;
            }
        }

Test Name:  Microsoft.AspNetCore.JsonPatch.Test.NestedObjectTests.Replace
Test FullName:  Microsoft.AspNetCore.JsonPatch.Test.NestedObjectTests.Replace
Test Source:    D:\0\JsonPatch\test\Microsoft.AspNetCore.JsonPatch.Test\NestedObjectTests.cs : line 886
Test Outcome:   Failed
Test Duration:  0:00:00.036

Result StackTrace:  
at Newtonsoft.Json.Serialization.ExpressionValueProvider.SetValue(Object target, Object value)
   at Microsoft.AspNetCore.JsonPatch.Adapters.ObjectAdapter.Remove(String path, Object objectToApplyTo, Operation operationToReport) in D:\0\JsonPatch\src\Microsoft.AspNetCore.JsonPatch\Adapters\ObjectAdapter.cs:line 687
   at Microsoft.AspNetCore.JsonPatch.Adapters.ObjectAdapter.Replace(Operation operation, Object objectToApplyTo) in D:\0\JsonPatch\src\Microsoft.AspNetCore.JsonPatch\Adapters\ObjectAdapter.cs:line 725
   at Microsoft.AspNetCore.JsonPatch.Operations.Operation`1.Apply(TModel objectToApplyTo, IObjectAdapter adapter) in D:\0\JsonPatch\src\Microsoft.AspNetCore.JsonPatch\Operations\OperationOfT.cs:line 67
   at Microsoft.AspNetCore.JsonPatch.JsonPatchDocument`1.ApplyTo(TModel objectToApplyTo, IObjectAdapter adapter) in D:\0\JsonPatch\src\Microsoft.AspNetCore.JsonPatch\JsonPatchDocumentOfT.cs:line 674
   at Microsoft.AspNetCore.JsonPatch.JsonPatchDocument`1.ApplyTo(TModel objectToApplyTo) in D:\0\JsonPatch\src\Microsoft.AspNetCore.JsonPatch\JsonPatchDocumentOfT.cs:line 636
   at Microsoft.AspNetCore.JsonPatch.Test.NestedObjectTests.Replace() in D:\0\JsonPatch\test\Microsoft.AspNetCore.JsonPatch.Test\NestedObjectTests.cs:line 903
   at Microsoft.AspNetCore.JsonPatch.Test.SimpleDTO.set_StringProperty(String value) in D:\0\JsonPatch\test\Microsoft.AspNetCore.JsonPatch.Test\SimpleDTO.cs:line 28
   at Newtonsoft.Json.Serialization.ExpressionValueProvider.SetValue(Object target, Object value)
Result Message: 
Error setting value to 'StringProperty' on 'Microsoft.AspNetCore.JsonPatch.Test.SimpleDTO'.
Value cannot be null.

-----------------------
Test Name:  Microsoft.AspNetCore.JsonPatch.Test.NestedObjectTests.Move
Test FullName:  Microsoft.AspNetCore.JsonPatch.Test.NestedObjectTests.Move
Test Source:    D:\0\JsonPatch\test\Microsoft.AspNetCore.JsonPatch.Test\NestedObjectTests.cs : line 1683
Test Outcome:   Failed
Test Duration:  0:00:00.005

Result StackTrace:  
at Newtonsoft.Json.Serialization.ExpressionValueProvider.SetValue(Object target, Object value)
   at Microsoft.AspNetCore.JsonPatch.Adapters.ObjectAdapter.Remove(String path, Object objectToApplyTo, Operation operationToReport) in D:\0\JsonPatch\src\Microsoft.AspNetCore.JsonPatch\Adapters\ObjectAdapter.cs:line 687
   at Microsoft.AspNetCore.JsonPatch.Adapters.ObjectAdapter.Move(Operation operation, Object objectToApplyTo) in D:\0\JsonPatch\src\Microsoft.AspNetCore.JsonPatch\Adapters\ObjectAdapter.cs:line 411
   at Microsoft.AspNetCore.JsonPatch.Operations.Operation`1.Apply(TModel objectToApplyTo, IObjectAdapter adapter) in D:\0\JsonPatch\src\Microsoft.AspNetCore.JsonPatch\Operations\OperationOfT.cs:line 70
   at Microsoft.AspNetCore.JsonPatch.JsonPatchDocument`1.ApplyTo(TModel objectToApplyTo, IObjectAdapter adapter) in D:\0\JsonPatch\src\Microsoft.AspNetCore.JsonPatch\JsonPatchDocumentOfT.cs:line 674
   at Microsoft.AspNetCore.JsonPatch.JsonPatchDocument`1.ApplyTo(TModel objectToApplyTo) in D:\0\JsonPatch\src\Microsoft.AspNetCore.JsonPatch\JsonPatchDocumentOfT.cs:line 636
   at Microsoft.AspNetCore.JsonPatch.Test.NestedObjectTests.Move() in D:\0\JsonPatch\test\Microsoft.AspNetCore.JsonPatch.Test\NestedObjectTests.cs:line 1699
   at Microsoft.AspNetCore.JsonPatch.Test.SimpleDTO.set_StringProperty(String value) in D:\0\JsonPatch\test\Microsoft.AspNetCore.JsonPatch.Test\SimpleDTO.cs:line 28
   at Newtonsoft.Json.Serialization.ExpressionValueProvider.SetValue(Object target, Object value)
Result Message: 
Error setting value to 'StringProperty' on 'Microsoft.AspNetCore.JsonPatch.Test.SimpleDTO'.
Value cannot be null.

@wannado wannado changed the title class property setters with null checks like the following makes 15 tests fail. class property setters with null checks makes Typed JsonPatchDocument tests fail. Jul 27, 2016
@Eilon
Copy link
Member

Eilon commented Jul 27, 2016

@wannado out of curiosity, was the PATCH trying to clear out the value? That is, if the PATCH "document" was indicating that the property needed to be cleared, then I would expect this to happen because the property is indicating that it can't be cleared.

Or is the PATCH document just trying to set a new value (e.g. change property from "foo" to "bar")?

@wannado
Copy link
Author

wannado commented Jul 28, 2016

@Eilon Thanks for looking in to it!!! Yes patch document had "op" as "replace", as you said, just trying to set a new value (e.g. change property from "foo" to "bar".

@Eilon
Copy link
Member

Eilon commented Jul 28, 2016

@wannado ah ok then that indeed sounds like a bug. We'll take a look!

@Eilon Eilon added this to the 1.1.0 milestone Jul 28, 2016
@Eilon Eilon modified the milestones: 1.1.0, 1.1.0-preview1 Oct 6, 2016
@Eilon Eilon modified the milestones: 1.2.0, 1.1.0 Oct 26, 2016
@ryanwalls
Copy link

We are hitting this as well when we are trying to convert a string to an enum in a setter, e.g.

        public string Status
        {
            get
            {
                return this.SimulationStatus.ToString();
            }

            set
            {
                SimulationStatus updatedStatus;
                Console.WriteLine($"Trying to set simulation status to value: {value}");
                if (Enum.TryParse(value, out updatedStatus))
                {
                    this.SimulationStatus = updatedStatus;
                }
                else
                {
                    throw new ArgumentException($"Invalid simulation status setting: {value}");
                }
            }
        }

@Eilon
Copy link
Member

Eilon commented Jan 9, 2017

@jbagga can you take a look?

@kichalla
Copy link
Member

@Eilon Thanks for looking in to it!!! Yes patch document had "op" as "replace", as you said, just trying to set a new value (e.g. change property from "foo" to "bar".

I cannot repro this.

@ryanwalls what kind of operation are you trying to do when you hit this issue? for example, if you were trying to do a 'move' operation, the value of source property is copied onto the destination property and the source property would be set to its default value (in case of 'string' since its a reference type, it would be set to null).

@kichalla
Copy link
Member

Update: I was able to repro it on 1.0...In this version during a Replace operation, a Remove operation is run first and then an Add and in case of Remove operation the default value for a type is set because of which we are seeing this exception.

@jbagga
Copy link
Contributor

jbagga commented Jan 25, 2017

@ryanwalls @wannado Are you able to reproduce this? As @kichalla noted, there have been changes to the Replace operation since 1.0.0. If you can provide more information like the build version (1.0.0 or 1.1.0) and type of operation performed, we can investigate this further!

@ryanwalls
Copy link

I'm doing a "replace" operation with version 1.1.0.

@jbagga
Copy link
Contributor

jbagga commented Jan 26, 2017

@ryanwalls I have tried to reproduce this issue with 1.1.0 but haven't been able to. Can you send us a sample or repro steps?

@Eilon Also, adding tests to 2.0.0 to ensure Replace does not fail for properties with null checks. See #56.

@ryanwalls
Copy link

@jbagga No worries. If you've ensured null checks work in 2.0.0, I'll just wait to update. We've worked around the problem for now.

@jbagga
Copy link
Contributor

jbagga commented Jan 26, 2017

Closing this as it cannot be reproduced for 1.1.0. Added tests to ensure property setters with null checks do not throw for Replace operation.

Please feel free to reopen this issue if you have a repro sample app for 1.1.0. We can investigate this further then!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants