Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to patching decimals in JsonMergePatch.SystemText #41

Merged
merged 1 commit into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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