Skip to content
This repository has been archived by the owner on Nov 15, 2018. It is now read-only.

Commit

Permalink
[Fixes #50] JsonPatchDocument.Replace() yields invalid path when [Jso…
Browse files Browse the repository at this point in the history
…nProperty] is used (1.1.0)
  • Loading branch information
kichalla committed Dec 28, 2016
1 parent c8c2dfa commit 683fb8f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static string GetPropertyNameFromMemberExpression(MemberExpression membe
}
// get value
var castedAttribute = (JsonPropertyAttribute)jsonPropertyAttribute;
return castedAttribute.PropertyName;
return castedAttribute.PropertyName ?? memberExpression.Member.Name;
}

private static bool ContinueWithSubPath(ExpressionType expressionType, bool firstTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,30 @@ public void Add_WithExpression_RespectsJsonPropertyName_ForModelProperty()
Assert.Equal(pathToCheck, "/anothername");
}

[Fact]
public void Add_WithExpression_FallsbackToPropertyName_WhenJsonPropertyName_IsEmpty()
{
// Arrange
var patchDoc = new JsonPatchDocument<JsonPropertyWithNoPropertyName>();
patchDoc.Add(m => m.Description, "Test");
var serialized = JsonConvert.SerializeObject(patchDoc);

// Act
var deserialized =
JsonConvert.DeserializeObject<JsonPatchDocument<JsonPropertyWithNoPropertyName>>(serialized);

// Assert
var pathToCheck = deserialized.Operations.First().path;
Assert.Equal("/description", pathToCheck);
}

[Fact]
public void Add_WithExpression_RespectsJsonPropertyName_WhenApplyingToDifferentlyTypedClassWithPropertyMatchingJsonPropertyName()
{
var patchDocToSerialize = new JsonPatchDocument<JsonPropertyDTO>();
patchDocToSerialize.Add(p => p.Name, "John");

// the patchdoc will deserialize to "anothername". We should thus be able to apply
// 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 JsonPropertyWithAnotherNameDTO()
{
Expand Down Expand Up @@ -81,7 +98,7 @@ public void Add_OnApplyFromJson_RespectsJsonPropertyNameOnJsonDocument()
{
Name = "InitialValue"
};

// serialization should serialize to "AnotherName"
var serialized = "[{\"value\":\"Kevin\",\"path\":\"/AnotherName\",\"op\":\"add\"}]";
var deserialized =
Expand Down Expand Up @@ -167,5 +184,11 @@ public void Add_OnApplyFromJson_EscapingHandledOnComplexJsonPropertyNameOnJsonDo
Assert.Equal("Kevin", doc.FooSlashBars);
Assert.Equal("Final Value", doc.FooSlashTilde.StringProperty);
}

private class JsonPropertyWithNoPropertyName
{
[JsonProperty]
public string Description { get; set; }
}
}
}

0 comments on commit 683fb8f

Please sign in to comment.