Add regression testing for #76802. (#87632)
authorEirik Tsarpalis <eirik.tsarpalis@gmail.com>
Thu, 15 Jun 2023 21:26:38 +0000 (22:26 +0100)
committerGitHub <noreply@github.com>
Thu, 15 Jun 2023 21:26:38 +0000 (22:26 +0100)
src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.cs
src/libraries/System.Text.Json/tests/Common/TestClasses/TestClasses.cs
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/CollectionTests.cs

index ce69e97..5c4d119 100644 (file)
@@ -1,11 +1,48 @@
 // Licensed to the .NET Foundation under one or more agreements.
 // The .NET Foundation licenses this file to you under the MIT license.
 
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Xunit;
+
 namespace System.Text.Json.Serialization.Tests
 {
     public abstract partial class CollectionTests : SerializerTests
     {
         public CollectionTests(JsonSerializerWrapper stringSerializerWrapper)
             : base(stringSerializerWrapper) { }
+
+        [Fact]
+        public async Task RoundtripClassWithRecursiveCollectionProperties()
+        {
+            // Regression test for https://github.com/dotnet/runtime/issues/76802
+
+            var value = new ClassWithRecursiveCollectionTypes
+            {
+                Nested = new(),
+                List = new() { new() },
+                Dictionary = new Dictionary<string, ClassWithRecursiveCollectionTypes>
+                {
+                    ["key"] = new()
+                },
+            };
+
+            string expectedJson = """
+                {
+                    "Nested" : {"Nested":null,"List":null,"Dictionary":null},
+                    "List" : [{"Nested":null,"List":null,"Dictionary":null}],
+                    "Dictionary" : {"key" : {"Nested":null,"List":null,"Dictionary":null}}
+                }
+                """;
+
+            string json = await Serializer.SerializeWrapper(value);
+            JsonTestHelper.AssertJsonEqual(expectedJson, json);
+
+            value = await Serializer.DeserializeWrapper<ClassWithRecursiveCollectionTypes>(json);
+            Assert.NotNull(value.Nested);
+            Assert.Null(value.Nested.Nested);
+            Assert.NotNull(value.List);
+            Assert.NotNull(value.Dictionary);
+        }
     }
 }
index 9377445..dfe51ae 100644 (file)
@@ -2286,4 +2286,11 @@ namespace System.Text.Json.Serialization.Tests
             Document.Dispose();
         }
     }
+
+    public class ClassWithRecursiveCollectionTypes
+    {
+        public ClassWithRecursiveCollectionTypes? Nested { get; set; }
+        public List<ClassWithRecursiveCollectionTypes> List { get; set; }
+        public IReadOnlyDictionary<string, ClassWithRecursiveCollectionTypes>? Dictionary { get; set; }
+    }
 }
index a9b785a..43d8bf9 100644 (file)
@@ -453,6 +453,7 @@ namespace System.Text.Json.SourceGeneration.Tests
         [JsonSerializable(typeof(KeyValuePair<int, int>))]
         [JsonSerializable(typeof(KeyValuePair<string, KeyValuePair<string, int>>))]
         [JsonSerializable(typeof(StackWrapper))]
+        [JsonSerializable(typeof(ClassWithRecursiveCollectionTypes))]
         internal sealed partial class CollectionTestsContext_Metadata : JsonSerializerContext
         {
         }
@@ -855,6 +856,7 @@ namespace System.Text.Json.SourceGeneration.Tests
         [JsonSerializable(typeof(KeyValuePair<int, int>))]
         [JsonSerializable(typeof(KeyValuePair<string, KeyValuePair<string, int>>))]
         [JsonSerializable(typeof(StackWrapper))]
+        [JsonSerializable(typeof(ClassWithRecursiveCollectionTypes))]
         internal sealed partial class CollectionTestsContext_Default : JsonSerializerContext
         {
         }