fix: handle arrays of generic types in GetFriendlyTypeName (issue - #7369) (#7400)

* BugFix 7369 - Properly resolve element type for arrays (e.g., List<string>[]) and append []

* PR comments updated

* PR for #7369
This commit is contained in:
minaxi98 2026-04-26 23:32:15 +05:30 committed by GitHub
parent 8ee0b43b34
commit b88af1e023
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 71 additions and 0 deletions

View file

@ -85,6 +85,14 @@ public static class TypeExtensions
/// </summary>
public static string GetFriendlyTypeName(this Type type, Brackets brackets)
{
if (type.IsArray)
{
var elementTypeName = GetFriendlyTypeName(type.GetElementType()!, brackets);
var rank = type.GetArrayRank();
var commas = rank > 1 ? new string(',', rank - 1) : string.Empty;
return elementTypeName + "[" + commas + "]";
}
if (!type.IsGenericType)
return type.FullName!;

View file

@ -0,0 +1,63 @@
using Elsa.Expressions.Models;
using Elsa.Extensions;
namespace Elsa.Expressions.UnitTests.Extensions;
public class TypeExtensionsTests
{
[Fact]
public void GetFriendlyTypeName_NonGenericType_ReturnsFullName()
{
var result = typeof(string).GetFriendlyTypeName(Brackets.Angle);
Assert.Equal(typeof(string).FullName, result);
}
[Fact]
public void GetFriendlyTypeName_GenericType_ReturnsFriendlyName()
{
var result = typeof(List<string>).GetFriendlyTypeName(Brackets.Angle);
Assert.Equal("System.Collections.Generic.List<System.String>", result);
}
[Fact]
public void GetFriendlyTypeName_ArrayOfGenericType_ReturnsFriendlyName()
{
var result = typeof(List<string>[]).GetFriendlyTypeName(Brackets.Angle);
Assert.Equal("System.Collections.Generic.List<System.String>[]", result);
}
[Fact]
public void GetFriendlyTypeName_ArrayOfNonGenericType_ReturnsFullNameWithBrackets()
{
var result = typeof(string[]).GetFriendlyTypeName(Brackets.Angle);
Assert.Equal("System.String[]", result);
}
[Fact]
public void GetFriendlyTypeName_NestedGenericArray_ReturnsFriendlyName()
{
var result = typeof(Dictionary<string, int>[]).GetFriendlyTypeName(Brackets.Angle);
Assert.Equal("System.Collections.Generic.Dictionary<System.String, System.Int32>[]", result);
}
[Fact]
public void GetFriendlyTypeName_MultiDimensionalArray_PreservesRank()
{
var result = typeof(int[,]).GetFriendlyTypeName(Brackets.Angle);
Assert.Equal("System.Int32[,]", result);
}
[Fact]
public void GetFriendlyTypeName_MultiDimensionalGenericArray_PreservesRank()
{
var result = typeof(List<string>[,]).GetFriendlyTypeName(Brackets.Angle);
Assert.Equal("System.Collections.Generic.List<System.String>[,]", result);
}
[Fact]
public void GetFriendlyTypeName_SquareBrackets_UsesCorrectBrackets()
{
var result = typeof(List<string>).GetFriendlyTypeName(Brackets.Square);
Assert.Equal("System.Collections.Generic.List[System.String]", result);
}
}