Skip to content

Commit

Permalink
Fix to patching decimals in JsonMergePatch.SystemText (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteadman authored Nov 11, 2020
1 parent e251cba commit c1b2549
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/3.0-JsonMergePatch.SystemText/Builders/PatchBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private static object ToObject(this JsonElement jsonElement)
{
case JsonValueKind.Null: return null;
case JsonValueKind.String: return jsonElement.GetString();
case JsonValueKind.Number: return jsonElement.GetInt64();
case JsonValueKind.Number: return jsonElement.GetGenericNumber();
case JsonValueKind.True: return true;
case JsonValueKind.False: return false;
case JsonValueKind.Undefined:
Expand All @@ -24,6 +24,17 @@ private static object ToObject(this JsonElement jsonElement)
}
}

private static object GetGenericNumber (this JsonElement jsonElement)
{

// Attempt to parse the JSON Element as an Int32 first
if (jsonElement.TryGetInt32(out int int32)) return int32;

// Failing that, parse it as a Decimal instead
return jsonElement.GetDecimal();

}

private static bool IsValue(this JsonValueKind valueKind)
=> (valueKind == JsonValueKind.False)
|| (valueKind == JsonValueKind.True)
Expand Down
19 changes: 19 additions & 0 deletions src/3.0-JsonMergePatch.Tests/Integration/MvcTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ await p.MergePatchAsync(null, new[]
}
}

[Theory]
[MemberData(nameof(GetCombinations))]
public async Task NullableDecimalFloatingPoint(bool core, bool newtonsoft)
{
using (var p = new TestHelper(core, newtonsoft)) {
await p.PostAsync("0", p.GetTestModel());

await p.MergePatchAsync(null, new[]
{
new { id = 0, NullableDecimal = 7.5 }
});

var patchedModel = await p.GetAsync("0");
var expected = p.GetTestModel();
expected.NullableDecimal = (decimal?) 7.5;
Assert.Equal(expected, patchedModel);
}
}

[Theory(Skip = "Does not work")]
[MemberData(nameof(GetCombinations))]
public async Task MissingRequiredProperty(bool core, bool newtonsoft)
Expand Down

0 comments on commit c1b2549

Please sign in to comment.