Skip to content

Commit b619c9c

Browse files
Merge pull request #9 from drasticactions/dev/list-type
Fix generation of nested generic list types
2 parents f1e9709 + 3cd2a59 commit b619c9c

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
# dotenv files
77
.env
8+
*.lscache
89

910
# User-specific files
1011
*.rsuser

src/CarpaNet.SourceGen/CarpaNet.SourceGen.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
<IsPackable>false</IsPackable>
1212
</PropertyGroup>
1313

14+
<ItemGroup>
15+
<InternalsVisibleTo Include="CarpaNet.UnitTests, PublicKey=00240000048000009400000006020000002400005253413100040000010001005311bd88d09f39da8d0a7814421594e8769c2c6af9b162a58a5c531868178ee6f8c3a108ffef4035eebe195bc06615043564b1c319b5b83be9ee8fae7f99a5b0b52cab19f8aabb66acbe9384dfda7a5d359e6b34ac3f7907db45f2c0c33c2bb4290ca55adc414a1823888a92d9b9f61c32f48d391be17000f3032e0b5146b48b" />
16+
</ItemGroup>
17+
1418
<ItemGroup>
1519
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" />
1620
<PackageReference Include="System.Text.Json" PrivateAssets="all" GeneratePathProperty="true" />

src/CarpaNet.SourceGen/Generation/JsonContextGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ private static void GenerateListTypeFactory(
799799
dispatchLines.Add($"if (type == typeof({globalList})) return Create_BuiltIn_{safeName}_TypeInfo(options);");
800800
}
801801

802-
private static string ToBuiltInSuffix(string typeName)
802+
internal static string ToBuiltInSuffix(string typeName)
803803
{
804804
return typeName switch
805805
{
@@ -809,7 +809,7 @@ private static string ToBuiltInSuffix(string typeName)
809809
"int" => "Int32",
810810
"byte[]" => "ByteArray",
811811
"object" => "Object",
812-
_ => typeName.Replace(".", "_").Replace("@", "")
812+
_ => typeName.Replace(".", "_").Replace("@", "").Replace("<", "_").Replace(">", "_")
813813
};
814814
}
815815
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using CarpaNet.Generation;
2+
3+
using Xunit;
4+
5+
namespace CarpaNet.UnitTests.Generation;
6+
7+
public class JsonContextGeneratorTests
8+
{
9+
[Theory]
10+
[InlineData("string", "String")]
11+
[InlineData("long", "Int64")]
12+
[InlineData("bool", "Boolean")]
13+
[InlineData("int", "Int32")]
14+
[InlineData("byte[]", "ByteArray")]
15+
[InlineData("object", "Object")]
16+
[InlineData("System.DateTimeOffset", "System_DateTimeOffset")]
17+
[InlineData("System.Collections.Generic.List<string>", "System_Collections_Generic_List_string_")]
18+
public void ToBuiltInSuffix_ProducesValidIdentifier(string typeName, string expected)
19+
{
20+
var result = JsonContextGenerator.ToBuiltInSuffix(typeName);
21+
Assert.Equal(expected, result);
22+
}
23+
24+
[Fact]
25+
public void ToBuiltInSuffix_NestedGenericType_SanitizesAngleBrackets()
26+
{
27+
var result = JsonContextGenerator.ToBuiltInSuffix("System.Collections.Generic.List<string>");
28+
29+
Assert.DoesNotContain("<", result);
30+
Assert.DoesNotContain(">", result);
31+
Assert.Equal("System_Collections_Generic_List_string_", result);
32+
}
33+
}

0 commit comments

Comments
 (0)