ApplyTo() public method

Apply this JsonPatchDocument
public ApplyTo ( object objectToApplyTo ) : void
objectToApplyTo object Object to apply the JsonPatchDocument to
return void
示例#1
0
        public void ApplyTo_JsonPatchDocument_PrefixModelState()
        {
            // Arrange
            var operation = new Operation<Customer>("add", "Customer/CustomerId", from: null, value: "TestName");
            var patchDoc = new JsonPatchDocument<Customer>();
            patchDoc.Operations.Add(operation);

            var modelState = new ModelStateDictionary();

            // Act
            patchDoc.ApplyTo(new Customer(), modelState, "jsonpatch");

            // Assert
            var error = Assert.Single(modelState["jsonpatch.Customer"].Errors);
            Assert.Equal("The property at path 'Customer/CustomerId' could not be added.", error.ErrorMessage);
        }
示例#2
0
        public void ReplacePropertyValue_ForExpandoObject_WithCustomNamingStrategy()
        {
            // Arrange
            var contractResolver = new DefaultContractResolver
            {
                NamingStrategy = new TestNamingStrategy()
            };

            dynamic targetObject = new ExpandoObject();

            targetObject.customTest = 1;

            var patchDocument = new JsonPatchDocument();

            patchDocument.Replace("Test", 2);
            patchDocument.ContractResolver = contractResolver;

            // Act
            patchDocument.ApplyTo(targetObject);

            // Assert
            Assert.Equal(2, targetObject.customTest);
        }
示例#3
0
        public void CopyPropertyValue_ToDynamicTestObject_WithCustomNamingStrategy()
        {
            // Arrange
            var contractResolver = new DefaultContractResolver
            {
                NamingStrategy = new TestNamingStrategy()
            };

            dynamic targetObject = new DynamicTestObject();

            targetObject.customStringProperty        = "A";
            targetObject.customAnotherStringProperty = "B";

            var patchDocument = new JsonPatchDocument();

            patchDocument.Copy("StringProperty", "AnotherStringProperty");
            patchDocument.ContractResolver = contractResolver;

            // Act
            patchDocument.ApplyTo(targetObject);

            // Assert
            Assert.Equal("A", targetObject.customAnotherStringProperty);
        }
示例#4
0
        public void AddProperty_ToDynamicTestObject_WithCustomNamingStrategy()
        {
            // Arrange
            var contractResolver = new DefaultContractResolver
            {
                NamingStrategy = new TestNamingStrategy()
            };

            dynamic targetObject = new DynamicTestObject();

            targetObject.Test = 1;

            var patchDocument = new JsonPatchDocument();

            patchDocument.Add("NewInt", 1);
            patchDocument.ContractResolver = contractResolver;

            // Act
            patchDocument.ApplyTo(targetObject);

            // Assert
            Assert.Equal(1, targetObject.customNewInt);
            Assert.Equal(1, targetObject.Test);
        }