Use DictionaryKeyResolver for ExpandoObjectAdapter (#100)

Addresses #102
This commit is contained in:
Jass Bagga 2017-08-23 10:30:11 -07:00 committed by GitHub
parent a993c2a5ff
commit d4b64af0e6
16 changed files with 330 additions and 241 deletions

View File

@ -2,8 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.JsonPatch.Exceptions;
using Microsoft.AspNetCore.JsonPatch.Helpers;
using Microsoft.AspNetCore.JsonPatch.Internal;
using Microsoft.AspNetCore.JsonPatch.Operations;
using Newtonsoft.Json.Serialization;
@ -22,12 +20,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Adapters
IContractResolver contractResolver,
Action<JsonPatchError> logErrorAction)
{
if (contractResolver == null)
{
throw new ArgumentNullException(nameof(contractResolver));
}
ContractResolver = contractResolver;
ContractResolver = contractResolver ?? throw new ArgumentNullException(nameof(contractResolver));
LogErrorAction = logErrorAction;
}
@ -144,10 +137,8 @@ namespace Microsoft.AspNetCore.JsonPatch.Adapters
var parsedPath = new ParsedPath(path);
var visitor = new ObjectVisitor(parsedPath, ContractResolver);
IAdapter adapter;
var target = objectToApplyTo;
string errorMessage;
if (!visitor.TryVisit(ref target, out adapter, out errorMessage))
if (!visitor.TryVisit(ref target, out var adapter, out var errorMessage))
{
var error = CreatePathNotFoundError(objectToApplyTo, path, operation, errorMessage);
ErrorReporter(error);
@ -197,9 +188,8 @@ namespace Microsoft.AspNetCore.JsonPatch.Adapters
throw new ArgumentNullException(nameof(objectToApplyTo));
}
object propertyValue;
// Get value at 'from' location and add that value to the 'path' location
if (TryGetValue(operation.from, objectToApplyTo, operation, out propertyValue))
if (TryGetValue(operation.from, objectToApplyTo, operation, out var propertyValue))
{
// remove that value
Remove(operation.from, objectToApplyTo, operation);
@ -253,10 +243,8 @@ namespace Microsoft.AspNetCore.JsonPatch.Adapters
var parsedPath = new ParsedPath(path);
var visitor = new ObjectVisitor(parsedPath, ContractResolver);
IAdapter adapter;
var target = objectToApplyTo;
string errorMessage;
if (!visitor.TryVisit(ref target, out adapter, out errorMessage))
if (!visitor.TryVisit(ref target, out var adapter, out var errorMessage))
{
var error = CreatePathNotFoundError(objectToApplyTo, path, operationToReport, errorMessage);
ErrorReporter(error);
@ -306,10 +294,8 @@ namespace Microsoft.AspNetCore.JsonPatch.Adapters
var parsedPath = new ParsedPath(operation.path);
var visitor = new ObjectVisitor(parsedPath, ContractResolver);
IAdapter adapter;
var target = objectToApplyTo;
string errorMessage;
if (!visitor.TryVisit(ref target, out adapter, out errorMessage))
if (!visitor.TryVisit(ref target, out var adapter, out var errorMessage))
{
var error = CreatePathNotFoundError(objectToApplyTo, operation.path, operation, errorMessage);
ErrorReporter(error);
@ -358,9 +344,8 @@ namespace Microsoft.AspNetCore.JsonPatch.Adapters
throw new ArgumentNullException(nameof(objectToApplyTo));
}
object propertyValue;
// Get value at 'from' location and add that value to the 'path' location
if (TryGetValue(operation.from, objectToApplyTo, operation, out propertyValue))
if (TryGetValue(operation.from, objectToApplyTo, operation, out var propertyValue))
{
// Create deep copy
var copyResult = ConversionResultProvider.CopyTo(propertyValue, propertyValue.GetType());
@ -406,10 +391,8 @@ namespace Microsoft.AspNetCore.JsonPatch.Adapters
var parsedPath = new ParsedPath(fromLocation);
var visitor = new ObjectVisitor(parsedPath, ContractResolver);
IAdapter adapter;
var target = objectToGetValueFrom;
string errorMessage;
if (!visitor.TryVisit(ref target, out adapter, out errorMessage))
if (!visitor.TryVisit(ref target, out var adapter, out var errorMessage))
{
var error = CreatePathNotFoundError(objectToGetValueFrom, fromLocation, operation, errorMessage);
ErrorReporter(error);

View File

@ -16,10 +16,10 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
object value,
out string errorMessage)
{
var contract = (JsonDictionaryContract)contractResolver.ResolveContract(target.GetType());
var key = contract.DictionaryKeyResolver(segment);
var dictionary = (IDictionary<string, object>)target;
var key = dictionary.GetKeyUsingCaseInsensitiveSearch(segment);
// As per JsonPatch spec, if a key already exists, adding should replace the existing value
dictionary[key] = ConvertValue(dictionary, key, value);
@ -34,9 +34,10 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
out object value,
out string errorMessage)
{
var contract = (JsonDictionaryContract)contractResolver.ResolveContract(target.GetType());
var key = contract.DictionaryKeyResolver(segment);
var dictionary = (IDictionary<string, object>)target;
var key = dictionary.GetKeyUsingCaseInsensitiveSearch(segment);
value = dictionary[key];
errorMessage = null;
@ -49,10 +50,10 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
IContractResolver contractResolver,
out string errorMessage)
{
var contract = (JsonDictionaryContract)contractResolver.ResolveContract(target.GetType());
var key = contract.DictionaryKeyResolver(segment);
var dictionary = (IDictionary<string, object>)target;
var key = dictionary.GetKeyUsingCaseInsensitiveSearch(segment);
// As per JsonPatch spec, the target location must exist for remove to be successful
if (!dictionary.ContainsKey(key))
{
@ -73,10 +74,10 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
object value,
out string errorMessage)
{
var contract = (JsonDictionaryContract)contractResolver.ResolveContract(target.GetType());
var key = contract.DictionaryKeyResolver(segment);
var dictionary = (IDictionary<string, object>)target;
var key = dictionary.GetKeyUsingCaseInsensitiveSearch(segment);
// As per JsonPatch spec, the target location must exist for remove to be successful
if (!dictionary.ContainsKey(key))
{
@ -105,10 +106,10 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
return false;
}
var contract = (JsonDictionaryContract)contractResolver.ResolveContract(target.GetType());
var key = contract.DictionaryKeyResolver(segment);
var dictionary = (IDictionary<string, object>)expandoObject;
var key = dictionary.GetKeyUsingCaseInsensitiveSearch(segment);
if (dictionary.ContainsKey(key))
{
nextTarget = dictionary[key];
@ -125,8 +126,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
private object ConvertValue(IDictionary<string, object> dictionary, string key, object newValue)
{
object existingValue = null;
if (dictionary.TryGetValue(key, out existingValue))
if (dictionary.TryGetValue(key, out var existingValue))
{
if (existingValue != null)
{

View File

@ -1,26 +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
{
// Helper methods to allow case-insensitive key search
public static class ExpandoObjectDictionaryExtensions
{
internal static string GetKeyUsingCaseInsensitiveSearch(
this IDictionary<string, object> propertyDictionary,
string key)
{
foreach (var keyInDictionary in propertyDictionary.Keys)
{
if (string.Equals(key, keyInDictionary, StringComparison.OrdinalIgnoreCase))
{
return keyInDictionary;
}
}
return key;
}
}
}

View File

@ -20,20 +20,17 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
{
var list = (IList)target;
Type typeArgument = null;
if (!TryGetListTypeArgument(list, out typeArgument, out errorMessage))
if (!TryGetListTypeArgument(list, out var typeArgument, out errorMessage))
{
return false;
}
PositionInfo positionInfo;
if (!TryGetPositionInfo(list, segment, OperationType.Add, out positionInfo, out errorMessage))
if (!TryGetPositionInfo(list, segment, OperationType.Add, out var positionInfo, out errorMessage))
{
return false;
}
object convertedValue = null;
if (!TryConvertValue(value, typeArgument, segment, out convertedValue, out errorMessage))
if (!TryConvertValue(value, typeArgument, segment, out var convertedValue, out errorMessage))
{
return false;
}
@ -60,15 +57,13 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
{
var list = (IList)target;
Type typeArgument = null;
if (!TryGetListTypeArgument(list, out typeArgument, out errorMessage))
if (!TryGetListTypeArgument(list, out var typeArgument, out errorMessage))
{
value = null;
return false;
}
PositionInfo positionInfo;
if (!TryGetPositionInfo(list, segment, OperationType.Get, out positionInfo, out errorMessage))
if (!TryGetPositionInfo(list, segment, OperationType.Get, out var positionInfo, out errorMessage))
{
value = null;
return false;
@ -95,14 +90,12 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
{
var list = (IList)target;
Type typeArgument = null;
if (!TryGetListTypeArgument(list, out typeArgument, out errorMessage))
if (!TryGetListTypeArgument(list, out var typeArgument, out errorMessage))
{
return false;
}
PositionInfo positionInfo;
if (!TryGetPositionInfo(list, segment, OperationType.Remove, out positionInfo, out errorMessage))
if (!TryGetPositionInfo(list, segment, OperationType.Remove, out var positionInfo, out errorMessage))
{
return false;
}
@ -129,20 +122,17 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
{
var list = (IList)target;
Type typeArgument = null;
if (!TryGetListTypeArgument(list, out typeArgument, out errorMessage))
if (!TryGetListTypeArgument(list, out var typeArgument, out errorMessage))
{
return false;
}
PositionInfo positionInfo;
if (!TryGetPositionInfo(list, segment, OperationType.Replace, out positionInfo, out errorMessage))
if (!TryGetPositionInfo(list, segment, OperationType.Replace, out var positionInfo, out errorMessage))
{
return false;
}
object convertedValue = null;
if (!TryConvertValue(value, typeArgument, segment, out convertedValue, out errorMessage))
if (!TryConvertValue(value, typeArgument, segment, out var convertedValue, out errorMessage))
{
return false;
}
@ -175,7 +165,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
return false;
}
int index = -1;
var index = -1;
if (!int.TryParse(segment, out index))
{
value = null;
@ -257,7 +247,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
return true;
}
int position = -1;
var position = -1;
if (int.TryParse(segment, out position))
{
if (position >= 0 && position < list.Count)

View File

@ -17,8 +17,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
object value,
out string errorMessage)
{
JsonProperty jsonProperty = null;
if (!TryGetJsonProperty(target, contractResolver, segment, out jsonProperty))
if (!TryGetJsonProperty(target, contractResolver, segment, out var jsonProperty))
{
errorMessage = Resources.FormatTargetLocationAtPathSegmentNotFound(segment);
return false;
@ -30,8 +29,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
return false;
}
object convertedValue = null;
if (!TryConvertValue(value, jsonProperty.PropertyType, out convertedValue))
if (!TryConvertValue(value, jsonProperty.PropertyType, out var convertedValue))
{
errorMessage = Resources.FormatInvalidValueForProperty(value);
return false;
@ -50,8 +48,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
out object value,
out string errorMessage)
{
JsonProperty jsonProperty = null;
if (!TryGetJsonProperty(target, contractResolver, segment, out jsonProperty))
if (!TryGetJsonProperty(target, contractResolver, segment, out var jsonProperty))
{
errorMessage = Resources.FormatTargetLocationAtPathSegmentNotFound(segment);
value = null;
@ -76,8 +73,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
IContractResolver contractResolver,
out string errorMessage)
{
JsonProperty jsonProperty = null;
if (!TryGetJsonProperty(target, contractResolver, segment, out jsonProperty))
if (!TryGetJsonProperty(target, contractResolver, segment, out var jsonProperty))
{
errorMessage = Resources.FormatTargetLocationAtPathSegmentNotFound(segment);
return false;
@ -112,8 +108,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
object value,
out string errorMessage)
{
JsonProperty jsonProperty = null;
if (!TryGetJsonProperty(target, contractResolver, segment, out jsonProperty))
if (!TryGetJsonProperty(target, contractResolver, segment, out var jsonProperty))
{
errorMessage = Resources.FormatTargetLocationAtPathSegmentNotFound(segment);
return false;
@ -125,8 +120,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
return false;
}
object convertedValue = null;
if (!TryConvertValue(value, jsonProperty.PropertyType, out convertedValue))
if (!TryConvertValue(value, jsonProperty.PropertyType, out var convertedValue))
{
errorMessage = Resources.FormatInvalidValueForProperty(value);
return false;
@ -152,8 +146,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
return false;
}
JsonProperty jsonProperty = null;
if (TryGetJsonProperty(target, contractResolver, segment, out jsonProperty))
if (TryGetJsonProperty(target, contractResolver, segment, out var jsonProperty))
{
value = jsonProperty.ValueProvider.GetValue(target);
errorMessage = null;
@ -171,8 +164,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
string segment,
out JsonProperty jsonProperty)
{
var jsonObjectContract = contractResolver.ResolveContract(target.GetType()) as JsonObjectContract;
if (jsonObjectContract != null)
if (contractResolver.ResolveContract(target.GetType()) is JsonObjectContract jsonObjectContract)
{
var pocoProperty = jsonObjectContract
.Properties

View File

@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("NewInt", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
obj.Test = 1;
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("NewInt", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -65,7 +65,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("Nested/NewInt", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -90,7 +90,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("Nested/NewInt", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -115,7 +115,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("Nested/StringProperty", "A");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -137,7 +137,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("Nested/NewInt", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -158,7 +158,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("DynamicProperty/NewInt", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -181,7 +181,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("DynamicProperty/StringProperty", "B");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -203,7 +203,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
dynamic valueToAdd = new { IntValue = 1, StringValue = "test", GuidValue = Guid.NewGuid() };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("ComplexProperty", valueToAdd);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -227,7 +227,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("StringProperty", "B");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -249,7 +249,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.StringProperty = "A";
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("StringProperty", "B");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -269,7 +269,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.InBetweenFirst.InBetweenSecond.StringProperty = "A";
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("/InBetweenFirst/InBetweenSecond/StringProperty", "B");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -291,7 +291,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.Nested.DynamicProperty.InBetweenFirst.InBetweenSecond.StringProperty = "A";
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("/Nested/DynamicProperty/InBetweenFirst/InBetweenSecond/StringProperty", "B");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -325,7 +325,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("DynamicProperty/OtherProperty/IntProperty", 1);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -366,7 +366,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("baz/bat", "qux");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -382,13 +382,13 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
}
[Fact]
public void ShouldReplacePropertyWithDifferentCase()
public void ShouldNotReplacePropertyWithDifferentCase()
{
dynamic doc = new ExpandoObject();
doc.StringProperty = "A";
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("stringproperty", "B");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -396,7 +396,8 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
deserialized.ApplyTo(doc);
Assert.Equal("B", doc.StringProperty);
Assert.Equal("A", doc.StringProperty);
Assert.Equal("B", doc.stringproperty);
}
[Fact]
@ -408,7 +409,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -426,7 +427,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/-1", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -450,7 +451,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("integerlist/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -470,7 +471,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/4", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -494,7 +495,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -514,7 +515,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/-1", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -538,7 +539,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/-", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);

View File

@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("IntegerList/-1", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -49,7 +49,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("ListOfSimpleDTO/0/IntegerList/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("ListOfSimpleDTO/-1/IntegerList/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -103,7 +103,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
}
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Add("ListOfSimpleDTO/20/IntegerList/0", 4);
var serialized = JsonConvert.SerializeObject(patchDoc);

View File

@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.StringProperty = "A";
doc.AnotherStringProperty = "B";
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("StringProperty", "AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerList/0", "IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -53,7 +53,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerList/0", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -70,7 +70,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerList/0", "IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -89,7 +89,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerValue", "IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -107,7 +107,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerValue", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -129,7 +129,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleDTO/StringProperty", "SimpleDTO/AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -150,7 +150,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleDTO/IntegerList/0", "SimpleDTO/IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -170,7 +170,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleDTO/IntegerList/0", "SimpleDTO/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -191,7 +191,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleDTO/IntegerList/0", "SimpleDTO/IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -212,7 +212,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleDTO/IntegerValue", "SimpleDTO/IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
@ -232,7 +232,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleDTO/IntegerValue", "SimpleDTO/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);

View File

@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("StringProperty", "AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerList/0", "IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -58,7 +58,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerList/0", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -77,7 +77,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerList/0", "IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -98,7 +98,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerValue", "IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -118,7 +118,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("IntegerValue", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -142,7 +142,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleDTO/StringProperty", "SimpleDTO/AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -165,7 +165,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleDTO/IntegerList/0", "SimpleDTO/IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -187,7 +187,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleDTO/IntegerList/0", "SimpleDTO/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -210,7 +210,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleDTO/IntegerList/0", "SimpleDTO/IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -233,7 +233,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleDTO/IntegerValue", "SimpleDTO/IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
var deserialized = JsonConvert.DeserializeObject<JsonPatchDocument>(serialized);
@ -255,7 +255,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("SimpleDTO/IntegerValue", "SimpleDTO/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);

View File

@ -0,0 +1,154 @@
// 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

@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.AnotherStringProperty = "B";
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("StringProperty", "AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -29,8 +29,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
Assert.Equal("A", doc.AnotherStringProperty);
var cont = doc as IDictionary<string, object>;
object valueFromDictionary;
cont.TryGetValue("StringProperty", out valueFromDictionary);
cont.TryGetValue("StringProperty", out var valueFromDictionary);
Assert.Null(valueFromDictionary);
}
@ -41,7 +40,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.StringProperty = "A";
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("StringProperty", "AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -52,8 +51,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
Assert.Equal("A", doc.AnotherStringProperty);
var cont = doc as IDictionary<string, object>;
object valueFromDictionary;
cont.TryGetValue("StringProperty", out valueFromDictionary);
cont.TryGetValue("StringProperty", out var valueFromDictionary);
Assert.Null(valueFromDictionary);
}
@ -65,7 +63,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.SimpleDTO = new SimpleDTO() { AnotherStringProperty = "B" };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("StringProperty", "SimpleDTO/AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -76,8 +74,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
Assert.Equal("A", doc.SimpleDTO.AnotherStringProperty);
var cont = doc as IDictionary<string, object>;
object valueFromDictionary;
cont.TryGetValue("StringProperty", out valueFromDictionary);
cont.TryGetValue("StringProperty", out var valueFromDictionary);
Assert.Null(valueFromDictionary);
}
@ -89,7 +86,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.SimpleDTO = new SimpleDTO() { AnotherStringProperty = "B" };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("SimpleDTO/AnotherStringProperty", "StringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -112,7 +109,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("Nested/StringProperty", "Nested/AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -131,7 +128,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerList/0", "IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -152,7 +149,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("Nested/IntegerList/0", "Nested/IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -170,7 +167,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerList/0", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -190,7 +187,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("Nested/IntegerList/0", "Nested/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -207,7 +204,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerList/0", "IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -229,7 +226,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("Nested/IntegerList/0", "Nested/IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -249,7 +246,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerValue", "IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -258,8 +255,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
deserialized.ApplyTo(doc);
var cont = doc as IDictionary<string, object>;
object valueFromDictionary;
cont.TryGetValue("IntegerValue", out valueFromDictionary);
cont.TryGetValue("IntegerValue", out var valueFromDictionary);
Assert.Null(valueFromDictionary);
Assert.Equal(new List<int>() { 5, 1, 2, 3 }, doc.IntegerList);
@ -276,7 +272,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("Nested/IntegerValue", "Nested/IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -296,7 +292,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.IntegerList = new List<int>() { 1, 2, 3 };
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerValue", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -305,8 +301,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
deserialized.ApplyTo(doc);
var cont = doc as IDictionary<string, object>;
object valueFromDictionary;
cont.TryGetValue("IntegerValue", out valueFromDictionary);
cont.TryGetValue("IntegerValue", out var valueFromDictionary);
Assert.Null(valueFromDictionary);
Assert.Equal(new List<int>() { 1, 2, 3, 5 }, doc.IntegerList);
@ -323,7 +318,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("Nested/IntegerValue", "Nested/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);

View File

@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("StringProperty", "AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerList/0", "IntegerList/1");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -60,7 +60,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerList/0", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -79,7 +79,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerList/0", "IntegerValue");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -101,7 +101,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerValue", "IntegerList/0");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -123,7 +123,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Move("IntegerValue", "IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);

View File

@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
[Fact]
public void InvalidPathAtBeginningShouldThrowException()
{
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDoc.Add("//NewInt", 1);
@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
[Fact]
public void InvalidPathAtEndShouldThrowException()
{
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDoc.Add("NewInt//", 1);
@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
[Fact]
public void InvalidPathWithDotShouldThrowException()
{
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
var exception = Assert.Throws<JsonPatchException>(() =>
{
patchDoc.Add("NewInt.Test", 1);
@ -57,7 +57,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
AnotherStringProperty = "B"
};
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Copy("StringProperty", "AnotherStringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -77,10 +77,10 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
AnotherStringProperty = "B"
};
JsonPatchDocument<SimpleDTO> patchDocTyped = new JsonPatchDocument<SimpleDTO>();
var patchDocTyped = new JsonPatchDocument<SimpleDTO>();
patchDocTyped.Copy<string>(o => o.StringProperty, o => o.AnotherStringProperty);
JsonPatchDocument patchDocUntyped = new JsonPatchDocument();
var patchDocUntyped = new JsonPatchDocument();
patchDocUntyped.Copy("StringProperty", "AnotherStringProperty");
var serializedTyped = JsonConvert.SerializeObject(patchDocTyped);

View File

@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("Test");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -42,7 +42,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
doc.Test = 1;
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("NonExisting");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -64,7 +64,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
obj.Test = 1;
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("Test");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -73,32 +73,31 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
deserialized.ApplyTo(obj);
var cont = obj as IDictionary<string, object>;
object valueFromDictionary;
cont.TryGetValue("Test", out valueFromDictionary);
cont.TryGetValue("Test", out var valueFromDictionary);
Assert.Null(valueFromDictionary);
}
[Fact]
public void RemovePropertyFromExpandoObjectMixedCase()
public void RemoveProperty_FromExpandoObject_MixedCase_ThrowsPathNotFoundException()
{
dynamic obj = new ExpandoObject();
obj.Test = 1;
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
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>;
object valueFromDictionary;
cont.TryGetValue("Test", out valueFromDictionary);
Assert.Null(valueFromDictionary);
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]
@ -109,7 +108,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
obj.Test.AnotherTest = "A";
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("Test");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -118,32 +117,31 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
deserialized.ApplyTo(obj);
var cont = obj as IDictionary<string, object>;
object valueFromDictionary;
cont.TryGetValue("Test", out valueFromDictionary);
cont.TryGetValue("Test", out var valueFromDictionary);
Assert.Null(valueFromDictionary);
}
[Fact]
public void RemoveNestedPropertyFromExpandoObjectMixedCase()
public void RemoveNestedProperty_FromExpandoObject_MixedCase_ThrowsPathNotFoundException()
{
dynamic obj = new ExpandoObject();
obj.Test = new ExpandoObject();
obj.Test.AnotherTest = "A";
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
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>;
object valueFromDictionary;
cont.TryGetValue("Test", out valueFromDictionary);
Assert.Null(valueFromDictionary);
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]
@ -156,7 +154,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/StringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -167,7 +165,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
}
[Fact]
public void NestedRemoveMixedCase()
public void NestedRemove_MixedCase_ThrowsPathNotFoundException()
{
dynamic doc = new ExpandoObject();
doc.SimpleDTO = new SimpleDTO()
@ -176,15 +174,21 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
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);
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",
"/Simpledto/stringProperty"),
exception.Message);
}
[Fact]
@ -197,7 +201,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/IntegerList/2");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -218,7 +222,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/Integerlist/2");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -239,7 +243,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/IntegerList/3");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -264,7 +268,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/IntegerList/-1");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -289,7 +293,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);

View File

@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("StringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -39,7 +39,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("IntegerList/2");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -59,7 +59,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("IntegerList/3");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -83,7 +83,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("IntegerList/-1");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -107,7 +107,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -130,7 +130,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/StringProperty");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -153,7 +153,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/IntegerList/2");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -176,7 +176,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/IntegerList/3");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -203,7 +203,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/IntegerList/-1");
var serialized = JsonConvert.SerializeObject(patchDoc);
@ -230,7 +230,7 @@ namespace Microsoft.AspNetCore.JsonPatch.Test.Dynamic
};
// create patch
JsonPatchDocument patchDoc = new JsonPatchDocument();
var patchDoc = new JsonPatchDocument();
patchDoc.Remove("SimpleDTO/IntegerList/-");
var serialized = JsonConvert.SerializeObject(patchDoc);

View File

@ -98,10 +98,6 @@ namespace Microsoft.AspNetCore.JsonPatch.Internal
{
get
{
var model = new Class1();
yield return new object[] { model, "/Items/Name", model.Items };
yield return new object[] { model.Items, "/Name", model.Items };
var nestedModel = new Class1Nested();
nestedModel.Customers.Add(new Class1());
yield return new object[] { nestedModel, "/Customers/0/Items/Name", nestedModel.Customers[0].Items };