Skip to content

Commit f52b722

Browse files
Sync changes from main to net8 - Applied recent commits, updated versions & framework, preserved .csproj files (#528)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent e696b8c commit f52b722

File tree

2 files changed

+433
-5
lines changed

2 files changed

+433
-5
lines changed

src/TickerQ.Dashboard/Infrastructure/Dashboard/JsonExampleGenerator.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ internal static class JsonExampleGenerator
88
{
99
private static object GenerateExample(Type type) => Generate(type);
1010

11-
private static object Generate(Type type)
11+
private static object Generate(Type type, HashSet<Type>? visited = null, int depth = 0, int maxDepth = 2)
1212
{
13+
visited ??= new HashSet<Type>();
14+
1315
// Handle nullable types
1416
var underlyingType = Nullable.GetUnderlyingType(type);
1517
if (underlyingType != null)
1618
{
17-
return Generate(underlyingType);
19+
return Generate(underlyingType, visited, depth, maxDepth);
1820
}
1921

2022
// Handle primitive types
@@ -28,7 +30,7 @@ private static object Generate(Type type)
2830
{
2931
var elementType = type.GetElementType();
3032
var array = Array.CreateInstance(elementType!, 1);
31-
array.SetValue(Generate(elementType!), 0);
33+
array.SetValue(Generate(elementType!, visited, depth + 1, maxDepth), 0);
3234
return array;
3335
}
3436

@@ -38,23 +40,38 @@ private static object Generate(Type type)
3840
var elementType = type.GetGenericArguments()[0];
3941
var listType = typeof(List<>).MakeGenericType(elementType);
4042
var list = Activator.CreateInstance(listType);
41-
list!.GetType().GetMethod("Add")!.Invoke(list, new[] { Generate(elementType) });
43+
list!.GetType().GetMethod("Add")!.Invoke(list, new[] { Generate(elementType, visited, depth + 1, maxDepth) });
4244
return list;
4345
}
4446

4547
// Handle complex objects
4648
if (type.IsClass || type.IsValueType)
4749
{
50+
// Prevent infinite recursion for reference types and enforce maxDepth
51+
if (!type.IsValueType)
52+
{
53+
if (visited.Contains(type) || depth >= maxDepth)
54+
{
55+
// When a reference type has already been visited or the maximum depth is reached,
56+
// return the default value for the type instead of recursing further.
57+
return GetDefaultValue(type);
58+
}
59+
visited.Add(type);
60+
}
61+
4862
var instance = Activator.CreateInstance(type)!;
4963
foreach (var property in type.GetProperties())
5064
{
5165
if (property.CanWrite)
5266
{
53-
var value = Generate(property.PropertyType);
67+
var value = Generate(property.PropertyType, visited, depth + 1, maxDepth);
5468
property.SetValue(instance, value);
5569
}
5670
}
5771

72+
if (!type.IsValueType)
73+
visited.Remove(type);
74+
5875
return instance;
5976
}
6077

0 commit comments

Comments
 (0)