Reorganize tests (#117)

Addresses #105
This commit is contained in:
Jass Bagga 2017-10-26 12:51:06 -07:00 committed by GitHub
parent e46ba481c8
commit e453fafad5
47 changed files with 2091 additions and 8075 deletions

View File

@ -0,0 +1,153 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Dynamic;
using Newtonsoft.Json.Serialization;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch
{
public class CustomNamingStrategyTests
{
[Fact]
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);
}
[Fact]
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);
}
[Fact]
public void MovePropertyValue_ForExpandoObject_WithCustomNamingStrategy()
{
// Arrange
var contractResolver = new DefaultContractResolver
{
NamingStrategy = new TestNamingStrategy()
};
dynamic targetObject = new ExpandoObject();
targetObject.customStringProperty = "A";
targetObject.customAnotherStringProperty = "B";
var patchDocument = new JsonPatchDocument();
patchDocument.Move("StringProperty", "AnotherStringProperty");
patchDocument.ContractResolver = contractResolver;
// Act
patchDocument.ApplyTo(targetObject);
var cont = targetObject as IDictionary<string, object>;
cont.TryGetValue("customStringProperty", out var valueFromDictionary);
// Assert
Assert.Equal("A", targetObject.customAnotherStringProperty);
Assert.Null(valueFromDictionary);
}
[Fact]
public void RemoveProperty_FromDictionaryObject_WithCustomNamingStrategy()
{
// Arrange
var contractResolver = new DefaultContractResolver
{
NamingStrategy = new TestNamingStrategy()
};
var targetObject = new Dictionary<string, int>()
{
{ "customTest", 1},
};
var patchDocument = new JsonPatchDocument();
patchDocument.Remove("Test");
patchDocument.ContractResolver = contractResolver;
// Act
patchDocument.ApplyTo(targetObject);
var cont = targetObject as IDictionary<string, int>;
cont.TryGetValue("customTest", out var valueFromDictionary);
// Assert
Assert.Equal(0, valueFromDictionary);
}
[Fact]
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);
}
private class TestNamingStrategy : NamingStrategy
{
public new bool ProcessDictionaryKeys => true;
public override string GetDictionaryKey(string key)
{
return "custom" + key;
}
protected override string ResolvePropertyName(string name)
{
return name;
}
}
}
}

View File

@ -1,553 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Dynamic;
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Newtonsoft.Json;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class AddOperationTests
{
[Fact]
public void AddNewPropertyShouldFailIfRootIsNotAnExpandoObject()
{
dynamic doc = new
{
Test = 1
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("NewInt", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The target location specified by path segment '{0}' was not found.", "NewInt"),
exception.Message);
}
[Fact]
public void AddNewProperty()
{
dynamic obj = new ExpandoObject();
obj.Test = 1;
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("NewInt", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(obj);
Assert.Equal(1, obj.NewInt);
Assert.Equal(1, obj.Test);
}
[Fact]
public void AddNewPropertyToNestedAnonymousObjectShouldFail()
{
dynamic doc = new
{
Test = 1,
nested = new { }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("Nested/NewInt", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The target location specified by path segment '{0}' was not found.", "NewInt"),
exception.Message);
}
[Fact]
public void AddNewPropertyToTypedObjectShouldFail()
{
dynamic doc = new
{
Test = 1,
nested = new NestedObject()
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("Nested/NewInt", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The target location specified by path segment '{0}' was not found.", "NewInt"),
exception.Message);
}
[Fact]
public void AddToExistingPropertyOnNestedObject()
{
dynamic doc = new
{
Test = 1,
nested = new NestedObject()
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("Nested/StringProperty", "A");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("A", doc.nested.StringProperty);
Assert.Equal(1, doc.Test);
}
[Fact]
public void AddNewPropertyToExpandoOject()
{
dynamic doc = new
{
Test = 1,
nested = new ExpandoObject()
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("Nested/NewInt", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(1, doc.nested.NewInt);
Assert.Equal(1, doc.Test);
}
[Fact]
public void AddNewPropertyToExpandoOjectInTypedObject()
{
var doc = new NestedObject()
{
DynamicProperty = new ExpandoObject()
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("DynamicProperty/NewInt", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(1, doc.DynamicProperty.NewInt);
}
[Fact]
public void AddNewPropertyToTypedObjectInExpandoObject()
{
dynamic dynamicProperty = new ExpandoObject();
dynamicProperty.StringProperty = "A";
var doc = new NestedObject()
{
DynamicProperty = dynamicProperty
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("DynamicProperty/StringProperty", "B");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("B", doc.DynamicProperty.StringProperty);
}
[Fact]
public void AddNewPropertyToAnonymousObjectShouldFail()
{
dynamic doc = new
{
Test = 1
};
dynamic valueToAdd = new { IntValue = 1, StringValue = "test", GuidValue = Guid.NewGuid() };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("ComplexProperty", valueToAdd);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The target location specified by path segment '{0}' was not found.", "ComplexProperty"),
exception.Message);
}
[Fact]
public void AddResultsReplaceShouldFailOnAnonymousDueToNoSetter()
{
var doc = new
{
StringProperty = "A"
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("StringProperty", "B");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The property at path '{0}' could not be updated.", "StringProperty"),
exception.Message);
}
[Fact]
public void AddResultsShouldReplace()
{
dynamic doc = new ExpandoObject();
doc.StringProperty = "A";
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("StringProperty", "B");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("B", doc.StringProperty);
}
[Fact]
public void AddResultsShouldReplaceInNested()
{
dynamic doc = new ExpandoObject();
doc.InBetweenFirst = new ExpandoObject();
doc.InBetweenFirst.InBetweenSecond = new ExpandoObject();
doc.InBetweenFirst.InBetweenSecond.StringProperty = "A";
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("/InBetweenFirst/InBetweenSecond/StringProperty", "B");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("B", doc.InBetweenFirst.InBetweenSecond.StringProperty);
}
[Fact]
public void AddResultsShouldReplaceInNestedInDynamic()
{
dynamic doc = new ExpandoObject();
doc.Nested = new NestedObject();
doc.Nested.DynamicProperty = new ExpandoObject();
doc.Nested.DynamicProperty.InBetweenFirst = new ExpandoObject();
doc.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond = new ExpandoObject();
doc.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond.StringProperty = "A";
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("/Nested/DynamicProperty/InBetweenFirst/InBetweenSecond/StringProperty", "B");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("B", doc.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond.StringProperty);
}
[Fact]
public void ShouldNotBeAbleToAddToNonExistingPropertyThatIsNotTheRoot()
{
//Adding to a Nonexistent Target
//
// An example target JSON document:
// { "foo": "bar" }
// A JSON Patch document:
// [
// { "op": "add", "path": "/baz/bat", "value": "qux" }
// ]
// This JSON Patch document, applied to the target JSON document above,
// would result in an error (therefore, it would not be applied),
// because the "add" operation's target location that references neither
// the root of the document, nor a member of an existing object, nor a
// member of an existing array.
var doc = new NestedObject()
{
DynamicProperty = new ExpandoObject()
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("DynamicProperty/OtherProperty/IntProperty", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format(
"For operation '{0}', the target location specified by path '{1}' was not found.",
"add",
"/DynamicProperty/OtherProperty/IntProperty"),
exception.Message);
}
[Fact]
public void ShouldNotBeAbleToAddToNonExistingPropertyInNestedPropertyThatIsNotTheRoot()
{
//Adding to a Nonexistent Target
//
// An example target JSON document:
// { "foo": "bar" }
// A JSON Patch document:
// [
// { "op": "add", "path": "/baz/bat", "value": "qux" }
// ]
// This JSON Patch document, applied to the target JSON document above,
// would result in an error (therefore, it would not be applied),
// because the "add" operation's target location that references neither
// the root of the document, nor a member of an existing object, nor a
// member of an existing array.
var doc = new
{
Foo = "bar"
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("baz/bat", "qux");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The target location specified by path segment '{0}' was not found.", "baz"),
exception.Message);
}
[Fact]
public void ShouldNotReplacePropertyWithDifferentCase()
{
dynamic doc = new ExpandoObject();
doc.StringProperty = "A";
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("stringproperty", "B");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("A", doc.StringProperty);
Assert.Equal("B", doc.stringproperty);
}
[Fact]
public void AddToList()
{
var doc = new
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 4, 1, 2, 3 }, doc.IntegerList);
}
[Fact]
public void AddToListNegativePosition()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/-1", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"),
exception.Message);
}
[Fact]
public void ShouldAddToListWithDifferentCase()
{
var doc = new
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("integerlist/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 4, 1, 2, 3 }, doc.IntegerList);
}
[Fact]
public void AddToListInvalidPositionTooLarge()
{
var doc = new
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/4", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "4"),
exception.Message);
}
[Fact]
public void AddToListAtBeginning()
{
var doc = new
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 4, 1, 2, 3 }, doc.IntegerList);
}
[Fact]
public void AddToListInvalidPositionTooSmall()
{
var doc = new
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/-1", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"),
exception.Message);
}
[Fact]
public void AddToListAppend()
{
var doc = new
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/-", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 3, 4 }, doc.IntegerList);
}
}
}

View File

@ -1,121 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Newtonsoft.Json;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class AddTypedOperationTests
{
[Fact]
public void AddToListNegativePosition()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/-1", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"),
exception.Message);
}
[Fact]
public void AddToListInList()
{
var doc = new SimpleObjectWithNestedObject()
{
ListOfSimpleObject = new List<SimpleObject>()
{
new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("ListOfSimpleObject/0/IntegerList/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 4, 1, 2, 3 }, doc.ListOfSimpleObject[0].IntegerList);
}
[Fact]
public void AddToListInListInvalidPositionTooSmall()
{
var doc = new SimpleObjectWithNestedObject()
{
ListOfSimpleObject = new List<SimpleObject>()
{
new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("ListOfSimpleObject/-1/IntegerList/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"),
exception.Message);
}
[Fact]
public void AddToListInListInvalidPositionTooLarge()
{
var doc = new SimpleObjectWithNestedObject()
{
ListOfSimpleObject = new List<SimpleObject>()
{
new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("ListOfSimpleObject/20/IntegerList/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "20"),
exception.Message);
}
}
}

View File

@ -1,245 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Dynamic;
using Newtonsoft.Json;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class CopyOperationTests
{
[Fact]
public void Copy()
{
dynamic doc = new ExpandoObject();
doc.StringProperty = "A";
doc.AnotherStringProperty = "B";
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("StringProperty", "AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("A", doc.AnotherStringProperty);
}
[Fact]
public void CopyInList()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerList/0", "IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 1, 2, 3 }, doc.IntegerList);
}
[Fact]
public void CopyFromListToEndOfList()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerList/0", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 3, 1 }, doc.IntegerList);
}
[Fact]
public void CopyFromListToNonList()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerList/0", "IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(1, doc.IntegerValue);
}
[Fact]
public void CopyFromNonListToList()
{
dynamic doc = new ExpandoObject();
doc.IntegerValue = 5;
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerValue", "IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 5, 1, 2, 3 }, doc.IntegerList);
}
[Fact]
public void CopyToEndOfList()
{
dynamic doc = new ExpandoObject();
doc.IntegerValue = 5;
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerValue", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 3, 5 }, doc.IntegerList);
}
[Fact]
public void NestedCopy()
{
dynamic doc = new ExpandoObject();
doc.SimpleObject = new SimpleObject()
{
StringProperty = "A",
AnotherStringProperty = "B"
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleObject/StringProperty", "SimpleObject/AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("A", doc.SimpleObject.AnotherStringProperty);
}
[Fact]
public void NestedCopyInList()
{
dynamic doc = new ExpandoObject();
doc.SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleObject/IntegerList/0", "SimpleObject/IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 1, 2, 3 }, doc.SimpleObject.IntegerList);
}
[Fact]
public void NestedCopyFromListToEndOfList()
{
dynamic doc = new ExpandoObject();
doc.SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleObject/IntegerList/0", "SimpleObject/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 3, 1 }, doc.SimpleObject.IntegerList);
}
[Fact]
public void NestedCopyFromListToNonList()
{
dynamic doc = new ExpandoObject();
doc.SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleObject/IntegerList/0", "SimpleObject/IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(1, doc.SimpleObject.IntegerValue);
}
[Fact]
public void NestedCopyFromNonListToList()
{
dynamic doc = new ExpandoObject();
doc.SimpleObject = new SimpleObject()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleObject/IntegerValue", "SimpleObject/IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 5, 1, 2, 3 }, doc.SimpleObject.IntegerList);
}
[Fact]
public void NestedCopyToEndOfList()
{
dynamic doc = new ExpandoObject();
doc.SimpleObject = new SimpleObject()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleObject/IntegerValue", "SimpleObject/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 3, 5 }, doc.SimpleObject.IntegerList);
}
}
}

View File

@ -1,268 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Newtonsoft.Json;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class CopyTypedOperationTests
{
[Fact]
public void Copy()
{
var doc = new SimpleObject()
{
StringProperty = "A",
AnotherStringProperty = "B"
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("StringProperty", "AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("A", doc.AnotherStringProperty);
}
[Fact]
public void CopyInList()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerList/0", "IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 1, 2, 3 }, doc.IntegerList);
}
[Fact]
public void CopyFromListToEndOfList()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerList/0", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 3, 1 }, doc.IntegerList);
}
[Fact]
public void CopyFromListToNonList()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerList/0", "IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(1, doc.IntegerValue);
}
[Fact]
public void CopyFromNonListToList()
{
var doc = new SimpleObject()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerValue", "IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 5, 1, 2, 3 }, doc.IntegerList);
}
[Fact]
public void CopyToEndOfList()
{
var doc = new SimpleObject()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerValue", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 3, 5 }, doc.IntegerList);
}
[Fact]
public void NestedCopy()
{
var doc = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
StringProperty = "A",
AnotherStringProperty = "B"
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleObject/StringProperty", "SimpleObject/AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("A", doc.SimpleObject.AnotherStringProperty);
}
[Fact]
public void NestedCopyInList()
{
var doc = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleObject/IntegerList/0", "SimpleObject/IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 1, 2, 3 }, doc.SimpleObject.IntegerList);
}
[Fact]
public void NestedCopyFromListToEndOfList()
{
var doc = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleObject/IntegerList/0", "SimpleObject/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 3, 1 }, doc.SimpleObject.IntegerList);
}
[Fact]
public void NestedCopyFromListToNonList()
{
var doc = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleObject/IntegerList/0", "SimpleObject/IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(1, doc.SimpleObject.IntegerValue);
}
[Fact]
public void NestedCopyFromNonListToList()
{
var doc = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleObject/IntegerValue", "SimpleObject/IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 5, 1, 2, 3 }, doc.SimpleObject.IntegerList);
}
[Fact]
public void NestedCopyToEndOfList()
{
var doc = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleObject/IntegerValue", "SimpleObject/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 3, 5 }, doc.SimpleObject.IntegerList);
}
}
}

View File

@ -1,154 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Dynamic;
using Newtonsoft.Json.Serialization;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
{
public class CustomNamingStrategyTests
{
[Fact]
public void AddProperty_ToExpandoObject_WithCustomNamingStrategy()
{
// Arrange
var contractResolver = new DefaultContractResolver
{
NamingStrategy = new TestNamingStrategy()
};
dynamic obj = new ExpandoObject();
obj.Test = 1;
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Add("NewInt", 1);
patchDoc.ContractResolver = contractResolver;
// Act
patchDoc.ApplyTo(obj);
// Assert
Assert.Equal(1, obj.customNewInt);
Assert.Equal(1, obj.Test);
}
[Fact]
public void CopyPropertyValue_ForExpandoObject_WithCustomNamingStrategy()
{
// Arrange
var contractResolver = new DefaultContractResolver
{
NamingStrategy = new TestNamingStrategy()
};
dynamic obj = new ExpandoObject();
obj.customStringProperty = "A";
obj.customAnotherStringProperty = "B";
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("StringProperty", "AnotherStringProperty");
patchDoc.ContractResolver = contractResolver;
// Act
patchDoc.ApplyTo(obj);
// Assert
Assert.Equal("A", obj.customAnotherStringProperty);
}
[Fact]
public void MovePropertyValue_ForExpandoObject_WithCustomNamingStrategy()
{
// Arrange
var contractResolver = new DefaultContractResolver
{
NamingStrategy = new TestNamingStrategy()
};
dynamic obj = new ExpandoObject();
obj.customStringProperty = "A";
obj.customAnotherStringProperty = "B";
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("StringProperty", "AnotherStringProperty");
patchDoc.ContractResolver = contractResolver;
// Act
patchDoc.ApplyTo(obj);
var cont = obj as IDictionary<string, object>;
cont.TryGetValue("customStringProperty", out var valueFromDictionary);
// Assert
Assert.Equal("A", obj.customAnotherStringProperty);
Assert.Null(valueFromDictionary);
}
[Fact]
public void RemoveProperty_FromExpandoObject_WithCustomNamingStrategy()
{
// Arrange
var contractResolver = new DefaultContractResolver
{
NamingStrategy = new TestNamingStrategy()
};
dynamic obj = new ExpandoObject();
obj.customTest = 1;
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("Test");
patchDoc.ContractResolver = contractResolver;
// Act
patchDoc.ApplyTo(obj);
var cont = obj as IDictionary<string, object>;
cont.TryGetValue("customTest", out var valueFromDictionary);
// Assert
Assert.Null(valueFromDictionary);
}
[Fact]
public void ReplacePropertyValue_ForExpandoObject_WithCustomNamingStrategy()
{
// Arrange
var contractResolver = new DefaultContractResolver
{
NamingStrategy = new TestNamingStrategy()
};
dynamic obj = new ExpandoObject();
obj.customTest = 1;
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("Test", 2);
patchDoc.ContractResolver = contractResolver;
// Act
patchDoc.ApplyTo(obj);
// Assert
Assert.Equal(2, obj.customTest);
}
private class TestNamingStrategy : NamingStrategy
{
public new bool ProcessDictionaryKeys => true;
public override string GetDictionaryKey(string key)
{
return "custom" + key;
}
protected override string ResolvePropertyName(string name)
{
return name;
}
}
}
}

View File

@ -1,10 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class InheritedObject : SimpleObject
{
public string AdditionalStringProperty { get; set; }
}
}

View File

@ -1,333 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Dynamic;
using Newtonsoft.Json;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class MoveOperationTests
{
[Fact]
public void Move()
{
dynamic doc = new ExpandoObject();
doc.StringProperty = "A";
doc.AnotherStringProperty = "B";
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("StringProperty", "AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("A", doc.AnotherStringProperty);
var cont = doc as IDictionary<string, object>;
cont.TryGetValue("StringProperty", out object valueFromDictionary);
Assert.Null(valueFromDictionary);
}
[Fact]
public void MoveToNonExisting()
{
dynamic doc = new ExpandoObject();
doc.StringProperty = "A";
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("StringProperty", "AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("A", doc.AnotherStringProperty);
var cont = doc as IDictionary<string, object>;
cont.TryGetValue("StringProperty", out var valueFromDictionary);
Assert.Null(valueFromDictionary);
}
[Fact]
public void MoveDynamicToTyped()
{
dynamic doc = new ExpandoObject();
doc.StringProperty = "A";
doc.SimpleDTO = new SimpleObject() { AnotherStringProperty = "B" };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("StringProperty", "SimpleDTO/AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("A", doc.SimpleDTO.AnotherStringProperty);
var cont = doc as IDictionary<string, object>;
cont.TryGetValue("StringProperty", out object valueFromDictionary);
Assert.Null(valueFromDictionary);
}
[Fact]
public void MoveTypedToDynamic()
{
dynamic doc = new ExpandoObject();
doc.StringProperty = "A";
doc.SimpleDTO = new SimpleObject() { AnotherStringProperty = "B" };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("SimpleDTO/AnotherStringProperty", "StringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("B", doc.StringProperty);
Assert.Null(doc.SimpleDTO.AnotherStringProperty);
}
[Fact]
public void NestedMove()
{
dynamic doc = new ExpandoObject();
doc.Nested = new SimpleObject()
{
StringProperty = "A",
AnotherStringProperty = "B"
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("Nested/StringProperty", "Nested/AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("A", doc.Nested.AnotherStringProperty);
Assert.Null(doc.Nested.StringProperty);
}
[Fact]
public void MoveInList()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerList/0", "IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 2, 1, 3 }, doc.IntegerList);
}
[Fact]
public void NestedMoveInList()
{
dynamic doc = new ExpandoObject();
doc.Nested = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("Nested/IntegerList/0", "Nested/IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 2, 1, 3 }, doc.Nested.IntegerList);
}
[Fact]
public void MoveFromListToEndOfList()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerList/0", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 2, 3, 1 }, doc.IntegerList);
}
[Fact]
public void NestedMoveFromListToEndOfList()
{
dynamic doc = new ExpandoObject();
doc.Nested = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("Nested/IntegerList/0", "Nested/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 2, 3, 1 }, doc.Nested.IntegerList);
}
[Fact]
public void MoveFomListToNonList()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerList/0", "IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 2, 3 }, doc.IntegerList);
Assert.Equal(1, doc.IntegerValue);
}
[Fact]
public void NestedMoveFomListToNonList()
{
dynamic doc = new ExpandoObject();
doc.Nested = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("Nested/IntegerList/0", "Nested/IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 2, 3 }, doc.Nested.IntegerList);
Assert.Equal(1, doc.Nested.IntegerValue);
}
[Fact]
public void MoveFromNonListToList()
{
dynamic doc = new ExpandoObject();
doc.IntegerValue = 5;
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerValue", "IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
var cont = doc as IDictionary<string, object>;
cont.TryGetValue("IntegerValue", out object valueFromDictionary);
Assert.Null(valueFromDictionary);
Assert.Equal(new List<int>() { 5, 1, 2, 3 }, doc.IntegerList);
}
[Fact]
public void NestedMoveFromNonListToList()
{
dynamic doc = new ExpandoObject();
doc.Nested = new SimpleObject()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("Nested/IntegerValue", "Nested/IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(0, doc.Nested.IntegerValue);
Assert.Equal(new List<int>() { 5, 1, 2, 3 }, doc.Nested.IntegerList);
}
[Fact]
public void MoveToEndOfList()
{
dynamic doc = new ExpandoObject();
doc.IntegerValue = 5;
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerValue", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
var cont = doc as IDictionary<string, object>;
cont.TryGetValue("IntegerValue", out var valueFromDictionary);
Assert.Null(valueFromDictionary);
Assert.Equal(new List<int>() { 1, 2, 3, 5 }, doc.IntegerList);
}
[Fact]
public void NestedMoveToEndOfList()
{
dynamic doc = new ExpandoObject();
doc.Nested = new SimpleObject()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("Nested/IntegerValue", "Nested/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(0, doc.Nested.IntegerValue);
Assert.Equal(new List<int>() { 1, 2, 3, 5 }, doc.Nested.IntegerList);
}
}
}

View File

@ -1,138 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Newtonsoft.Json;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class MoveTypedOperationTests
{
[Fact]
public void Move()
{
var doc = new SimpleObject()
{
StringProperty = "A",
AnotherStringProperty = "B"
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("StringProperty", "AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("A", doc.AnotherStringProperty);
Assert.Null(doc.StringProperty);
}
[Fact]
public void MoveInList()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerList/0", "IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 2, 1, 3 }, doc.IntegerList);
}
[Fact]
public void MoveFromListToEndOfList()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerList/0", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 2, 3, 1 }, doc.IntegerList);
}
[Fact]
public void MoveFomListToNonList()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerList/0", "IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 2, 3 }, doc.IntegerList);
Assert.Equal(1, doc.IntegerValue);
}
[Fact]
public void MoveFromNonListToList()
{
var doc = new SimpleObject()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerValue", "IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(0, doc.IntegerValue);
Assert.Equal(new List<int>() { 5, 1, 2, 3 }, doc.IntegerList);
}
[Fact]
public void MoveToEndOfList()
{
var doc = new SimpleObject()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerValue", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(0, doc.IntegerValue);
Assert.Equal(new List<int>() { 1, 2, 3, 5 }, doc.IntegerList);
}
}
}

View File

@ -1,95 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Newtonsoft.Json;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class PatchDocumentTests
{
[Fact]
public void InvalidPathAtBeginningShouldThrowException()
{
var patchDoc = new JsonPatchDocument();
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDoc.Add("//NewInt", 1);
});
Assert.Equal(
"The provided string '//NewInt' is an invalid path.",
exception.Message);
}
[Fact]
public void InvalidPathAtEndShouldThrowException()
{
var patchDoc = new JsonPatchDocument();
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDoc.Add("NewInt//", 1);
});
Assert.Equal(
"The provided string 'NewInt//' is an invalid path.",
exception.Message);
}
[Fact]
public void InvalidPathWithDotShouldThrowException()
{
var patchDoc = new JsonPatchDocument();
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDoc.Add("NewInt.Test", 1);
});
Assert.Equal(
"The provided string 'NewInt.Test' is an invalid path.",
exception.Message);
}
[Fact]
public void NonGenericPatchDocToGenericMustSerialize()
{
var doc = new SimpleObject()
{
StringProperty = "A",
AnotherStringProperty = "B"
};
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("StringProperty", "AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleObject>>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("A", doc.AnotherStringProperty);
}
[Fact]
public void GenericPatchDocToNonGenericMustSerialize()
{
var doc = new SimpleObject()
{
StringProperty = "A",
AnotherStringProperty = "B"
};
var patchDocTyped = new JsonPatchDocument<SimpleObject>();
patchDocTyped.Copy(o => o.StringProperty, o => o.AnotherStringProperty);
var patchDocUntyped = new JsonPatchDocument();
patchDocUntyped.Copy("StringProperty", "AnotherStringProperty");
var serializedTyped = JsonConvert.SerializeObject(patchDocTyped);
var serializedUntyped = JsonConvert.SerializeObject(patchDocUntyped);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serializedTyped);
deserialized.ApplyTo(doc);
Assert.Equal("A", doc.AnotherStringProperty);
}
}
}

View File

@ -1,313 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Dynamic;
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Newtonsoft.Json;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class RemoveOperationTests
{
[Fact]
public void RemovePropertyShouldFailIfRootIsAnonymous()
{
dynamic doc = new
{
Test = 1
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("Test");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The property at path '{0}' could not be updated.", "Test"),
exception.Message);
}
[Fact]
public void RemovePropertyShouldFailIfItDoesntExist()
{
dynamic doc = new ExpandoObject();
doc.Test = 1;
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("NonExisting");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The target location specified by path segment '{0}' was not found.", "NonExisting"),
exception.Message);
}
[Fact]
public void RemovePropertyFromExpandoObject()
{
dynamic obj = new ExpandoObject();
obj.Test = 1;
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("Test");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(obj);
var cont = obj as IDictionary<string, object>;
cont.TryGetValue("Test", out object valueFromDictionary);
Assert.Null(valueFromDictionary);
}
[Fact]
public void RemoveProperty_FromExpandoObject_MixedCase_ThrowsPathNotFoundException()
{
dynamic obj = new ExpandoObject();
obj.Test = 1;
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("test");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(obj);
});
Assert.Equal(
string.Format(
"The target location specified by path segment '{0}' was not found.",
"test"),
exception.Message);
}
[Fact]
public void RemoveNestedPropertyFromExpandoObject()
{
dynamic obj = new ExpandoObject();
obj.Test = new ExpandoObject();
obj.Test.AnotherTest = "A";
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("Test");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(obj);
var cont = obj as IDictionary<string, object>;
cont.TryGetValue("Test", out object valueFromDictionary);
Assert.Null(valueFromDictionary);
}
[Fact]
public void RemoveNestedProperty_FromExpandoObject_MixedCase_ThrowsPathNotFoundException()
{
dynamic obj = new ExpandoObject();
obj.Test = new ExpandoObject();
obj.Test.AnotherTest = "A";
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("test");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(obj);
});
Assert.Equal(
string.Format(
"The target location specified by path segment '{0}' was not found.",
"test"),
exception.Message);
}
[Fact]
public void NestedRemove()
{
dynamic doc = new ExpandoObject();
doc.SimpleDTO = new SimpleObject()
{
StringProperty = "A"
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/StringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Null(doc.SimpleDTO.StringProperty);
}
[Fact]
public void NestedRemove_MixedCase_ThrowsPathNotFoundException()
{
dynamic doc = new ExpandoObject();
doc.SimpleObject = new SimpleObject()
{
StringProperty = "A"
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("Simpleobject/stringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format(
"For operation '{0}', the target location specified by path '{1}' was not found.",
"remove",
"/Simpleobject/stringProperty"),
exception.Message);
}
[Fact]
public void NestedRemoveFromList()
{
dynamic doc = new ExpandoObject();
doc.SimpleDTO = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/IntegerList/2");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2 }, doc.SimpleDTO.IntegerList);
}
[Fact]
public void NestedRemoveFromList_MixedCase()
{
dynamic doc = new ExpandoObject();
doc.SimpleDTO = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/Integerlist/2");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2 }, doc.SimpleDTO.IntegerList);
}
[Fact]
public void NestedRemoveFromListInvalidPositionTooLarge()
{
dynamic doc = new ExpandoObject();
doc.SimpleDTO = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/IntegerList/3");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"),
exception.Message);
}
[Fact]
public void NestedRemoveFromListInvalidPositionTooSmall()
{
dynamic doc = new ExpandoObject();
doc.SimpleDTO = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/IntegerList/-1");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"),
exception.Message);
}
[Fact]
public void NestedRemoveFromEndOfList()
{
dynamic doc = new ExpandoObject();
doc.SimpleDTO = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2 }, doc.SimpleDTO.IntegerList);
}
}
}

View File

@ -1,244 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Newtonsoft.Json;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class RemoveTypedOperationTests
{
[Fact]
public void Remove()
{
var doc = new SimpleObject()
{
StringProperty = "A"
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("StringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Null(doc.StringProperty);
}
[Fact]
public void RemoveFromList()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("IntegerList/2");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2 }, doc.IntegerList);
}
[Fact]
public void RemoveFromListInvalidPositionTooLarge()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("IntegerList/3");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"),
exception.Message);
}
[Fact]
public void RemoveFromListInvalidPositionTooSmall()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("IntegerList/-1");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"),
exception.Message);
}
[Fact]
public void RemoveFromEndOfList()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2 }, doc.IntegerList);
}
[Fact]
public void NestedRemove()
{
var doc = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
StringProperty = "A"
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleObject/StringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Null(doc.SimpleObject.StringProperty);
}
[Fact]
public void NestedRemoveFromList()
{
var doc = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleObject/IntegerList/2");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2 }, doc.SimpleObject.IntegerList);
}
[Fact]
public void NestedRemoveFromListInvalidPositionTooLarge()
{
var doc = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleObject/IntegerList/3");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "3"),
exception.Message);
}
[Fact]
public void NestedRemoveFromListInvalidPositionTooSmall()
{
var doc = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleObject/IntegerList/-1");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
var exception = Assert.Throws<JsonPatchException>(() =>
{
deserialized.ApplyTo(doc);
});
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", "-1"),
exception.Message);
}
[Fact]
public void NestedRemoveFromEndOfList()
{
var doc = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleObject/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2 }, doc.SimpleObject.IntegerList);
}
}
}

View File

@ -1,206 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class ReplaceOperationTests
{
[Fact]
public void ReplaceGuidTest()
{
dynamic doc = new SimpleObject()
{
GuidValue = Guid.NewGuid()
};
var newGuid = Guid.NewGuid();
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("GuidValue", newGuid);
patchDoc.ApplyTo(doc);
Assert.Equal(newGuid, doc.GuidValue);
}
[Fact]
public void ReplaceGuidTestExpandoObject()
{
dynamic doc = new ExpandoObject();
doc.GuidValue = Guid.NewGuid();
var newGuid = Guid.NewGuid();
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("GuidValue", newGuid);
patchDoc.ApplyTo(doc);
Assert.Equal(newGuid, doc.GuidValue);
}
[Fact]
public void ReplaceGuidTestExpandoObjectInAnonymous()
{
dynamic nestedObject = new ExpandoObject();
nestedObject.GuidValue = Guid.NewGuid();
dynamic doc = new
{
NestedObject = nestedObject
};
var newGuid = Guid.NewGuid();
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("nestedobject/GuidValue", newGuid);
patchDoc.ApplyTo(doc);
Assert.Equal(newGuid, doc.NestedObject.GuidValue);
}
[Fact]
public void ReplaceNestedObjectTest()
{
dynamic doc = new ExpandoObject();
doc.SimpleDTO = new SimpleObject()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
};
var newDTO = new SimpleObject()
{
DoubleValue = 1
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("SimpleDTO", newDTO);
patchDoc.ApplyTo(doc);
Assert.Equal(1, doc.SimpleDTO.DoubleValue);
Assert.Equal(0, doc.SimpleDTO.IntegerValue);
Assert.Null(doc.SimpleDTO.IntegerList);
}
[Fact]
public void ReplaceInList()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList/0", 5);
patchDoc.ApplyTo(doc);
Assert.Equal(new List<int>() { 5, 2, 3 }, doc.IntegerList);
}
[Fact]
public void ReplaceFullList()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList", new List<int>() { 4, 5, 6 });
patchDoc.ApplyTo(doc);
Assert.Equal(new List<int>() { 4, 5, 6 }, doc.IntegerList);
}
[Fact]
public void ReplaceInListInList()
{
dynamic doc = new ExpandoObject();
doc.SimpleDTOList = new List<SimpleObject>() {
new SimpleObject() {
IntegerList = new List<int>(){1,2,3}
}};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("SimpleDTOList/0/IntegerList/0", 4);
patchDoc.ApplyTo(doc);
Assert.Equal(4, doc.SimpleDTOList[0].IntegerList[0]);
}
[Fact]
public void ReplaceInListInListAtEnd()
{
dynamic doc = new ExpandoObject();
doc.SimpleDTOList = new List<SimpleObject>() {
new SimpleObject() {
IntegerList = new List<int>(){1,2,3}
}};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("SimpleDTOList/0/IntegerList/-", 4);
patchDoc.ApplyTo(doc);
Assert.Equal(4, doc.SimpleDTOList[0].IntegerList[2]);
}
[Fact]
public void ReplaceFullListFromEnumerable()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList", new List<int>() { 4, 5, 6 });
patchDoc.ApplyTo(doc);
Assert.Equal(new List<int>() { 4, 5, 6 }, doc.IntegerList);
}
[Fact]
public void ReplaceFullListWithCollection()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList", new Collection<int>() { 4, 5, 6 });
patchDoc.ApplyTo(doc);
Assert.Equal(new List<int>() { 4, 5, 6 }, doc.IntegerList);
}
[Fact]
public void ReplaceAtEndOfList()
{
dynamic doc = new ExpandoObject();
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList/-", 5);
patchDoc.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 5 }, doc.IntegerList);
}
}
}

View File

@ -1,191 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Newtonsoft.Json;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class ReplaceTypedOperationTests
{
[Fact]
public void ReplaceGuidTest()
{
var doc = new SimpleObject()
{
GuidValue = Guid.NewGuid()
};
var newGuid = Guid.NewGuid();
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("GuidValue", newGuid);
// serialize & deserialize
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserizalized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserizalized.ApplyTo(doc);
Assert.Equal(newGuid, doc.GuidValue);
}
[Fact]
public void SerializeAndReplaceNestedObjectTest()
{
var doc = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
}
};
var newDTO = new SimpleObject()
{
DoubleValue = 1
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("SimpleObject", newDTO);
// serialize & deserialize
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(1, doc.SimpleObject.DoubleValue);
Assert.Equal(0, doc.SimpleObject.IntegerValue);
Assert.Null(doc.SimpleObject.IntegerList);
}
[Fact]
public void ReplaceInList()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList/0", 5);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 5, 2, 3 }, doc.IntegerList);
}
[Fact]
public void ReplaceFullList()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList", new List<int>() { 4, 5, 6 });
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 4, 5, 6 }, doc.IntegerList);
}
[Fact]
public void ReplaceInListInList()
{
var doc = new SimpleObject()
{
SimpleObjectList = new List<SimpleObject>() {
new SimpleObject() {
IntegerList = new List<int>(){1,2,3}
}}
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("SimpleObjectList/0/IntegerList/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(4, doc.SimpleObjectList.First().IntegerList.First());
}
[Fact]
public void ReplaceFullListFromEnumerable()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList", new List<int>() { 4, 5, 6 });
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 4, 5, 6 }, doc.IntegerList);
}
[Fact]
public void ReplaceFullListWithCollection()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList", new Collection<int>() { 4, 5, 6 });
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 4, 5, 6 }, doc.IntegerList);
}
[Fact]
public void ReplaceAtEndOfList()
{
var doc = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
// create patch
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList/-", 5);
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal(new List<int>() { 1, 2, 5 }, doc.IntegerList);
}
}
}

View File

@ -1,21 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class SimpleObject
{
public List<SimpleObject> SimpleObjectList { get; set; }
public List<int> IntegerList { get; set; }
public int IntegerValue { get; set; }
public string StringProperty { get; set; }
public string AnotherStringProperty { get; set; }
public decimal DecimalValue { get; set; }
public double DoubleValue { get; set; }
public float FloatValue { get; set; }
public Guid GuidValue { get; set; }
}
}

View File

@ -1,22 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class SimpleObjectWithNestedObject
{
public int IntegerValue { get; set; }
public NestedObject NestedObject { get; set; }
public SimpleObject SimpleObject { get; set; }
public List<SimpleObject> ListOfSimpleObject { get; set; }
public SimpleObjectWithNestedObject()
{
NestedObject = new NestedObject();
SimpleObject = new SimpleObject();
ListOfSimpleObject = new List<SimpleObject>();
}
}
}

View File

@ -0,0 +1,190 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests
{
public class AnonymousObjectIntegrationTest
{
[Fact]
public void AddNewProperty_ShouldFail()
{
// Arrange
var targetObject = new { };
var patchDocument = new JsonPatchDocument();
patchDocument.Add("NewProperty", 4);
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.ApplyTo(targetObject);
});
// Assert
Assert.Equal("The target location specified by path segment 'NewProperty' was not found.",
exception.Message);
}
[Fact]
public void AddNewProperty_ToNestedAnonymousObject_ShouldFail()
{
// Arrange
dynamic targetObject = new
{
Test = 1,
nested = new { }
};
var patchDocument = new JsonPatchDocument();
patchDocument.Add("Nested/NewInt", 1);
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.ApplyTo(targetObject);
});
// Assert
Assert.Equal("The target location specified by path segment 'NewInt' was not found.",
exception.Message);
}
[Fact]
public void AddDoesNotReplace()
{
// Arrange
var targetObject = new
{
StringProperty = "A"
};
var patchDocument = new JsonPatchDocument();
patchDocument.Add("StringProperty", "B");
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.ApplyTo(targetObject);
});
// Assert
Assert.Equal("The property at path 'StringProperty' could not be updated.",
exception.Message);
}
[Fact]
public void RemoveProperty_ShouldFail()
{
// Arrange
dynamic targetObject = new
{
Test = 1
};
var patchDocument = new JsonPatchDocument();
patchDocument.Remove("Test");
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.ApplyTo(targetObject);
});
// Assert
Assert.Equal("The property at path 'Test' could not be updated.",
exception.Message);
}
[Fact]
public void ReplaceProperty_ShouldFail()
{
// Arrange
var targetObject = new
{
StringProperty = "A",
AnotherStringProperty = "B"
};
var patchDocument = new JsonPatchDocument();
patchDocument.Replace("StringProperty", "AnotherStringProperty");
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.ApplyTo(targetObject);
});
// Assert
Assert.Equal("The property at path 'StringProperty' could not be updated.",
exception.Message);
}
[Fact]
public void MoveProperty_ShouldFail()
{
// Arrange
var targetObject = new
{
StringProperty = "A",
AnotherStringProperty = "B"
};
var patchDocument = new JsonPatchDocument();
patchDocument.Move("StringProperty", "AnotherStringProperty");
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.ApplyTo(targetObject);
});
// Assert
Assert.Equal("The property at path 'StringProperty' could not be updated.",
exception.Message);
}
[Fact]
public void TestStringProperty_IsSucessful()
{
// Arrange
var targetObject = new
{
StringProperty = "A",
AnotherStringProperty = "B"
};
var patchDocument = new JsonPatchDocument();
patchDocument.Test("StringProperty", "A");
// Act & Assert
patchDocument.ApplyTo(targetObject);
}
[Fact]
public void TestStringProperty_Fails()
{
// Arrange
var targetObject = new
{
StringProperty = "A",
AnotherStringProperty = "B"
};
var patchDocument = new JsonPatchDocument();
patchDocument.Test("StringProperty", "B");
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.ApplyTo(targetObject);
});
// Assert
Assert.Equal("The current value 'A' at path 'StringProperty' is not equal to the test value 'B'.",
exception.Message);
}
}
}

View File

@ -0,0 +1,319 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests
{
public class DictionaryTest
{
[Fact]
public void TestIntegerValue_IsSuccessful()
{
// Arrange
var model = new IntDictionary();
model.DictionaryOfStringToInteger["one"] = 1;
model.DictionaryOfStringToInteger["two"] = 2;
var patchDocument = new JsonPatchDocument();
patchDocument.Test("/DictionaryOfStringToInteger/two", 2);
// Act & Assert
patchDocument.ApplyTo(model);
}
[Fact]
public void AddIntegerValue_Succeeds()
{
// Arrange
var model = new IntDictionary();
model.DictionaryOfStringToInteger["one"] = 1;
model.DictionaryOfStringToInteger["two"] = 2;
var patchDocument = new JsonPatchDocument();
patchDocument.Add("/DictionaryOfStringToInteger/three", 3);
// Act
patchDocument.ApplyTo(model);
// Assert
Assert.Equal(3, model.DictionaryOfStringToInteger.Count);
Assert.Equal(1, model.DictionaryOfStringToInteger["one"]);
Assert.Equal(2, model.DictionaryOfStringToInteger["two"]);
Assert.Equal(3, model.DictionaryOfStringToInteger["three"]);
}
[Fact]
public void RemoveIntegerValue_Succeeds()
{
// Arrange
var model = new IntDictionary();
model.DictionaryOfStringToInteger["one"] = 1;
model.DictionaryOfStringToInteger["two"] = 2;
var patchDocument = new JsonPatchDocument();
patchDocument.Remove("/DictionaryOfStringToInteger/two");
// Act
patchDocument.ApplyTo(model);
// Assert
Assert.Equal(1, model.DictionaryOfStringToInteger.Count);
Assert.Equal(1, model.DictionaryOfStringToInteger["one"]);
}
[Fact]
public void MoveIntegerValue_Succeeds()
{
// Arrange
var model = new IntDictionary();
model.DictionaryOfStringToInteger["one"] = 1;
model.DictionaryOfStringToInteger["two"] = 2;
var patchDocument = new JsonPatchDocument();
patchDocument.Move("/DictionaryOfStringToInteger/one", "/DictionaryOfStringToInteger/two");
// Act
patchDocument.ApplyTo(model);
// Assert
Assert.Equal(1, model.DictionaryOfStringToInteger.Count);
Assert.Equal(1, model.DictionaryOfStringToInteger["two"]);
}
[Fact]
public void ReplaceIntegerValue_Succeeds()
{
// Arrange
var model = new IntDictionary();
model.DictionaryOfStringToInteger["one"] = 1;
model.DictionaryOfStringToInteger["two"] = 2;
var patchDocument = new JsonPatchDocument();
patchDocument.Replace("/DictionaryOfStringToInteger/two", 20);
// Act
patchDocument.ApplyTo(model);
// Assert
Assert.Equal(2, model.DictionaryOfStringToInteger.Count);
Assert.Equal(1, model.DictionaryOfStringToInteger["one"]);
Assert.Equal(20, model.DictionaryOfStringToInteger["two"]);
}
[Fact]
public void CopyIntegerValue_Succeeds()
{
// Arrange
var model = new IntDictionary();
model.DictionaryOfStringToInteger["one"] = 1;
model.DictionaryOfStringToInteger["two"] = 2;
var patchDocument = new JsonPatchDocument();
patchDocument.Copy("/DictionaryOfStringToInteger/one", "/DictionaryOfStringToInteger/two");
// Act
patchDocument.ApplyTo(model);
// Assert
Assert.Equal(2, model.DictionaryOfStringToInteger.Count);
Assert.Equal(1, model.DictionaryOfStringToInteger["one"]);
Assert.Equal(1, model.DictionaryOfStringToInteger["two"]);
}
private class Customer
{
public string Name { get; set; }
public Address Address { get; set; }
}
private class Address
{
public string City { get; set; }
}
private class IntDictionary
{
public IDictionary<string, int> DictionaryOfStringToInteger { get; } = new Dictionary<string, int>();
}
private class CustomerDictionary
{
public IDictionary<int, Customer> DictionaryOfStringToCustomer { get; } = new Dictionary<int, Customer>();
}
[Fact]
public void TestPocoObject_Succeeds()
{
// Arrange
var key1 = 100;
var value1 = new Customer() { Name = "James" };
var model = new CustomerDictionary();
model.DictionaryOfStringToCustomer[key1] = value1;
var patchDocument = new JsonPatchDocument();
patchDocument.Test($"/DictionaryOfStringToCustomer/{key1}/Name", "James");
// Act & Assert
patchDocument.ApplyTo(model);
}
[Fact]
public void TestPocoObject_FailsWhenTestValueIsNotEqualToObjectValue()
{
// Arrange
var key1 = 100;
var value1 = new Customer() { Name = "James" };
var model = new CustomerDictionary();
model.DictionaryOfStringToCustomer[key1] = value1;
var patchDocument = new JsonPatchDocument();
patchDocument.Test($"/DictionaryOfStringToCustomer/{key1}/Name", "Mike");
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.ApplyTo(model);
});
// Assert
Assert.Equal("The current value 'James' at path 'Name' is not equal to the test value 'Mike'.", exception.Message);
}
[Fact]
public void AddReplacesPocoObject_Succeeds()
{
// Arrange
var key1 = 100;
var value1 = new Customer() { Name = "Jamesss" };
var key2 = 200;
var value2 = new Customer() { Name = "Mike" };
var model = new CustomerDictionary();
model.DictionaryOfStringToCustomer[key1] = value1;
model.DictionaryOfStringToCustomer[key2] = value2;
var patchDocument = new JsonPatchDocument();
patchDocument.Add($"/DictionaryOfStringToCustomer/{key1}/Name", "James");
// Act
patchDocument.ApplyTo(model);
// Assert
Assert.Equal(2, model.DictionaryOfStringToCustomer.Count);
var actualValue1 = model.DictionaryOfStringToCustomer[key1];
Assert.NotNull(actualValue1);
Assert.Equal("James", actualValue1.Name);
}
[Fact]
public void RemovePocoObject_Succeeds()
{
// Arrange
var key1 = 100;
var value1 = new Customer() { Name = "Jamesss" };
var key2 = 200;
var value2 = new Customer() { Name = "Mike" };
var model = new CustomerDictionary();
model.DictionaryOfStringToCustomer[key1] = value1;
model.DictionaryOfStringToCustomer[key2] = value2;
var patchDocument = new JsonPatchDocument();
patchDocument.Remove($"/DictionaryOfStringToCustomer/{key1}/Name");
// Act
patchDocument.ApplyTo(model);
// Assert
var actualValue1 = model.DictionaryOfStringToCustomer[key1];
Assert.Null(actualValue1.Name);
}
[Fact]
public void MovePocoObject_Succeeds()
{
// Arrange
var key1 = 100;
var value1 = new Customer() { Name = "James" };
var key2 = 200;
var value2 = new Customer() { Name = "Mike" };
var model = new CustomerDictionary();
model.DictionaryOfStringToCustomer[key1] = value1;
model.DictionaryOfStringToCustomer[key2] = value2;
var patchDocument = new JsonPatchDocument();
patchDocument.Move($"/DictionaryOfStringToCustomer/{key1}/Name", $"/DictionaryOfStringToCustomer/{key2}/Name");
// Act
patchDocument.ApplyTo(model);
// Assert
var actualValue2 = model.DictionaryOfStringToCustomer[key2];
Assert.NotNull(actualValue2);
Assert.Equal("James", actualValue2.Name);
}
[Fact]
public void CopyPocoObject_Succeeds()
{
// Arrange
var key1 = 100;
var value1 = new Customer() { Name = "James" };
var key2 = 200;
var value2 = new Customer() { Name = "Mike" };
var model = new CustomerDictionary();
model.DictionaryOfStringToCustomer[key1] = value1;
model.DictionaryOfStringToCustomer[key2] = value2;
var patchDocument = new JsonPatchDocument();
patchDocument.Copy($"/DictionaryOfStringToCustomer/{key1}/Name", $"/DictionaryOfStringToCustomer/{key2}/Name");
// Act
patchDocument.ApplyTo(model);
// Assert
Assert.Equal(2, model.DictionaryOfStringToCustomer.Count);
var actualValue2 = model.DictionaryOfStringToCustomer[key2];
Assert.NotNull(actualValue2);
Assert.Equal("James", actualValue2.Name);
}
[Fact]
public void ReplacePocoObject_Succeeds()
{
// Arrange
var key1 = 100;
var value1 = new Customer() { Name = "Jamesss" };
var key2 = 200;
var value2 = new Customer() { Name = "Mike" };
var model = new CustomerDictionary();
model.DictionaryOfStringToCustomer[key1] = value1;
model.DictionaryOfStringToCustomer[key2] = value2;
var patchDocument = new JsonPatchDocument();
patchDocument.Replace($"/DictionaryOfStringToCustomer/{key1}/Name", "James");
// Act
patchDocument.ApplyTo(model);
// Assert
Assert.Equal(2, model.DictionaryOfStringToCustomer.Count);
var actualValue1 = model.DictionaryOfStringToCustomer[key1];
Assert.NotNull(actualValue1);
Assert.Equal("James", actualValue1.Name);
}
[Fact]
public void ReplacePocoObject_WithEscaping_Succeeds()
{
// Arrange
var key1 = "Foo/Name";
var value1 = 100;
var key2 = "Foo";
var value2 = 200;
var model = new IntDictionary();
model.DictionaryOfStringToInteger[key1] = value1;
model.DictionaryOfStringToInteger[key2] = value2;
var patchDocument = new JsonPatchDocument();
patchDocument.Replace($"/DictionaryOfStringToInteger/Foo~1Name", 300);
// Act
patchDocument.ApplyTo(model);
// Assert
Assert.Equal(2, model.DictionaryOfStringToInteger.Count);
var actualValue1 = model.DictionaryOfStringToInteger[key1];
var actualValue2 = model.DictionaryOfStringToInteger[key2];
Assert.Equal(300, actualValue1);
Assert.Equal(200, actualValue2);
}
}
}

View File

@ -5,9 +5,9 @@ using System.Collections.Generic;
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Internal
namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests
{
public class DynamicObjectTest
public class DynamicObjectIntegrationTest
{
[Fact]
public void AddResults_ShouldReplaceExistingPropertyValue_InNestedDynamicObject()
@ -20,11 +20,11 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
dynamicTestObject.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond = new DynamicTestObject();
dynamicTestObject.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond.StringProperty = "A";
var patchDoc = new JsonPatchDocument();
patchDoc.Add("/Nested/DynamicProperty/InBetweenFirst/InBetweenSecond/StringProperty", "B");
var patchDocument = new JsonPatchDocument();
patchDocument.Add("/Nested/DynamicProperty/InBetweenFirst/InBetweenSecond/StringProperty", "B");
// Act
patchDoc.ApplyTo(dynamicTestObject);
patchDocument.ApplyTo(dynamicTestObject);
// Assert
Assert.Equal("B", dynamicTestObject.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond.StringProperty);
@ -53,21 +53,17 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
DynamicProperty = new DynamicTestObject()
};
var patchDoc = new JsonPatchDocument();
patchDoc.Add("DynamicProperty/OtherProperty/IntProperty", 1);
var patchDocument = new JsonPatchDocument();
patchDocument.Add("DynamicProperty/OtherProperty/IntProperty", 1);
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDoc.ApplyTo(nestedObject);
patchDocument.ApplyTo(nestedObject);
});
// Assert
Assert.Equal(
string.Format(
"The target location specified by path segment '{0}' was not found.",
"OtherProperty"),
exception.Message);
Assert.Equal("The target location specified by path segment 'OtherProperty' was not found.", exception.Message);
}
[Fact]
@ -79,11 +75,11 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
dynamicTestObject.NestedDynamicObject.StringProperty = "A";
dynamicTestObject.NestedDynamicObject.AnotherStringProperty = "B";
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("NestedDynamicObject/StringProperty", "NestedDynamicObject/AnotherStringProperty");
var patchDocument = new JsonPatchDocument();
patchDocument.Copy("NestedDynamicObject/StringProperty", "NestedDynamicObject/AnotherStringProperty");
// Act
patchDoc.ApplyTo(dynamicTestObject);
patchDocument.ApplyTo(dynamicTestObject);
// Assert
Assert.Equal("A", dynamicTestObject.NestedDynamicObject.AnotherStringProperty);
@ -97,11 +93,11 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
dynamic dynamicTestObject = new DynamicTestObject();
dynamicTestObject.StringProperty = "A";
var patchDoc = new JsonPatchDocument();
patchDoc.Move("StringProperty", "AnotherStringProperty");
var patchDocument = new JsonPatchDocument();
patchDocument.Move("StringProperty", "AnotherStringProperty");
// Act
patchDoc.ApplyTo(dynamicTestObject);
patchDocument.ApplyTo(dynamicTestObject);
dynamicTestObject.TryGetValue("StringProperty", out object valueFromDictionary);
// Assert
@ -117,11 +113,11 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
dynamicTestObject.StringProperty = "A";
dynamicTestObject.SimpleObject = new SimpleObject() { AnotherStringProperty = "B" };
var patchDoc = new JsonPatchDocument();
patchDoc.Move("StringProperty", "SimpleObject/AnotherStringProperty");
var patchDocument = new JsonPatchDocument();
patchDocument.Move("StringProperty", "SimpleObject/AnotherStringProperty");
// Act
patchDoc.ApplyTo(dynamicTestObject);
patchDocument.ApplyTo(dynamicTestObject);
dynamicTestObject.TryGetValue("StringProperty", out object valueFromDictionary);
// Assert
@ -129,27 +125,6 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
Assert.Null(valueFromDictionary);
}
[Fact]
public void MovePropertyValue_FromListToNonList_InNestedTypedObject_InDynamicObject()
{
// Arrange
dynamic dynamicTestObject = new DynamicTestObject();
dynamicTestObject.Nested = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
var patchDoc = new JsonPatchDocument();
patchDoc.Move("Nested/IntegerList/0", "Nested/IntegerValue");
// Act
patchDoc.ApplyTo(dynamicTestObject);
// Assert
Assert.Equal(new List<int>() { 2, 3 }, dynamicTestObject.Nested.IntegerList);
Assert.Equal(1, dynamicTestObject.Nested.IntegerValue);
}
[Fact]
public void RemoveNestedProperty_FromDynamicObject()
{
@ -158,11 +133,11 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
dynamicTestObject.Test = new DynamicTestObject();
dynamicTestObject.Test.AnotherTest = "A";
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("Test");
var patchDocument = new JsonPatchDocument();
patchDocument.Remove("Test");
// Act
patchDoc.ApplyTo(dynamicTestObject);
patchDocument.ApplyTo(dynamicTestObject);
dynamicTestObject.TryGetValue("Test", out object valueFromDictionary);
// Assert
@ -179,41 +154,19 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
StringProperty = "A"
};
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("Simpleobject/stringProperty");
var patchDocument = new JsonPatchDocument();
patchDocument.Remove("Simpleobject/stringProperty");
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDoc.ApplyTo(dynamicTestObject);
patchDocument.ApplyTo(dynamicTestObject);
});
// Assert
Assert.Equal(
string.Format("The target location specified by path segment '{0}' was not found.",
"Simpleobject"),
exception.Message);
Assert.Equal("The target location specified by path segment 'Simpleobject' was not found.", exception.Message);
}
[Fact]
public void RemoveFromList_NestedInDynamicObject()
{
// Arrange
dynamic dynamicTestObject = new DynamicTestObject();
dynamicTestObject.SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleObject/IntegerList/2");
// Act
patchDoc.ApplyTo(dynamicTestObject);
// Assert
Assert.Equal(new List<int>() { 1, 2 }, dynamicTestObject.SimpleObject.IntegerList);
}
[Fact]
public void ReplaceNestedTypedObject_InDynamicObject()
@ -231,11 +184,11 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
DoubleValue = 1
};
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("SimpleObject", newObject);
var patchDocument = new JsonPatchDocument();
patchDocument.Replace("SimpleObject", newObject);
// Act
patchDoc.ApplyTo(dynamicTestObject);
patchDocument.ApplyTo(dynamicTestObject);
// Assert
Assert.Equal(1, dynamicTestObject.SimpleObject.DoubleValue);
@ -244,41 +197,21 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
}
[Fact]
public void ReplaceFullList_InDynamicObject()
public void TestStringPropertyValue_IsSuccessful()
{
// Arrange
dynamic dynamicTestObject = new DynamicTestObject();
dynamicTestObject.IntegerList = new List<int>() { 1, 2, 3 };
dynamicTestObject.Property = "A";
var patchDoc = new JsonPatchDocument();
patchDoc.Replace("IntegerList", new List<int>() { 4, 5, 6 });
// Act
patchDoc.ApplyTo(dynamicTestObject);
// Assert
Assert.Equal(new List<int>() { 4, 5, 6 }, dynamicTestObject.IntegerList);
}
[Fact]
public void TestPropertyValue_FromListToNonList_InNestedTypedObject_InDynamicObject()
{
// Arrange
dynamic dynamicTestObject = new DynamicTestObject();
dynamicTestObject.Nested = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
var patchDoc = new JsonPatchDocument();
patchDoc.Test("Nested/IntegerList/1", 2);
var patchDocument = new JsonPatchDocument();
patchDocument.Test("Property", "A");
// Act & Assert
patchDoc.ApplyTo(dynamicTestObject);
patchDocument.ApplyTo(dynamicTestObject);
}
[Fact]
public void TestPropertyValue_FromListToNonList_InNestedTypedObject_InDynamicObject_ThrowsJsonPatchException_IfTestFails()
public void TestIntegerPropertyValue_ThrowsJsonPatchException_IfTestFails()
{
// Arrange
dynamic dynamicTestObject = new DynamicTestObject();
@ -287,13 +220,13 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
IntegerList = new List<int>() { 1, 2, 3 }
};
var patchDoc = new JsonPatchDocument();
patchDoc.Test("Nested/IntegerList/0", 2);
var patchDocument = new JsonPatchDocument();
patchDocument.Test("Nested/IntegerList/0", 2);
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDoc.ApplyTo(dynamicTestObject);
patchDocument.ApplyTo(dynamicTestObject);
});
// Assert

View File

@ -0,0 +1,327 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Dynamic;
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests
{
public class ExpandoObjectIntegrationTest
{
[Fact]
public void AddNewIntProperty()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.Test = 1;
var patchDocument = new JsonPatchDocument();
patchDocument.Add("NewInt", 1);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(1, targetObject.NewInt);
Assert.Equal(1, targetObject.Test);
}
[Fact]
public void AddNewProperty_ToTypedObject_InExpandoObject()
{
// Arrange
dynamic dynamicProperty = new ExpandoObject();
dynamicProperty.StringProperty = "A";
var targetObject = new NestedObject()
{
DynamicProperty = dynamicProperty
};
var patchDocument = new JsonPatchDocument();
patchDocument.Add("DynamicProperty/StringProperty", "B");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal("B", targetObject.DynamicProperty.StringProperty);
}
[Fact]
public void AddReplaces_ExistingProperty()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.StringProperty = "A";
var patchDocument = new JsonPatchDocument();
patchDocument.Add("StringProperty", "B");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal("B", targetObject.StringProperty);
}
[Fact]
public void AddReplaces_ExistingProperty_InNestedExpandoObject()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.InBetweenFirst = new ExpandoObject();
targetObject.InBetweenFirst.InBetweenSecond = new ExpandoObject();
targetObject.InBetweenFirst.InBetweenSecond.StringProperty = "A";
var patchDocument = new JsonPatchDocument();
patchDocument.Add("/InBetweenFirst/InBetweenSecond/StringProperty", "B");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal("B", targetObject.InBetweenFirst.InBetweenSecond.StringProperty);
}
[Fact]
public void ShouldNotReplaceProperty_WithDifferentCase()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.StringProperty = "A";
var patchDocument = new JsonPatchDocument();
patchDocument.Add("stringproperty", "B");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal("A", targetObject.StringProperty);
Assert.Equal("B", targetObject.stringproperty);
}
[Fact]
public void TestIntegerProperty_IsSucessful()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.Test = 1;
var patchDocument = new JsonPatchDocument();
patchDocument.Test("Test", 1);
// Act & Assert
patchDocument.ApplyTo(targetObject);
}
[Fact]
public void TestStringProperty_ThrowsJsonPatchException_IfTestFails()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.Test = "Value";
var patchDocument = new JsonPatchDocument();
patchDocument.Test("Test", "TestValue");
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.ApplyTo(targetObject);
});
// Assert
Assert.Equal("The current value 'Value' at path 'Test' is not equal to the test value 'TestValue'.",
exception.Message);
}
[Fact]
public void CopyStringProperty_ToAnotherStringProperty()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.StringProperty = "A";
targetObject.AnotherStringProperty = "B";
var patchDocument = new JsonPatchDocument();
patchDocument.Copy("StringProperty", "AnotherStringProperty");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal("A", targetObject.AnotherStringProperty);
}
[Fact]
public void MoveIntegerValue_ToAnotherIntegerProperty()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.IntegerValue = 100;
targetObject.AnotherIntegerValue = 200;
var patchDocument = new JsonPatchDocument();
patchDocument.Move("IntegerValue", "AnotherIntegerValue");
// Act
patchDocument.ApplyTo(targetObject);
Assert.Equal(100, targetObject.AnotherIntegerValue);
var cont = targetObject as IDictionary<string, object>;
cont.TryGetValue("IntegerValue", out object valueFromDictionary);
// Assert
Assert.Null(valueFromDictionary);
}
[Fact]
public void Move_ToNonExistingProperty()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.StringProperty = "A";
var patchDocument = new JsonPatchDocument();
patchDocument.Move("StringProperty", "AnotherStringProperty");
// Act
patchDocument.ApplyTo(targetObject);
Assert.Equal("A", targetObject.AnotherStringProperty);
var cont = targetObject as IDictionary<string, object>;
cont.TryGetValue("StringProperty", out var valueFromDictionary);
// Assert
Assert.Null(valueFromDictionary);
}
[Fact]
public void RemoveProperty_ShouldFail_IfItDoesntExist()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.Test = 1;
var patchDocument = new JsonPatchDocument();
patchDocument.Remove("NonExisting");
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.ApplyTo(targetObject);
});
// Assert
Assert.Equal("The target location specified by path segment 'NonExisting' was not found.", exception.Message);
}
[Fact]
public void RemoveStringProperty()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.Test = 1;
var patchDocument = new JsonPatchDocument();
patchDocument.Remove("Test");
// Act
patchDocument.ApplyTo(targetObject);
var cont = targetObject as IDictionary<string, object>;
cont.TryGetValue("Test", out object valueFromDictionary);
// Assert
Assert.Null(valueFromDictionary);
}
[Fact]
public void RemoveProperty_MixedCase_ThrowsPathNotFoundException()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.Test = 1;
var patchDocument = new JsonPatchDocument();
patchDocument.Remove("test");
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.ApplyTo(targetObject);
});
// Assert
Assert.Equal("The target location specified by path segment 'test' was not found.", exception.Message);
}
[Fact]
public void RemoveNestedProperty()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.Test = new ExpandoObject();
targetObject.Test.AnotherTest = "A";
var patchDocument = new JsonPatchDocument();
patchDocument.Remove("Test");
// Act
patchDocument.ApplyTo(targetObject);
var cont = targetObject as IDictionary<string, object>;
cont.TryGetValue("Test", out object valueFromDictionary);
// Assert
Assert.Null(valueFromDictionary);
}
[Fact]
public void RemoveNestedProperty_MixedCase_ThrowsPathNotFoundException()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.Test = new ExpandoObject();
targetObject.Test.AnotherTest = "A";
var patchDocument = new JsonPatchDocument();
patchDocument.Remove("test");
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.ApplyTo(targetObject);
});
// Assert
Assert.Equal("The target location specified by path segment 'test' was not found.", exception.Message);
}
[Fact]
public void ReplaceGuid()
{
// Arrange
dynamic targetObject = new ExpandoObject();
targetObject.GuidValue = Guid.NewGuid();
var newGuid = Guid.NewGuid();
var patchDocument = new JsonPatchDocument();
patchDocument.Replace("GuidValue", newGuid);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(newGuid, targetObject.GuidValue);
}
}
}

View File

@ -0,0 +1,366 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests
{
public class ListIntegrationTest
{
[Fact]
public void TestInList_IsSuccessful()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Test(o => o.SimpleObject.IntegerList, 3, 2);
// Act & Assert
patchDocument.ApplyTo(targetObject);
}
[Fact]
public void TestInList_InvalidPosition()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Test(o => o.SimpleObject.IntegerList, 4, -1);
// Act & Assert
var exception = Assert.Throws<JsonPatchException>(() => { patchDocument.ApplyTo(targetObject); });
Assert.Equal("The index value provided by path segment '-1' is out of bounds of the array size.",
exception.Message);
}
[Fact]
public void AddToIntegerIList()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerIList = new List<int>() { 1, 2, 3 }
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Add(o => (List<int>)o.SimpleObject.IntegerIList, 4, 0);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(new List<int>() { 4, 1, 2, 3 }, targetObject.SimpleObject.IntegerIList);
}
[Fact]
public void AddToComplextTypeList_SpecifyIndex()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObjectList = new List<SimpleObject>()
{
new SimpleObject
{
StringProperty = "String1"
},
new SimpleObject
{
StringProperty = "String2"
}
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Add(o => o.SimpleObjectList[0].StringProperty, "ChangedString1");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal("ChangedString1", targetObject.SimpleObjectList[0].StringProperty);
}
[Fact]
public void AddToListAppend()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Add(o => o.SimpleObject.IntegerList, 4);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(new List<int>() { 1, 2, 3, 4 }, targetObject.SimpleObject.IntegerList);
}
[Fact]
public void RemoveFromList()
{
// Arrange
var targetObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
var patchDocument = new JsonPatchDocument();
patchDocument.Remove("IntegerList/2");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(new List<int>() { 1, 2 }, targetObject.IntegerList);
}
[Theory]
[InlineData("3")]
[InlineData("-1")]
public void RemoveFromList_InvalidPosition(string position)
{
// Arrange
var targetObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
var patchDocument = new JsonPatchDocument();
patchDocument.Remove("IntegerList/" + position);
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.ApplyTo(targetObject);
});
// Assert
Assert.Equal($"The index value provided by path segment '{position}' is out of bounds of the array size.", exception.Message);
}
[Fact]
public void Remove_FromEndOfList()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Remove<int>(o => o.SimpleObject.IntegerList);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(new List<int>() { 1, 2 }, targetObject.SimpleObject.IntegerList);
}
[Fact]
public void ReplaceFullList_WithCollection()
{
// Arrange
var targetObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
var patchDocument = new JsonPatchDocument();
patchDocument.Replace("IntegerList", new Collection<int>() { 4, 5, 6 });
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(new List<int>() { 4, 5, 6 }, targetObject.IntegerList);
}
[Fact]
public void Replace_AtEndOfList()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Replace(o => o.SimpleObject.IntegerList, 5);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(new List<int>() { 1, 2, 5 }, targetObject.SimpleObject.IntegerList);
}
[Fact]
public void Replace_InList_InvalidPosition()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Replace(o => o.SimpleObject.IntegerList, 5, -1);
// Act
var exception = Assert.Throws<JsonPatchException>(() => { patchDocument.ApplyTo(targetObject); });
// Assert
Assert.Equal("The index value provided by path segment '-1' is out of bounds of the array size.", exception.Message);
}
[Fact]
public void CopyFromListToEndOfList()
{
// Arrange
var targetObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
var patchDocument = new JsonPatchDocument();
patchDocument.Copy("IntegerList/0", "IntegerList/-");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(new List<int>() { 1, 2, 3, 1 }, targetObject.IntegerList);
}
[Fact]
public void CopyFromListToNonList()
{
// Arrange
var targetObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
};
var patchDocument = new JsonPatchDocument();
patchDocument.Copy("IntegerList/0", "IntegerValue");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(1, targetObject.IntegerValue);
}
[Fact]
public void MoveToEndOfList()
{
// Arrange
var targetObject = new SimpleObject()
{
IntegerValue = 5,
IntegerList = new List<int>() { 1, 2, 3 }
};
var patchDocument = new JsonPatchDocument();
patchDocument.Move("IntegerValue", "IntegerList/-");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(0, targetObject.IntegerValue);
Assert.Equal(new List<int>() { 1, 2, 3, 5 }, targetObject.IntegerList);
}
[Fact]
public void Move_KeepsObjectReferenceInList()
{
// Arrange
var simpleObject1 = new SimpleObject() { IntegerValue = 1 };
var simpleObject2 = new SimpleObject() { IntegerValue = 2 };
var simpleObject3 = new SimpleObject() { IntegerValue = 3 };
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObjectList = new List<SimpleObject>() {
simpleObject1,
simpleObject2,
simpleObject3
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Move(o => o.SimpleObjectList, 0, o => o.SimpleObjectList, 1);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(new List<SimpleObject>() { simpleObject2, simpleObject1, simpleObject3 }, targetObject.SimpleObjectList);
Assert.Equal(2, targetObject.SimpleObjectList[0].IntegerValue);
Assert.Equal(1, targetObject.SimpleObjectList[1].IntegerValue);
Assert.Same(simpleObject2, targetObject.SimpleObjectList[0]);
Assert.Same(simpleObject1, targetObject.SimpleObjectList[1]);
}
[Fact]
public void MoveFromList_ToNonList_BetweenHierarchy()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerList = new List<int>() { 1, 2, 3 }
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Move(o => o.SimpleObject.IntegerList, 0, o => o.IntegerValue);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(new List<int>() { 2, 3 }, targetObject.SimpleObject.IntegerList);
Assert.Equal(1, targetObject.IntegerValue);
}
}
}

View File

@ -0,0 +1,342 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Dynamic;
using Newtonsoft.Json;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests
{
public class NestedObjectIntegrationTest
{
[Fact]
public void Replace_DTOWithNullCheck()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObjectWithNullCheck()
{
SimpleObjectWithNullCheck = new SimpleObjectWithNullCheck()
{
StringProperty = "A"
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObjectWithNullCheck>();
patchDocument.Replace(o => o.SimpleObjectWithNullCheck.StringProperty, "B");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal("B", targetObject.SimpleObjectWithNullCheck.StringProperty);
}
[Fact]
public void ReplaceNestedObject_WithSerialization()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
IntegerValue = 1
};
var newNested = new NestedObject() { StringProperty = "B" };
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Replace(o => o.NestedObject, newNested);
var serialized = JsonConvert.SerializeObject(patchDocument);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleObjectWithNestedObject>>(serialized);
// Act
deserialized.ApplyTo(targetObject);
// Assert
Assert.Equal("B", targetObject.NestedObject.StringProperty);
}
[Fact]
public void TestStringProperty_InNestedObject()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
NestedObject = new NestedObject() { StringProperty = "A"}
};
var patchDocument = new JsonPatchDocument<NestedObject>();
patchDocument.Test(o => o.StringProperty, "A");
// Act
patchDocument.ApplyTo(targetObject.NestedObject);
// Assert
Assert.Equal("A", targetObject.NestedObject.StringProperty);
}
[Fact]
public void TestNestedObject()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
NestedObject = new NestedObject() { StringProperty = "B"}
};
var testNested = new NestedObject() { StringProperty = "B" };
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Test(o => o.NestedObject, testNested);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal("B", targetObject.NestedObject.StringProperty);
}
[Fact]
public void AddReplaces_ExistingStringProperty()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
StringProperty = "A"
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Add(o => o.SimpleObject.StringProperty, "B");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal("B", targetObject.SimpleObject.StringProperty);
}
[Fact]
public void AddNewProperty_ToExpandoOject_InTypedObject()
{
var targetObject = new NestedObject()
{
DynamicProperty = new ExpandoObject()
};
var patchDocument = new JsonPatchDocument();
patchDocument.Add("DynamicProperty/NewInt", 1);
patchDocument.ApplyTo(targetObject);
Assert.Equal(1, targetObject.DynamicProperty.NewInt);
}
[Fact]
public void RemoveStringProperty()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
StringProperty = "A"
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Remove(o => o.SimpleObject.StringProperty);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Null(targetObject.SimpleObject.StringProperty);
}
[Fact]
public void CopyStringProperty_ToAnotherStringProperty()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
StringProperty = "A",
AnotherStringProperty = "B"
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Copy(o => o.SimpleObject.StringProperty, o => o.SimpleObject.AnotherStringProperty);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal("A", targetObject.SimpleObject.AnotherStringProperty);
}
[Fact]
public void Copy_DeepClonesObject()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
StringProperty = "A",
AnotherStringProperty = "B"
},
InheritedObject = new InheritedObject()
{
StringProperty = "C",
AnotherStringProperty = "D"
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Copy(o => o.InheritedObject, o => o.SimpleObject);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal("C", targetObject.SimpleObject.StringProperty);
Assert.Equal("D", targetObject.SimpleObject.AnotherStringProperty);
Assert.Equal("C", targetObject.InheritedObject.StringProperty);
Assert.Equal("D", targetObject.InheritedObject.AnotherStringProperty);
Assert.NotSame(targetObject.SimpleObject.StringProperty, targetObject.InheritedObject.StringProperty);
}
[Fact]
public void Copy_KeepsObjectType()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject(),
InheritedObject = new InheritedObject()
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Copy(o => o.InheritedObject, o => o.SimpleObject);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(typeof(InheritedObject), targetObject.SimpleObject.GetType());
}
[Fact]
public void Copy_BreaksObjectReference()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject(),
InheritedObject = new InheritedObject()
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Copy(o => o.InheritedObject, o => o.SimpleObject);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.NotSame(targetObject.SimpleObject, targetObject.InheritedObject);
}
[Fact]
public void MoveIntegerValue_ToAnotherIntegerProperty()
{
// Arrange
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = new SimpleObject()
{
IntegerValue = 2,
AnotherIntegerValue = 3
}
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Move(o => o.SimpleObject.IntegerValue, o => o.SimpleObject.AnotherIntegerValue);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(2, targetObject.SimpleObject.AnotherIntegerValue);
Assert.Equal(0, targetObject.SimpleObject.IntegerValue);
}
[Fact]
public void Move_KeepsObjectReference()
{
// Arrange
var sDto = new SimpleObject()
{
StringProperty = "A",
AnotherStringProperty = "B"
};
var iDto = new InheritedObject()
{
StringProperty = "C",
AnotherStringProperty = "D"
};
var targetObject = new SimpleObjectWithNestedObject()
{
SimpleObject = sDto,
InheritedObject = iDto
};
var patchDocument = new JsonPatchDocument<SimpleObjectWithNestedObject>();
patchDocument.Move(o => o.InheritedObject, o => o.SimpleObject);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal("C", targetObject.SimpleObject.StringProperty);
Assert.Equal("D", targetObject.SimpleObject.AnotherStringProperty);
Assert.Same(iDto, targetObject.SimpleObject);
Assert.Null(targetObject.InheritedObject);
}
private class SimpleObjectWithNullCheck
{
private string stringProperty;
public string StringProperty
{
get
{
return stringProperty;
}
set
{
if (value == null)
{
throw new ArgumentNullException();
}
stringProperty = value;
}
}
}
private class SimpleObjectWithNestedObjectWithNullCheck
{
public SimpleObjectWithNullCheck SimpleObjectWithNullCheck { get; set; }
public SimpleObjectWithNestedObjectWithNullCheck()
{
SimpleObjectWithNullCheck = new SimpleObjectWithNullCheck();
}
}
}
}

View File

@ -0,0 +1,128 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.IntegrationTests
{
public class SimpleObjectIntegrationTest
{
[Fact]
public void TestDoubleValueProperty()
{
// Arrange
var targetObject = new SimpleObject()
{
DoubleValue = 9.8
};
var patchDocument = new JsonPatchDocument();
patchDocument.Test("DoubleValue", 9.8);
// Act & Assert
patchDocument.ApplyTo(targetObject);
}
[Fact]
public void CopyStringProperty_ToAnotherStringProperty()
{
// Arrange
var targetObject = new SimpleObject()
{
StringProperty = "A",
AnotherStringProperty = "B"
};
var patchDocument = new JsonPatchDocument();
patchDocument.Copy("StringProperty", "AnotherStringProperty");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal("A", targetObject.AnotherStringProperty);
}
[Fact]
public void MoveIntegerProperty_ToAnotherIntegerProperty()
{
// Arrange
var targetObject = new SimpleObject()
{
IntegerValue = 2,
AnotherIntegerValue = 3
};
var patchDocument = new JsonPatchDocument();
patchDocument.Move("IntegerValue", "AnotherIntegerValue");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(2, targetObject.AnotherIntegerValue);
Assert.Equal(0, targetObject.IntegerValue);
}
[Fact]
public void RemoveDecimalPropertyValue()
{
// Arrange
var targetObject = new SimpleObject()
{
DecimalValue = 9.8M
};
var patchDocument = new JsonPatchDocument();
patchDocument.Remove("DecimalValue");
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(0, targetObject.DecimalValue);
}
[Fact]
public void ReplaceGuid()
{
// Arrange
var targetObject = new SimpleObject()
{
GuidValue = Guid.NewGuid()
};
var newGuid = Guid.NewGuid();
var patchDocument = new JsonPatchDocument();
patchDocument.Replace("GuidValue", newGuid);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(newGuid, targetObject.GuidValue);
}
[Fact]
public void AddReplacesGuid()
{
// Arrange
var targetObject = new SimpleObject()
{
GuidValue = Guid.NewGuid()
};
var newGuid = Guid.NewGuid();
var patchDocument = new JsonPatchDocument();
patchDocument.Add("GuidValue", newGuid);
// Act
patchDocument.ApplyTo(targetObject);
// Assert
Assert.Equal(newGuid, targetObject.GuidValue);
}
}
}

View File

@ -74,9 +74,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(getStatus);
Assert.Equal(
string.Format("The provided path segment '{0}' cannot be converted to the target type.", guidKey.ToString()),
message);
Assert.Equal($"The provided path segment '{guidKey.ToString()}' cannot be converted to the target type.", message);
Assert.Null(outValue);
}
@ -103,9 +101,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(getStatus);
Assert.Equal(
string.Format("The target location specified by path segment '{0}' was not found.", nameKey.ToUpper()),
message);
Assert.Equal("The target location specified by path segment 'NAME' was not found.", message);
Assert.Null(outValue);
}
@ -191,9 +187,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(replaceStatus);
Assert.Equal(
string.Format("The value '{0}' is invalid for target location.", "test"),
message);
Assert.Equal("The value 'test' is invalid for target location.", message);
Assert.Equal(5, dictionary[guidKey]);
}
@ -211,9 +205,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(replaceStatus);
Assert.Equal(
string.Format("The target location specified by path segment '{0}' was not found.", nameKey),
message);
Assert.Equal("The target location specified by path segment 'Name' was not found.", message);
Assert.Empty(dictionary);
}
@ -231,9 +223,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(removeStatus);
Assert.Equal(
string.Format("The target location specified by path segment '{0}' was not found.", nameKey),
message);
Assert.Equal("The target location specified by path segment 'Name' was not found.", message);
Assert.Empty(dictionary);
}
@ -280,13 +270,19 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
{
// Arrange
var key = "Name";
var dictionary = new Dictionary<string, object>();
dictionary[key] = "James";
var dictionaryAdapter = new DictionaryAdapter<string, object>();
var dictionary = new Dictionary<string, List<object>>();
var value = new List<object>()
{
"James",
2,
new Customer("James", 25)
};
dictionary[key] = value;
var dictionaryAdapter = new DictionaryAdapter<string, List<object>>();
var resolver = new DefaultContractResolver();
// Act
var testStatus = dictionaryAdapter.TryTest(dictionary, key, resolver, "James", out var message);
var testStatus = dictionaryAdapter.TryTest(dictionary, key, resolver, value, out var message);
//Assert
Assert.True(testStatus);

View File

@ -233,15 +233,21 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
{
var adapter = new DynamicObjectAdapter();
dynamic target = new DynamicTestObject();
target.NewProperty = "Joana";
var value = new List<object>()
{
"Joana",
2,
new Customer("Joana", 25)
};
target.NewProperty = value;
var segment = "NewProperty";
var resolver = new DefaultContractResolver();
// Act
var testStatus = adapter.TryTest(target, segment, resolver, "Joana", out string errorMessage);
var testStatus = adapter.TryTest(target, segment, resolver, value, out string errorMessage);
// Assert
Assert.Equal("Joana", target.NewProperty);
Assert.Equal(value, target.NewProperty);
Assert.True(testStatus);
Assert.True(string.IsNullOrEmpty(errorMessage), "Expected no error message");
}

View File

@ -24,11 +24,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(addStatus);
Assert.Equal(
string.Format(
"The type '{0}' which is an array is not supported for json patch operations as it has a fixed size.",
targetObject.GetType().FullName),
message);
Assert.Equal($"The type '{targetObject.GetType().FullName}' which is an array is not supported for json patch operations as it has a fixed size.", message);
}
[Fact]
@ -46,11 +42,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(addStatus);
Assert.Equal(
string.Format(
"The type '{0}' which is a non generic list is not supported for json patch operations. Only generic list types are supported.",
targetObject.GetType().FullName),
message);
Assert.Equal($"The type '{targetObject.GetType().FullName}' which is a non generic list is not supported for json patch operations. Only generic list types are supported.", message);
}
[Fact]
@ -88,9 +80,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(addStatus);
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", position),
message);
Assert.Equal($"The index value provided by path segment '{position}' is out of bounds of the array size.", message);
}
[Theory]
@ -108,9 +98,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(addStatus);
Assert.Equal(
string.Format("The path segment '{0}' is invalid for an array index.", position),
message);
Assert.Equal($"The path segment '{position}' is invalid for an array index.", message);
}
public static TheoryData<List<int>, List<int>> AppendAtEndOfListData
@ -200,7 +188,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(addStatus);
Assert.Equal(string.Format("The value '{0}' is invalid for target location.", "James"), message);
Assert.Equal("The value 'James' is invalid for target location.", message);
}
public static TheoryData<IList, object, string, IList> AddingDifferentComplexTypeWorksData
@ -255,8 +243,10 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
Assert.Equal(expected, targetObject);
}
public static TheoryData<IList, object, string, IList> AddingKeepsObjectReferenceData {
get {
public static TheoryData<IList, object, string, IList> AddingKeepsObjectReferenceData
{
get
{
var sDto1 = new SimpleObject();
var sDto2 = new SimpleObject();
var sDto3 = new SimpleObject();
@ -324,9 +314,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(getStatus);
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", position),
message);
Assert.Equal($"The index value provided by path segment '{position}' is out of bounds of the array size.", message);
}
[Theory]
@ -365,9 +353,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(removeStatus);
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", position),
message);
Assert.Equal($"The index value provided by path segment '{position}' is out of bounds of the array size.", message);
}
[Theory]
@ -402,9 +388,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(replaceStatus);
Assert.Equal(
string.Format("The value '{0}' is invalid for target location.", "James"),
message);
Assert.Equal("The value 'James' is invalid for target location.", message);
}
[Fact]

View File

@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Dynamic;
using Newtonsoft.Json.Serialization;
@ -168,9 +167,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(visitStatus);
Assert.Equal(
string.Format("The index value provided by path segment '{0}' is out of bounds of the array size.", position),
message);
Assert.Equal($"The index value provided by path segment '{position}' is out of bounds of the array size.", message);
}
[Theory]
@ -188,12 +185,9 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
// Assert
Assert.False(visitStatus);
Assert.Equal(string.Format(
"The path segment '{0}' is invalid for an array index.", position),
message);
Assert.Equal($"The path segment '{position}' is invalid for an array index.", message);
}
// The adapter takes care of the responsibility of validating the final segment
[Fact]
public void Visit_DoesNotValidate_FinalPathSegment()
{

View File

@ -2,10 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Microsoft.AspNetCore.JsonPatch.Internal;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Test
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
public class ParsedPathTests
{
@ -19,7 +18,10 @@ namespace Microsoft.AspNetCore.JsonPatch.Test
[InlineData("~0~1foo", new string[] { "~/foo" })]
public void ParsingValidPathShouldSucceed(string path, string[] expected)
{
// Arrange & Act
var parsedPath = new ParsedPath(path);
// Assert
Assert.Equal(expected, parsedPath.Segments);
}
@ -30,6 +32,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test
[InlineData("foo~3bar")]
public void PathWithInvalidEscapeSequenceShouldFail(string path)
{
// Arrange, Act & Assert
Assert.Throws<JsonPatchException>(() =>
{
var parsedPath = new ParsedPath(path);

View File

@ -100,9 +100,7 @@ namespace Microsoft.AspNetCore.JsonPatch
});
// Assert
Assert.Equal(
string.Format("The expression '(p.IntegerValue >= 4)' is not supported. Supported expressions include member access and indexer expressions."),
exception.Message);
Assert.Equal("The expression '(p.IntegerValue >= 4)' is not supported. Supported expressions include member access and indexer expressions.", exception.Message);
}
}

View File

@ -1,9 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Xunit;
@ -13,263 +11,39 @@ namespace Microsoft.AspNetCore.JsonPatch
public class JsonPatchDocumentJsonPropertyAttributeTest
{
[Fact]
public void Add_ToRoot_OfListOfObjects_AtEndOfList()
public void Add_RespectsJsonPropertyAttribute()
{
var patchDoc = new JsonPatchDocument<List<JsonPropertyObject>>();
patchDoc.Add(p => p, new JsonPropertyObject());
// Arrange
var patchDocument = new JsonPatchDocument<JsonPropertyObject>();
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyWithAnotherNameObject>>(serialized);
// Act
patchDocument.Add(p => p.Name, "John");
// get path
var pathToCheck = deserialized.Operations.First().path;
Assert.Equal("/-", pathToCheck);
}
[Fact]
public void Add_ToRoot_OfListOfObjects_AtGivenPosition()
{
var patchDoc = new JsonPatchDocument<List<JsonPropertyObject>>();
patchDoc.Add(p => p[3], new JsonPropertyObject());
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyWithAnotherNameObject>>(serialized);
// get path
var pathToCheck = deserialized.Operations.First().path;
Assert.Equal("/3", pathToCheck);
}
[Fact]
public void Add_WithExpression_RespectsJsonPropertyName_ForModelProperty()
{
var patchDoc = new JsonPatchDocument<JsonPropertyObject>();
patchDoc.Add(p => p.Name, "John");
var serialized = JsonConvert.SerializeObject(patchDoc);
// serialized value should have "AnotherName" as path
// deserialize to a JsonPatchDocument<JsonPropertyWithAnotherNameDTO> to check
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyWithAnotherNameObject>>(serialized);
// get path
var pathToCheck = deserialized.Operations.First().path;
// Assert
var pathToCheck = patchDocument.Operations.First().path;
Assert.Equal("/AnotherName", pathToCheck);
}
[Fact]
public void Add_WithExpressionOnStringProperty_FallsbackToPropertyName_WhenJsonPropertyName_IsEmpty()
public void Move_FallsbackToPropertyName_WhenJsonPropertyAttributeName_IsEmpty()
{
// Arrange
var patchDoc = new JsonPatchDocument<JsonPropertyWithNoPropertyName>();
patchDoc.Add(m => m.StringProperty, "Test");
var serialized = JsonConvert.SerializeObject(patchDoc);
var patchDocument = new JsonPatchDocument<JsonPropertyWithNoPropertyName>();
// Act
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyWithNoPropertyName>>(serialized);
patchDocument.Move(m => m.StringProperty, m => m.StringProperty2);
// Assert
var pathToCheck = deserialized.Operations.First().path;
Assert.Equal("/StringProperty", pathToCheck);
}
[Fact]
public void Add_WithExpressionOnArrayProperty_FallsbackToPropertyName_WhenJsonPropertyName_IsEmpty()
{
// Arrange
var patchDoc = new JsonPatchDocument<JsonPropertyWithNoPropertyName>();
patchDoc.Add(m => m.ArrayProperty, "James");
var serialized = JsonConvert.SerializeObject(patchDoc);
// Act
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyWithNoPropertyName>>(serialized);
// Assert
var pathToCheck = deserialized.Operations.First().path;
Assert.Equal("/ArrayProperty/-", pathToCheck);
}
[Fact]
public void Add_WithExpression_RespectsJsonPropertyName_WhenApplyingToDifferentlyTypedClassWithPropertyMatchingJsonPropertyName()
{
var patchDocToSerialize = new JsonPatchDocument<JsonPropertyObject>();
patchDocToSerialize.Add(p => p.Name, "John");
// the patchdoc will deserialize to "anothername". We should thus be able to apply
// it to a class that HAS that other property name.
var doc = new JsonPropertyWithAnotherNameObject()
{
AnotherName = "InitialValue"
};
var serialized = JsonConvert.SerializeObject(patchDocToSerialize);
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyWithAnotherNameObject>>
(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("John", doc.AnotherName);
}
[Fact]
public void Add_WithExpression_RespectsJsonPropertyName_WhenApplyingToSameTypedClassWithMatchingJsonPropertyName()
{
var patchDocToSerialize = new JsonPatchDocument<JsonPropertyObject>();
patchDocToSerialize.Add(p => p.Name, "John");
// the patchdoc will deserialize to "anothername". As JsonPropertyDTO has
// a JsonProperty signifying that "Name" should be deseriallized from "AnotherName",
// we should be able to apply the patchDoc.
var doc = new JsonPropertyObject()
{
Name = "InitialValue"
};
var serialized = JsonConvert.SerializeObject(patchDocToSerialize);
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyObject>>
(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("John", doc.Name);
}
[Fact]
public void Add_OnApplyFromJson_RespectsJsonPropertyNameOnJsonDocument()
{
var doc = new JsonPropertyObject()
{
Name = "InitialValue"
};
// serialization should serialize to "AnotherName"
var serialized = "[{\"value\":\"Kevin\",\"path\":\"/AnotherName\",\"op\":\"add\"}]";
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyObject>>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("Kevin", doc.Name);
}
[Fact]
public void Remove_OnApplyFromJson_RespectsJsonPropertyNameOnJsonDocument()
{
var doc = new JsonPropertyObject()
{
Name = "InitialValue"
};
// serialization should serialize to "AnotherName"
var serialized = "[{\"path\":\"/AnotherName\",\"op\":\"remove\"}]";
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyObject>>(serialized);
deserialized.ApplyTo(doc);
Assert.Null(doc.Name);
}
[Fact]
public void Add_OnApplyFromJson_RespectsInheritedJsonPropertyNameOnJsonDocument()
{
var doc = new JsonPropertyWithInheritanceObject()
{
Name = "InitialName"
};
// serialization should serialize to "AnotherName"
var serialized = "[{\"value\":\"Kevin\",\"path\":\"/AnotherName\",\"op\":\"add\"}]";
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyWithInheritanceObject>>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("Kevin", doc.Name);
}
[Fact]
public void Add_WithExpression_RespectsJsonPropertyName_ForInheritedModelProperty()
{
var patchDoc = new JsonPatchDocument<JsonPropertyWithInheritanceObject>();
patchDoc.Add(p => p.Name, "John");
var serialized = JsonConvert.SerializeObject(patchDoc);
// serialized value should have "AnotherName" as path
// deserialize to a JsonPatchDocument<JsonPropertyWithAnotherNameDTO> to check
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyWithAnotherNameObject>>(serialized);
// get path
var pathToCheck = deserialized.Operations.First().path;
Assert.Equal("/AnotherName", pathToCheck);
}
[Fact]
public void Add_OnApplyFromJson_EscapingHandledOnComplexJsonPropertyNameOnJsonDocument()
{
var doc = new JsonPropertyComplexNameObject()
{
FooSlashBars = "InitialName",
FooSlashTilde = new SimpleObject
{
StringProperty = "Initial Value"
}
};
// serialization should serialize to "AnotherName"
var serialized = "[{\"value\":\"Kevin\",\"path\":\"/foo~1bar~0\",\"op\":\"add\"},{\"value\":\"Final Value\",\"path\":\"/foo~1~0/StringProperty\",\"op\":\"replace\"}]";
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyComplexNameObject>>(serialized);
deserialized.ApplyTo(doc);
Assert.Equal("Kevin", doc.FooSlashBars);
Assert.Equal("Final Value", doc.FooSlashTilde.StringProperty);
}
[Fact]
public void Move_WithExpression_FallsbackToPropertyName_WhenJsonPropertyName_IsEmpty()
{
// Arrange
var patchDoc = new JsonPatchDocument<JsonPropertyWithNoPropertyName>();
patchDoc.Move(m => m.StringProperty, m => m.StringProperty2);
var serialized = JsonConvert.SerializeObject(patchDoc);
// Act
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyWithNoPropertyName>>(serialized);
// Assert
var fromPath = deserialized.Operations.First().from;
var fromPath = patchDocument.Operations.First().from;
Assert.Equal("/StringProperty", fromPath);
var toPath = deserialized.Operations.First().path;
var toPath = patchDocument.Operations.First().path;
Assert.Equal("/StringProperty2", toPath);
}
[Fact]
public void Add_WithExpression_AndCustomContractResolver_UsesPropertyName_SetByContractResolver()
private class JsonPropertyObject
{
// Arrange
var patchDoc = new JsonPatchDocument<JsonPropertyWithNoPropertyName>();
patchDoc.ContractResolver = new CustomContractResolver();
patchDoc.Add(m => m.SSN, "123-45-6789");
var serialized = JsonConvert.SerializeObject(patchDoc);
// Act
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyWithNoPropertyName>>(serialized);
// Assert
var path = deserialized.Operations.First().path;
Assert.Equal("/SocialSecurityNumber", path);
[JsonProperty("AnotherName")]
public string Name { get; set; }
}
private class JsonPropertyWithNoPropertyName
@ -286,20 +60,5 @@ namespace Microsoft.AspNetCore.JsonPatch
[JsonProperty]
public string SSN { get; set; }
}
private class CustomContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var jsonProperty = base.CreateProperty(member, memberSerialization);
if (jsonProperty.PropertyName == "SSN")
{
jsonProperty.PropertyName = "SocialSecurityNumber";
}
return jsonProperty;
}
}
}
}

View File

@ -1,55 +1,180 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Newtonsoft.Json;
using Xunit;
namespace Microsoft.AspNetCore.JsonPatch.Test
namespace Microsoft.AspNetCore.JsonPatch
{
public class JsonPatchDocumentTest
{
[Fact]
public void InvalidOperation_ThrowsException_CallsIntoLogErrorAction()
public void InvalidPathAtBeginningShouldThrowException()
{
// Arrange
var operationName = "foo";
var serialized = "[{\"value\":\"John\",\"path\":\"/Name\",\"op\":\"" + operationName + "\"}]";
var jsonPatchDocument = JsonConvert.DeserializeObject<JsonPatchDocument<Customer>>(serialized);
var model = new Customer();
var expectedErrorMessage = $"Invalid JsonPatch operation '{operationName}'.";
string actualErrorMessage = null;
var patchDocument = new JsonPatchDocument();
// Act
jsonPatchDocument.ApplyTo(model, (jsonPatchError) =>
var exception = Assert.Throws<JsonPatchException>(() =>
{
actualErrorMessage = jsonPatchError.ErrorMessage;
patchDocument.Add("//NewInt", 1);
});
// Assert
Assert.Equal(expectedErrorMessage, actualErrorMessage);
Assert.Equal(
"The provided string '//NewInt' is an invalid path.",
exception.Message);
}
[Fact]
public void InvalidOperation_NoLogErrorAction_ThrowsJsonPatchException()
public void InvalidPathAtEndShouldThrowException()
{
// Arrange
var operationName = "foo";
var serialized = "[{\"value\":\"John\",\"path\":\"/Name\",\"op\":\"" + operationName + "\"}]";
var jsonPatchDocument = JsonConvert.DeserializeObject<JsonPatchDocument<Customer>>(serialized);
var model = new Customer();
var expectedErrorMessage = $"Invalid JsonPatch operation '{operationName}'.";
var patchDocument = new JsonPatchDocument();
// Act
var jsonPatchException = Assert.Throws<JsonPatchException>(() => jsonPatchDocument.ApplyTo(model));
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.Add("NewInt//", 1);
});
// Assert
Assert.Equal(expectedErrorMessage, jsonPatchException.Message);
Assert.Equal(
"The provided string 'NewInt//' is an invalid path.",
exception.Message);
}
private class Customer
[Fact]
public void InvalidPathWithDotShouldThrowException()
{
public string Name { get; set; }
// Arrange
var patchDocument = new JsonPatchDocument();
// Act
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDocument.Add("NewInt.Test", 1);
});
// Assert
Assert.Equal(
"The provided string 'NewInt.Test' is an invalid path.",
exception.Message);
}
[Fact]
public void NonGenericPatchDocToGenericMustSerialize()
{
// Arrange
var targetObject = new SimpleObject()
{
StringProperty = "A",
AnotherStringProperty = "B"
};
var patchDocument = new JsonPatchDocument();
patchDocument.Copy("StringProperty", "AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDocument);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleObject>>(serialized);
// Act
deserialized.ApplyTo(targetObject);
// Assert
Assert.Equal("A", targetObject.AnotherStringProperty);
}
[Fact]
public void GenericPatchDocToNonGenericMustSerialize()
{
// Arrange
var targetObject = new SimpleObject()
{
StringProperty = "A",
AnotherStringProperty = "B"
};
var patchDocTyped = new JsonPatchDocument<SimpleObject>();
patchDocTyped.Copy(o => o.StringProperty, o => o.AnotherStringProperty);
var patchDocUntyped = new JsonPatchDocument();
patchDocUntyped.Copy("StringProperty", "AnotherStringProperty");
var serializedTyped = JsonConvert.SerializeObject(patchDocTyped);
var serializedUntyped = JsonConvert.SerializeObject(patchDocUntyped);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serializedTyped);
// Act
deserialized.ApplyTo(targetObject);
// Assert
Assert.Equal("A", targetObject.AnotherStringProperty);
}
[Fact]
public void Deserialization_Successful_ForValidJsonPatchDocument()
{
// Arrange
var doc = new SimpleObject()
{
StringProperty = "A",
DecimalValue = 10,
DoubleValue = 10,
FloatValue = 10,
IntegerValue = 10
};
var patchDocument = new JsonPatchDocument<SimpleObject>();
patchDocument.Replace(o => o.StringProperty, "B");
patchDocument.Replace(o => o.DecimalValue, 12);
patchDocument.Replace(o => o.DoubleValue, 12);
patchDocument.Replace(o => o.FloatValue, 12);
patchDocument.Replace(o => o.IntegerValue, 12);
// default: no envelope
var serialized = JsonConvert.SerializeObject(patchDocument);
// Act
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument<SimpleObject>>(serialized);
// Assert
Assert.IsType<JsonPatchDocument<SimpleObject>>(deserialized);
}
[Fact]
public void Deserialization_Fails_ForInvalidJsonPatchDocument()
{
// Arrange
var serialized = "{\"Operations\": [{ \"op\": \"replace\", \"path\": \"/title\", \"value\": \"New Title\"}]}";
// Act
var exception = Assert.Throws<JsonSerializationException>(() =>
{
var deserialized
= JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
});
// Assert
Assert.Equal("The JSON patch document was malformed and could not be parsed.", exception.Message);
}
[Fact]
public void Deserialization_Fails_ForInvalidTypedJsonPatchDocument()
{
// Arrange
var serialized = "{\"Operations\": [{ \"op\": \"replace\", \"path\": \"/title\", \"value\": \"New Title\"}]}";
// Act
var exception = Assert.Throws<JsonSerializationException>(() =>
{
var deserialized
= JsonConvert.DeserializeObject<JsonPatchDocument<SimpleObject>>(serialized);
});
// Assert
Assert.Equal("The JSON patch document was malformed and could not be parsed.", exception.Message);
}
}
}
}

View File

@ -1,16 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Newtonsoft.Json;
namespace Microsoft.AspNetCore.JsonPatch
{
public class JsonPropertyComplexNameObject
{
[JsonProperty("foo/bar~")]
public string FooSlashBars { get; set; }
[JsonProperty("foo/~")]
public SimpleObject FooSlashTilde { get; set; }
}
}

View File

@ -1,13 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Newtonsoft.Json;
namespace Microsoft.AspNetCore.JsonPatch
{
public class JsonPropertyObject
{
[JsonProperty("AnotherName")]
public string Name { get; set; }
}
}

View File

@ -1,10 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.JsonPatch
{
public class JsonPropertyWithAnotherNameObject
{
public string AnotherName { get; set; }
}
}

View File

@ -1,15 +0,0 @@
using Newtonsoft.Json;
namespace Microsoft.AspNetCore.JsonPatch
{
public class JsonPropertyWithInheritanceObject : JsonPropertyWithInheritanceBaseObject
{
public override string Name { get; set; }
}
public abstract class JsonPropertyWithInheritanceBaseObject
{
[JsonProperty("AnotherName")]
public abstract string Name { get; set; }
}
}

View File

@ -1,10 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.JsonPatch
{
public class NestedObject
{
public string StringProperty { get; set; }
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.JsonPatch
{
public class SimpleObjectWithNestedObjectWithNullCheck
{
public SimpleObjectWithNullCheck SimpleObjectWithNullCheck { get; set; }
public SimpleObjectWithNestedObjectWithNullCheck()
{
SimpleObjectWithNullCheck = new SimpleObjectWithNullCheck();
}
}
}

View File

@ -1,30 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.AspNetCore.JsonPatch
{
public class SimpleObjectWithNullCheck
{
private string stringProperty;
public string StringProperty
{
get
{
return stringProperty;
}
set
{
if (value == null)
{
throw new ArgumentNullException();
}
stringProperty = value;
}
}
}
}

View File

@ -0,0 +1,17 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.JsonPatch.Internal
{
internal class Customer
{
private string _name;
private int _age;
public Customer(string name, int age)
{
_name = name;
_age = age;
}
}
}

View File

@ -4,7 +4,7 @@
using System.Collections.Generic;
using System.Dynamic;
namespace Microsoft.AspNetCore.JsonPatch.Internal
namespace Microsoft.AspNetCore.JsonPatch
{
public class DynamicTestObject : DynamicObject
{

View File

@ -1,11 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNetCore.JsonPatch.Internal
namespace Microsoft.AspNetCore.JsonPatch
{
public class NestedObject
{
public string StringProperty { get; set; }
public dynamic DynamicProperty { get; set; }
}
}
}

View File

@ -8,9 +8,11 @@ namespace Microsoft.AspNetCore.JsonPatch
{
public class SimpleObject
{
public List<SimpleObject> SimpleObjectList { get; set; }
public List<int> IntegerList { get; set; }
public IList<int> IntegerIList { get; set; }
public int IntegerValue { get; set; }
public int AnotherIntegerValue { get; set; }
public string StringProperty { get; set; }
public string AnotherStringProperty { get; set; }
public decimal DecimalValue { get; set; }