Additional tests for Generics in System.Text.Json.SourceGeneration (#72449)
authorIlya Pospelov <pos777@gmail.com>
Fri, 22 Jul 2022 10:17:17 +0000 (13:17 +0300)
committerGitHub <noreply@github.com>
Fri, 22 Jul 2022 10:17:17 +0000 (12:17 +0200)
* generic types tests

* test nested generic class serialization

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/ContextClasses.cs
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/TestClasses.cs

index 0765305..a36d06f 100644 (file)
@@ -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<T1> : JsonSerializerContext { }
+
+        public partial class NestedGenericContainer<T1>
+        {
+            [JsonSerializable(typeof(JsonMessage))]
+            public partial class NestedInNestedGenericContainerContext : JsonSerializerContext { }
+
+            [JsonSerializable(typeof(JsonMessage))]
+            public partial class NestedGenericInNestedGenericContainerContext<T2> : JsonSerializerContext { }
+        }
     }
 
     [JsonSerializable(typeof(MyContainingClass.MyNestedClass.MyNestedNestedClass))]
@@ -136,5 +148,6 @@ namespace System.Text.Json.SourceGeneration.Tests
     [JsonSerializable(typeof(MyContainingGenericClass<int>.MyNestedClass.MyNestedNestedGenericClass<int>))]
     [JsonSerializable(typeof(MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedClass))]
     [JsonSerializable(typeof(MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>))]
+    [JsonSerializable(typeof(MyContainingGenericClass<MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>>.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>))]
     internal partial class NestedGenericTypesContext : JsonSerializerContext { }
 }
index 6b533a8..af616d2 100644 (file)
@@ -24,9 +24,33 @@ namespace System.Text.Json.SourceGeneration.Tests
         [Fact]
         public static void VariousGenericsAreSupported()
         {
-            Assert.NotNull(GenericContext<object>.Default);
-            Assert.NotNull(ContextGenericContainer<object>.NestedInGenericContainerContext.Default);
+            AssertGenericContext(GenericContext<int>.Default);
+            AssertGenericContext(ContextGenericContainer<int>.NestedInGenericContainerContext.Default);
+            AssertGenericContext(ContextGenericContainer<int>.NestedGenericInGenericContainerContext<int>.Default);
+            AssertGenericContext(ContextGenericContainer<int>.NestedGenericContainer<int>.NestedInNestedGenericContainerContext.Default);
+            AssertGenericContext(ContextGenericContainer<int>.NestedGenericContainer<int>.NestedGenericInNestedGenericContainerContext<int>.Default);
+
             Assert.NotNull(NestedGenericTypesContext.Default);
+            var original = new MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>()
+            {
+                DataT = 1,
+                DataT1 = 10,
+                DataT2 = 100
+            };
+            Type type = typeof(MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>);
+            string json = JsonSerializer.Serialize(original, type, NestedGenericTypesContext.Default);
+            var deserialized = (MyContainingGenericClass<int>.MyNestedGenericClass<int>.MyNestedGenericNestedGenericClass<int>)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))]
index 1addbb2..8a424c8 100644 (file)
@@ -218,7 +218,12 @@ namespace System.Text.Json.SourceGeneration.Tests
         public class MyNestedGenericClass<T1>
         {
             public class MyNestedGenericNestedClass { }
-            public class MyNestedGenericNestedGenericClass<T2> { }
+            public class MyNestedGenericNestedGenericClass<T2>
+            {
+                public T DataT { get; set; }
+                public T1 DataT1 { get; set; }
+                public T2 DataT2 { get; set; }
+            }
         }
     }
 }