Skip to content

Commit

Permalink
Fixes an issue where dynamic JsonPatchDocument did not follow JsonNam…
Browse files Browse the repository at this point in the history
…ingPolicy for property names. Fixes #19
  • Loading branch information
Havunen committed Jul 16, 2023
1 parent da51501 commit 4fca64a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
49 changes: 48 additions & 1 deletion SystemTextJsonPatch.Tests/CustomNamingStrategyTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Text.Json;
using SystemTextJsonPatch.Internal;
using Xunit;

namespace SystemTextJsonPatch;
Expand Down Expand Up @@ -157,6 +159,51 @@ public void ReplacePropertyValueForExpandoObjectWithCustomNamingStrategy()
Assert.Equal(2, targetObject.customTest);
}

[Fact]
public void PatchDocumentOfTPathPropertyShouldFollowNamingPolicy()
{
var serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

var patch = new JsonPatchDocument<SimpleObject>(new(), serializerOptions);
patch.Replace((v) => v.StringProperty, "Bob");

var patchStr = JsonSerializer.Serialize(patch);

Assert.Equal("[{\"op\":\"replace\",\"path\":\"/stringProperty\",\"value\":\"Bob\"}]", patchStr);
}

[Fact]
public void PatchDocumentOfTPathPropertyShouldFollowNamingPolicyNested()
{
var serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

var patch = new JsonPatchDocument<SimpleObjectWithNestedObject>(new(), serializerOptions);
patch.Replace((v) => v.NestedObject.StringProperty, "Bob");

var patchStr = JsonSerializer.Serialize(patch);

Assert.Equal("[{\"op\":\"replace\",\"path\":\"/nestedObject/stringProperty\",\"value\":\"Bob\"}]", patchStr);
}

[Fact]
public void PatchDocumentOfTPathPropertyShouldNotChangeWhenNoOptionsDefined()
{
var serializerOptions = new JsonSerializerOptions();

var patch = new JsonPatchDocument<SimpleObject>(new(), serializerOptions);
patch.Replace((v) => v.StringProperty, "Bob");

var patchStr = JsonSerializer.Serialize(patch);

Assert.Equal("[{\"op\":\"replace\",\"path\":\"/StringProperty\",\"value\":\"Bob\"}]", patchStr);
}

private class TestNamingStrategy : JsonNamingPolicy
{
public override string ConvertName(string name)
Expand Down
13 changes: 10 additions & 3 deletions SystemTextJsonPatch/JsonPatchDocumentOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ private List<string> GetPathSegments(Expression expr)
var memberExpression = expr as MemberExpression;
listOfSegments.AddRange(GetPathSegments(memberExpression.Expression));
// Get property name, respecting JsonProperty attribute
listOfSegments.Add(JsonPatchDocument<TModel>.GetPropertyNameFromMemberExpression(memberExpression));
listOfSegments.Add(this.GetPropertyNameFromMemberExpression(memberExpression));
return listOfSegments;

case ExpressionType.Parameter:
Expand All @@ -842,7 +842,7 @@ private List<string> GetPathSegments(Expression expr)
}
}

private static string GetPropertyNameFromMemberExpression(MemberExpression memberExpression)
private string GetPropertyNameFromMemberExpression(MemberExpression memberExpression)
{
var jsonPropertyNameAttr = memberExpression.Member.GetCustomAttribute<JsonPropertyNameAttribute>();

Expand All @@ -851,7 +851,14 @@ private static string GetPropertyNameFromMemberExpression(MemberExpression membe
return jsonPropertyNameAttr.Name;
}

return memberExpression.Member.Name;
var memberName = memberExpression.Member.Name;

if (this.Options?.PropertyNamingPolicy != null)
{
return this.Options.PropertyNamingPolicy.ConvertName(memberName);
}

return memberName;
}


Expand Down

0 comments on commit 4fca64a

Please sign in to comment.