From: Layomi Akinrinade Date: Fri, 7 Aug 2020 15:36:19 +0000 (-0700) Subject: [JsonSerializer] Verify null can't be assigned to non-nullable structs (#40446) X-Git-Tag: submit/tizen/20210909.063632~6154 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=03022564c9e5e9a18330432c2d7fa8316256b599;p=platform%2Fupstream%2Fdotnet%2Fruntime.git [JsonSerializer] Verify null can't be assigned to non-nullable structs (#40446) --- diff --git a/src/libraries/System.Text.Json/tests/Serialization/Array.ReadTests.cs b/src/libraries/System.Text.Json/tests/Serialization/Array.ReadTests.cs index 780c8c1..6f91753 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/Array.ReadTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/Array.ReadTests.cs @@ -616,8 +616,7 @@ namespace System.Text.Json.Serialization.Tests Assert.Equal(0, obj.MyImmutableList.Count); TestRoundTrip(obj); - // TODO: Skip ImmutableArray due to https://github.com/dotnet/runtime/issues/1037. - const string inputJsonWithNullCollections = + string inputJsonWithNullCollections = @"{ ""Array"":null, ""List"":null, @@ -627,6 +626,10 @@ namespace System.Text.Json.Serialization.Tests obj = JsonSerializer.Deserialize(inputJsonWithNullCollections); TestRoundTrip(obj); + + // ImmutableArray is a struct and cannot be null. + inputJsonWithNullCollections = @"{""MyImmutableArray"":null}"; + Assert.Throws(() => JsonSerializer.Deserialize(inputJsonWithNullCollections)); } [Fact] diff --git a/src/libraries/System.Text.Json/tests/Serialization/Null.ReadTests.cs b/src/libraries/System.Text.Json/tests/Serialization/Null.ReadTests.cs index df8715e..0f32389 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/Null.ReadTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/Null.ReadTests.cs @@ -168,25 +168,33 @@ namespace System.Text.Json.Serialization.Tests Assert.Equal('Y', JsonSerializer.Deserialize("\"\u0059\"")); } - [ActiveIssue("https://github.com/dotnet/runtime/issues/1037")] [Fact] public static void ParseNullStringToStructShouldThrowJsonException() { string nullString = "null"; - Utf8JsonReader reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(nullString)); + byte[] nullStringAsBytes = Encoding.UTF8.GetBytes(nullString); + + Utf8JsonReader reader = new Utf8JsonReader(nullStringAsBytes); JsonTestHelper.AssertThrows(reader, (reader) => JsonSerializer.Deserialize(ref reader)); - Assert.Throws(() => JsonSerializer.Deserialize(Encoding.UTF8.GetBytes(nullString))); + Assert.Throws(() => JsonSerializer.Deserialize(nullStringAsBytes)); Assert.Throws(() => JsonSerializer.Deserialize(nullString)); + + // null can be assigned to nullable structs. + Assert.Null(JsonSerializer.Deserialize(nullStringAsBytes)); + Assert.Null(JsonSerializer.Deserialize(nullString)); } - [ActiveIssue("https://github.com/dotnet/runtime/issues/1037")] [Fact] public static async Task ParseNullStringShouldThrowJsonExceptionAsync() { using (var stream = new MemoryStream(Encoding.UTF8.GetBytes("null"))) { await Assert.ThrowsAsync(async () => await JsonSerializer.DeserializeAsync(stream)); + + // null can be assigned to nullable structs. + stream.Position = 0; + Assert.Null(await JsonSerializer.DeserializeAsync(stream)); } }