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

New tests that JsonIncludeAttribute and JsonNumberHandlingAttribute are honored during deserialization #47904

Merged
merged 3 commits into from
Mar 3, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,36 @@ public async Task HonorExtensionDataGeneric()
Assert.Equal("value", ((JsonElement)obj4.ExtensionData["key"]).GetString());
}

[Fact]
public async Task ArgumentDeserialization_Honors_JsonInclude()
{
Point_MembersHave_JsonInclude point = new Point_MembersHave_JsonInclude(1, 2,3);

string json = JsonSerializer.Serialize(point);
Assert.Contains(@"""X"":1", json);
Assert.Contains(@"""Y"":2", json);
//We should add another test for non-public members
//when https://github.com/dotnet/runtime/issues/31511 is implemented
Assert.Contains(@"""Z"":3", json);

point = await Serializer.DeserializeWrapper<Point_MembersHave_JsonInclude>(json);
point.Verify();
}

[Fact]
public async Task ArgumentDeserialization_Honors_JsonNumberHandling()
{
ClassWithFiveArgs_MembersHave_JsonNumberHandlingAttributes obj = await Serializer.DeserializeWrapper<ClassWithFiveArgs_MembersHave_JsonNumberHandlingAttributes>(ClassWithFiveArgs_MembersHave_JsonNumberHandlingAttributes.s_json);
obj.Verify();

string json = JsonSerializer.Serialize(obj);
Assert.Contains(@"""A"":1", json);
Assert.Contains(@"""B"":""NaN""", json);
Assert.Contains(@"""C"":2", json);
Assert.Contains(@"""D"":""3""", json);
Assert.Contains(@"""E"":""4""", json);
}

[Fact]
public async Task ArgumentDeserialization_Honors_JsonPropertyName()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async Task RunTestAsync<T>(byte[] testData)
}

// Array size is the count of the following tests.
Task[] tasks = new Task[14];
Task[] tasks = new Task[16];

// Simple models can be deserialized.
tasks[0] = Task.Run(async () => await RunTestAsync<Parameterized_IndexViewModel_Immutable>(Parameterized_IndexViewModel_Immutable.s_data));
Expand All @@ -48,10 +48,12 @@ async Task RunTestAsync<T>(byte[] testData)
tasks[8] = Task.Run(async () => await RunTestAsync<Point_MembersHave_JsonPropertyName>(Point_MembersHave_JsonPropertyName.s_data));
tasks[9] = Task.Run(async () => await RunTestAsync<Point_MembersHave_JsonConverter>(Point_MembersHave_JsonConverter.s_data));
tasks[10] = Task.Run(async () => await RunTestAsync<Point_MembersHave_JsonIgnore>(Point_MembersHave_JsonIgnore.s_data));
tasks[11] = Task.Run(async () => await RunTestAsync<Point_MembersHave_JsonInclude>(Point_MembersHave_JsonInclude.s_data));
tasks[12] = Task.Run(async () => await RunTestAsync<ClassWithFiveArgs_MembersHave_JsonNumberHandlingAttributes>(ClassWithFiveArgs_MembersHave_JsonNumberHandlingAttributes.s_data));
// Complex JSON as last argument works
tasks[11] = Task.Run(async () => await RunTestAsync<Point_With_Array>(Point_With_Array.s_data));
tasks[12] = Task.Run(async () => await RunTestAsync<Point_With_Dictionary>(Point_With_Dictionary.s_data));
tasks[13] = Task.Run(async () => await RunTestAsync<Point_With_Object>(Point_With_Object.s_data));
tasks[13] = Task.Run(async () => await RunTestAsync<Point_With_Array>(Point_With_Array.s_data));
tasks[14] = Task.Run(async () => await RunTestAsync<Point_With_Dictionary>(Point_With_Dictionary.s_data));
tasks[15] = Task.Run(async () => await RunTestAsync<Point_With_Object>(Point_With_Object.s_data));

await Task.WhenAll(tasks);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,67 @@ public void Verify()
Assert.Equal(0, Y); // We don't set parameter default value here.
}
}

public class Point_MembersHave_JsonInclude : ITestClass
{
[JsonInclude]
public int X { get; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Add another property that has a non-public accessor, without the attribute, to show that deserialization works regardless. Then, add a comment that says we should add another test for non-public members when #31511 is implemented.


[JsonInclude]
public int Y { get; private set; }

public int Z { get; private set; }

public Point_MembersHave_JsonInclude(int x, int y, int z) => (X, Y, Z) = (x, y, z);

public void Initialize() { }

public static readonly string s_json = @"{""X"":1,""Y"":2,""Z"":3}";

public static readonly byte[] s_data = Encoding.UTF8.GetBytes(s_json);

public void Verify()
SkiFoD marked this conversation as resolved.
Show resolved Hide resolved
{
Assert.Equal(1, X);
Assert.Equal(2, Y);
Assert.Equal(3, Z);
}
}

public class ClassWithFiveArgs_MembersHave_JsonNumberHandlingAttributes : ITestClass
{
[JsonNumberHandling(JsonNumberHandling.Strict)]
public int A { get; }

[JsonNumberHandling(JsonNumberHandling.AllowNamedFloatingPointLiterals)]
public float B { get; }

[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
public int C { get; }

[JsonNumberHandling(JsonNumberHandling.WriteAsString)]
public int D { get; }

[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)]
public int E { get; }

public ClassWithFiveArgs_MembersHave_JsonNumberHandlingAttributes(int a, float b, int c, int d, int e) => (A, B, C, D, E) = (a, b, c, d, e);

public void Initialize() { }
SkiFoD marked this conversation as resolved.
Show resolved Hide resolved

public static readonly string s_json = @"{""A"":1,""B"":""NaN"",""C"":""2"",""D"": 3,""E"":""4""}";

public static readonly byte[] s_data = Encoding.UTF8.GetBytes(s_json);

public void Verify()
SkiFoD marked this conversation as resolved.
Show resolved Hide resolved
{
Assert.Equal(1, A);
Assert.Equal(float.NaN, B);
Assert.Equal(2, C);
Assert.Equal(3, D);
Assert.Equal(4, E);
}
}

public class Point_MultipleMembers_BindTo_OneConstructorParameter
{
Expand Down