From: Ilya Pospelov Date: Fri, 22 Jul 2022 10:17:17 +0000 (+0300) Subject: Additional tests for Generics in System.Text.Json.SourceGeneration (#72449) X-Git-Tag: accepted/tizen/unified/riscv/20231226.055536~7631 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ea2c907f8a28e85c0c21d62c44d73848509b3c46;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Additional tests for Generics in System.Text.Json.SourceGeneration (#72449) * generic types tests * test nested generic class serialization --- diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs index 0765305..a36d06f 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs @@ -126,6 +126,18 @@ namespace System.Text.Json.SourceGeneration.Tests { [JsonSerializable(typeof(JsonMessage))] public partial class NestedInGenericContainerContext : JsonSerializerContext { } + + [JsonSerializable(typeof(JsonMessage))] + public partial class NestedGenericInGenericContainerContext : JsonSerializerContext { } + + public partial class NestedGenericContainer + { + [JsonSerializable(typeof(JsonMessage))] + public partial class NestedInNestedGenericContainerContext : JsonSerializerContext { } + + [JsonSerializable(typeof(JsonMessage))] + public partial class NestedGenericInNestedGenericContainerContext : JsonSerializerContext { } + } } [JsonSerializable(typeof(MyContainingClass.MyNestedClass.MyNestedNestedClass))] @@ -136,5 +148,6 @@ namespace System.Text.Json.SourceGeneration.Tests [JsonSerializable(typeof(MyContainingGenericClass.MyNestedClass.MyNestedNestedGenericClass))] [JsonSerializable(typeof(MyContainingGenericClass.MyNestedGenericClass.MyNestedGenericNestedClass))] [JsonSerializable(typeof(MyContainingGenericClass.MyNestedGenericClass.MyNestedGenericNestedGenericClass))] + [JsonSerializable(typeof(MyContainingGenericClass.MyNestedGenericClass.MyNestedGenericNestedGenericClass>.MyNestedGenericClass.MyNestedGenericNestedGenericClass))] internal partial class NestedGenericTypesContext : JsonSerializerContext { } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs index 6b533a8..af616d2 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs @@ -24,9 +24,33 @@ namespace System.Text.Json.SourceGeneration.Tests [Fact] public static void VariousGenericsAreSupported() { - Assert.NotNull(GenericContext.Default); - Assert.NotNull(ContextGenericContainer.NestedInGenericContainerContext.Default); + AssertGenericContext(GenericContext.Default); + AssertGenericContext(ContextGenericContainer.NestedInGenericContainerContext.Default); + AssertGenericContext(ContextGenericContainer.NestedGenericInGenericContainerContext.Default); + AssertGenericContext(ContextGenericContainer.NestedGenericContainer.NestedInNestedGenericContainerContext.Default); + AssertGenericContext(ContextGenericContainer.NestedGenericContainer.NestedGenericInNestedGenericContainerContext.Default); + Assert.NotNull(NestedGenericTypesContext.Default); + var original = new MyContainingGenericClass.MyNestedGenericClass.MyNestedGenericNestedGenericClass() + { + DataT = 1, + DataT1 = 10, + DataT2 = 100 + }; + Type type = typeof(MyContainingGenericClass.MyNestedGenericClass.MyNestedGenericNestedGenericClass); + string json = JsonSerializer.Serialize(original, type, NestedGenericTypesContext.Default); + var deserialized = (MyContainingGenericClass.MyNestedGenericClass.MyNestedGenericNestedGenericClass)JsonSerializer.Deserialize(json, type, NestedGenericTypesContext.Default); + Assert.Equal(1, deserialized.DataT); + Assert.Equal(10, deserialized.DataT1); + Assert.Equal(100, deserialized.DataT2); + + static void AssertGenericContext(JsonSerializerContext context) + { + Assert.NotNull(context); + string json = JsonSerializer.Serialize(new JsonMessage { Message = "Hi" }, typeof(JsonMessage), context); + JsonMessage deserialized = (JsonMessage)JsonSerializer.Deserialize(json, typeof(JsonMessage), context); + Assert.Equal("Hi", deserialized.Message); + } } [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs index 1addbb2..8a424c8 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs @@ -218,7 +218,12 @@ namespace System.Text.Json.SourceGeneration.Tests public class MyNestedGenericClass { public class MyNestedGenericNestedClass { } - public class MyNestedGenericNestedGenericClass { } + public class MyNestedGenericNestedGenericClass + { + public T DataT { get; set; } + public T1 DataT1 { get; set; } + public T2 DataT2 { get; set; } + } } } }