Skip to content

Commit

Permalink
New test that JsonIncludeAttribute is honored during deserialization
Browse files Browse the repository at this point in the history
ArgumentDeserialization_Honors_JsonInclude test is added dotnet#47855
  • Loading branch information
Sychev Vadim committed Feb 5, 2021
1 parent c7c734e commit 4e76f0d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,19 @@ 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);

string json = JsonSerializer.Serialize(point);
Assert.Contains(@"""X"":1", json);
Assert.Contains(@"""Y"":2", json);

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

[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[15];

// 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,11 @@ 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));
// 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[12] = Task.Run(async () => await RunTestAsync<Point_With_Array>(Point_With_Array.s_data));
tasks[13] = Task.Run(async () => await RunTestAsync<Point_With_Dictionary>(Point_With_Dictionary.s_data));
tasks[14] = 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 @@ -2060,6 +2060,28 @@ public void Verify()
}
}

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

[JsonInclude]
public int Y { get; }

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

public void Initialize() { }

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

public static readonly byte[] s_data = Encoding.UTF8.GetBytes(s_json);
public void Verify()
{
Assert.Equal(1, X);
Assert.Equal(2, Y);
}
}

public class Point_MultipleMembers_BindTo_OneConstructorParameter
{
public int X { get; }
Expand Down

0 comments on commit 4e76f0d

Please sign in to comment.