Skip to content

Commit

Permalink
#34 object properties (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
Morcatko authored Sep 30, 2020
1 parent b996907 commit e251cba
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/3.0-JsonMergePatch.Document/Internal/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ static class ReflectionHelper
private static PropertyInfo GetPropertyInfo(Type type, string propertyName)
=> type.GetProperties().Single(property => property.Name.Equals(propertyName, StringComparison.InvariantCultureIgnoreCase));

private static JsonProperty GetJsonProperty(JsonObjectContract jsonObjectContract, string propertyName)
=> jsonObjectContract.Properties.Single(p => p.PropertyName.Equals(propertyName, StringComparison.InvariantCultureIgnoreCase));

internal static Type GetPropertyTypeFromPath(Type type, string path, IContractResolver contractResolver)
{
var currentType = type;
Expand All @@ -25,8 +28,12 @@ internal static Type GetPropertyTypeFromPath(Type type, string path, IContractRe
currentType = jsonDictionaryContract.DictionaryValueType;
continue;
}
var currentProperty = GetPropertyInfo(currentType, propertyName);
currentType = currentProperty.PropertyType;
if (jsonContract is JsonObjectContract jsonObjectContract)
{
currentType = GetJsonProperty(jsonObjectContract, propertyName).PropertyType;
continue;
}
currentType = GetPropertyInfo(currentType, propertyName).PropertyType;
}
return currentType;
}
Expand Down Expand Up @@ -56,7 +63,10 @@ private static bool Exist(object value, IEnumerable<string> paths, IContractReso
{
return false;
}

}
else if (jsonContract is JsonObjectContract jsonObjectContract)
{
currentValue = GetJsonProperty(jsonObjectContract, currentPath).ValueProvider.GetValue(value);
}
else
{
Expand Down
55 changes: 55 additions & 0 deletions src/3.0-JsonMergePatch.Tests/NewtonsoftJson/Patching/Attributes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Morcatko.AspNetCore.JsonMergePatch.NewtonsoftJson.Builders;
using Newtonsoft.Json;
using Xunit;

namespace Morcatko.AspNetCore.JsonMergePatch.Tests.NewtonsoftJson.Patching
{
public class Attributes
{
class ClassOne
{
[JsonProperty("id")]
public int Id { get; set; } = 1;

[JsonProperty("rec_id")]
public string RecId { get; set; } = "abc";
}

class ClassTwo
{
[JsonProperty("first_name")]
public string FirstName { get; set; }

[JsonProperty("last_name")]
public string LastName { get; set; }

[JsonProperty("obj_property")]
public ClassOne ObjProperty { get; set; } = new ClassOne();
}


private readonly PatchBuilder<ClassTwo> _patchBuilder = new PatchBuilder<ClassTwo>();

[Fact]
public void CanPatchTopLevelProperties()
{
var original = new ClassTwo();

var patch = _patchBuilder.Build("{'first_name': 'MyFirstName'}");
var result = patch.ApplyTo(original);

Assert.Equal("MyFirstName", result.FirstName);
}

[Fact]
public void CannotPatchSubObject()
{
var original = new ClassTwo();

var patch = _patchBuilder.Build("{'obj_property': { 'id': 3 }}");
var result = patch.ApplyTo(original);

Assert.Equal(3, result.ObjProperty.Id);
}
}
}

0 comments on commit e251cba

Please sign in to comment.