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 Arrays in SystemText #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 9 additions & 10 deletions src/3.0-JsonMergePatch.SystemText/Builders/PatchBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Morcatko.AspNetCore.JsonMergePatch.Internal;
using System;
using System.Linq;
using System.Reflection;
using System.Text.Json;

Expand All @@ -14,25 +15,23 @@ private static object ToObject(this JsonElement jsonElement)
case JsonValueKind.Null: return null;
case JsonValueKind.String: return jsonElement.GetString();
case JsonValueKind.Number: return jsonElement.GetGenericNumber();
case JsonValueKind.Array: return jsonElement.EnumerateArray().Select(j => j.ToObject()).ToArray();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leads to an exception when an array contains complex objects. When ToObject is called with such array element, it throws NotSupportedException.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did any of you manage to make some progress with this?
@OleksandrHorbunovVP what was the end solution that you used?
Currently working on a project where this would be useful but immediately hit this problem.

case JsonValueKind.True: return true;
case JsonValueKind.False: return false;
case JsonValueKind.Undefined:
case JsonValueKind.Object:
case JsonValueKind.Array:
default:
throw new NotSupportedException($"Unsupported ValueKind - {jsonElement.ValueKind}");
}
}

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 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)
Expand Down
29 changes: 25 additions & 4 deletions src/3.0-JsonMergePatch.Tests/Integration/MvcTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ await p.MergePatchAsync(null, new[]
[MemberData(nameof(GetCombinations))]
public async Task NullableDecimalFloatingPoint(bool core, bool newtonsoft)
{
using (var p = new TestHelper(core, newtonsoft)) {
using (var p = new TestHelper(core, newtonsoft))
{
await p.PostAsync("0", p.GetTestModel());

await p.MergePatchAsync(null, new[]
Expand All @@ -133,11 +134,31 @@ await p.MergePatchAsync(null, new[]

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

[Theory]
[MemberData(nameof(GetCombinations))]
public async Task Arrays(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, ArrayOfFloats = new [] { 7, 3.5f }}
});

var patchedModel = await p.GetAsync("0");
var expected = p.GetTestModel();
expected.ArrayOfFloats = new [] { 7, 3.5f };
Assert.Equal(expected, patchedModel);
}
}

[Theory(Skip = "Does not work")]
[MemberData(nameof(GetCombinations))]
public async Task MissingRequiredProperty(bool core, bool newtonsoft)
Expand All @@ -152,8 +173,8 @@ public async Task MissingRequiredProperty(bool core, bool newtonsoft)
expected.Integer = 8;
Assert.Equal(expected, patchedModel);
}
}

}
#region ValueEnum
[Theory(Skip = "Enums do not work - https://github.com/aspnet/Home/issues/2423")]
[MemberData(nameof(GetCombinations))]
Expand Down
5 changes: 4 additions & 1 deletion src/3.0-JsonMergePatch.Tests/TestModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class TestModelBase : IEquatable<TestModelBase>, IEquatable<NewtonsoftTes

public decimal? NullableDecimal { get; set; }

public float[] ArrayOfFloats { get; set; } = new float[0];

public Dictionary<string, SubModel> SubModels { get; set; } = new Dictionary<string, SubModel>();

public bool Equals(TestModelBase other)
Expand All @@ -43,7 +45,8 @@ public bool Equals(TestModelBase other)
&& this.ValueEnum == other.ValueEnum
&& this.NullableDecimal == other.NullableDecimal
&& this.Date == other.Date
&& this.Date.GetValueOrDefault().Offset == other.Date.GetValueOrDefault().Offset
&& this.Date.GetValueOrDefault().Offset == other.Date.GetValueOrDefault().Offset
&& Enumerable.SequenceEqual(this.ArrayOfFloats, other.ArrayOfFloats)
&& Enumerable.SequenceEqual(this.SubModels?.Keys, other.SubModels?.Keys)
&& Enumerable.SequenceEqual(this.SubModels?.Values, other.SubModels?.Values)
&& ((this.SubModel == other.SubModel)
Expand Down