Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2286,4 +2286,11 @@ public void Dispose()
Document.Dispose();
}
}

public class ClassWithRecursiveCollectionTypes
{
public ClassWithRecursiveCollectionTypes? Nested { get; set; }
public List<ClassWithRecursiveCollectionTypes> List { get; set; }
public IReadOnlyDictionary<string, ClassWithRecursiveCollectionTypes>? Dictionary { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ public async Task DeserializeAsyncEnumerable()
[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
{
}
Expand Down Expand Up @@ -855,6 +856,7 @@ public CollectionTests_Default()
[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
{
}
Expand Down