Skip to content

Commit 40c64e8

Browse files
CronKzmichaelstaib
authored andcommitted
Fixed source-generated class names (#7819)
1 parent 3d5716c commit 40c64e8

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

src/HotChocolate/Core/src/Types.Analyzers/Generators/DataLoaderModuleGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void Generate(
8181
{
8282
if (syntaxInfo is DataLoaderModuleInfo module)
8383
{
84-
return module;
84+
return new DataLoaderModuleInfo(GeneratorUtils.SanitizeIdentifier(module.ModuleName));
8585
}
8686
}
8787

src/HotChocolate/Core/src/Types.Analyzers/Helpers/GeneratorUtils.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Immutable;
2+
using System.Text.RegularExpressions;
23
using HotChocolate.Types.Analyzers.Models;
34
using Microsoft.CodeAnalysis;
45

@@ -16,7 +17,7 @@ public static ModuleInfo GetModuleInfo(
1617
if (syntaxInfo is ModuleInfo module)
1718
{
1819
defaultModule = false;
19-
return module;
20+
return new ModuleInfo(SanitizeIdentifier(module.ModuleName), module.Options);
2021
}
2122
}
2223

@@ -61,7 +62,7 @@ public static DataLoaderDefaultsInfo GetDataLoaderDefaults(
6162
public static string CreateModuleName(string? assemblyName)
6263
=> assemblyName is null
6364
? "AssemblyTypes"
64-
: assemblyName.Split('.').Last() + "Types";
65+
: SanitizeIdentifier(assemblyName.Split('.').Last()) + "Types";
6566

6667
public static string ConvertDefaultValueToString(object? defaultValue, ITypeSymbol type)
6768
{
@@ -104,4 +105,18 @@ public static string ConvertDefaultValueToString(object? defaultValue, ITypeSymb
104105

105106
return defaultValue.ToString();
106107
}
108+
109+
public static string SanitizeIdentifier(string input)
110+
{
111+
Regex invalidCharsRegex = new("[^a-zA-Z0-9]", RegexOptions.Compiled);
112+
113+
var sanitized = invalidCharsRegex.Replace(input, "_");
114+
115+
if (!char.IsLetter(sanitized[0]))
116+
{
117+
sanitized = "_" + sanitized.Substring(1);
118+
}
119+
120+
return sanitized;
121+
}
107122
}

src/HotChocolate/Core/test/Types.Analyzers.Tests/TestHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static Snapshot GetGeneratedSourceSnapshot([StringSyntax("csharp")] strin
2424
return GetGeneratedSourceSnapshot([sourceText]);
2525
}
2626

27-
public static Snapshot GetGeneratedSourceSnapshot(string[] sourceTexts)
27+
public static Snapshot GetGeneratedSourceSnapshot(string[] sourceTexts, string? assemblyName = "Tests")
2828
{
2929
IEnumerable<PortableExecutableReference> references =
3030
[
@@ -49,7 +49,7 @@ public static Snapshot GetGeneratedSourceSnapshot(string[] sourceTexts)
4949

5050
// Create a Roslyn compilation for the syntax tree.
5151
var compilation = CSharpCompilation.Create(
52-
assemblyName: "Tests",
52+
assemblyName: assemblyName,
5353
syntaxTrees: sourceTexts.Select(s => CSharpSyntaxTree.ParseText(s)),
5454
references);
5555

src/HotChocolate/Core/test/Types.Analyzers.Tests/TypeModuleSyntaxGeneratorTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,20 @@ public static async Task<IReadOnlyDictionary<int, object>> GetObjectByIdAAsync(
157157
"""
158158
]).MatchMarkdownAsync();
159159
}
160+
161+
[Fact]
162+
public async Task GenerateSource_With_Problematic_Assembly_Name_MatchesSnapshot()
163+
{
164+
await TestHelper.GetGeneratedSourceSnapshot(
165+
[
166+
"""
167+
using HotChocolate.Types;
168+
169+
namespace TestNamespace;
170+
171+
internal class ATestBType: ObjectType<ATestB>;
172+
internal record ATestB(int Id);
173+
"""
174+
], assemblyName:"Custom-Module").MatchMarkdownAsync();
175+
}
160176
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# GenerateSource_With_Problematic_Assembly_Name_MatchesSnapshot
2+
3+
```csharp
4+
// <auto-generated/>
5+
6+
#nullable enable
7+
#pragma warning disable
8+
9+
using System;
10+
using System.Runtime.CompilerServices;
11+
using HotChocolate;
12+
using HotChocolate.Types;
13+
using HotChocolate.Execution.Configuration;
14+
15+
namespace Microsoft.Extensions.DependencyInjection
16+
{
17+
public static partial class Custom_ModuleTypesRequestExecutorBuilderExtensions
18+
{
19+
public static IRequestExecutorBuilder AddCustom_ModuleTypes(this IRequestExecutorBuilder builder)
20+
{
21+
builder.AddType<global::TestNamespace.ATestBType>();
22+
return builder;
23+
}
24+
}
25+
}
26+
27+
```

0 commit comments

Comments
 (0)