From 40c64e8798da7789475167d9d46787f8309d3bf9 Mon Sep 17 00:00:00 2001 From: Daniel Rebouta <34064225+CronKz@users.noreply.github.com> Date: Thu, 12 Dec 2024 13:37:01 +0100 Subject: [PATCH] Fixed source-generated class names (#7819) --- .../Generators/DataLoaderModuleGenerator.cs | 2 +- .../Types.Analyzers/Helpers/GeneratorUtils.cs | 19 +++++++++++-- .../test/Types.Analyzers.Tests/TestHelper.cs | 4 +-- .../TypeModuleSyntaxGeneratorTests.cs | 16 +++++++++++ ...oblematic_Assembly_Name_MatchesSnapshot.md | 27 +++++++++++++++++++ 5 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/TypeModuleSyntaxGeneratorTests.GenerateSource_With_Problematic_Assembly_Name_MatchesSnapshot.md diff --git a/src/HotChocolate/Core/src/Types.Analyzers/Generators/DataLoaderModuleGenerator.cs b/src/HotChocolate/Core/src/Types.Analyzers/Generators/DataLoaderModuleGenerator.cs index 96903b72814..a98e6a831b4 100644 --- a/src/HotChocolate/Core/src/Types.Analyzers/Generators/DataLoaderModuleGenerator.cs +++ b/src/HotChocolate/Core/src/Types.Analyzers/Generators/DataLoaderModuleGenerator.cs @@ -81,7 +81,7 @@ public void Generate( { if (syntaxInfo is DataLoaderModuleInfo module) { - return module; + return new DataLoaderModuleInfo(GeneratorUtils.SanitizeIdentifier(module.ModuleName)); } } diff --git a/src/HotChocolate/Core/src/Types.Analyzers/Helpers/GeneratorUtils.cs b/src/HotChocolate/Core/src/Types.Analyzers/Helpers/GeneratorUtils.cs index a8342c0c3a3..bea2a4f5267 100644 --- a/src/HotChocolate/Core/src/Types.Analyzers/Helpers/GeneratorUtils.cs +++ b/src/HotChocolate/Core/src/Types.Analyzers/Helpers/GeneratorUtils.cs @@ -1,4 +1,5 @@ using System.Collections.Immutable; +using System.Text.RegularExpressions; using HotChocolate.Types.Analyzers.Models; using Microsoft.CodeAnalysis; @@ -16,7 +17,7 @@ public static ModuleInfo GetModuleInfo( if (syntaxInfo is ModuleInfo module) { defaultModule = false; - return module; + return new ModuleInfo(SanitizeIdentifier(module.ModuleName), module.Options); } } @@ -61,7 +62,7 @@ public static DataLoaderDefaultsInfo GetDataLoaderDefaults( public static string CreateModuleName(string? assemblyName) => assemblyName is null ? "AssemblyTypes" - : assemblyName.Split('.').Last() + "Types"; + : SanitizeIdentifier(assemblyName.Split('.').Last()) + "Types"; public static string ConvertDefaultValueToString(object? defaultValue, ITypeSymbol type) { @@ -104,4 +105,18 @@ public static string ConvertDefaultValueToString(object? defaultValue, ITypeSymb return defaultValue.ToString(); } + + public static string SanitizeIdentifier(string input) + { + Regex invalidCharsRegex = new("[^a-zA-Z0-9]", RegexOptions.Compiled); + + var sanitized = invalidCharsRegex.Replace(input, "_"); + + if (!char.IsLetter(sanitized[0])) + { + sanitized = "_" + sanitized.Substring(1); + } + + return sanitized; + } } diff --git a/src/HotChocolate/Core/test/Types.Analyzers.Tests/TestHelper.cs b/src/HotChocolate/Core/test/Types.Analyzers.Tests/TestHelper.cs index b8df05d5b73..ca406c98ad6 100644 --- a/src/HotChocolate/Core/test/Types.Analyzers.Tests/TestHelper.cs +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/TestHelper.cs @@ -24,7 +24,7 @@ public static Snapshot GetGeneratedSourceSnapshot([StringSyntax("csharp")] strin return GetGeneratedSourceSnapshot([sourceText]); } - public static Snapshot GetGeneratedSourceSnapshot(string[] sourceTexts) + public static Snapshot GetGeneratedSourceSnapshot(string[] sourceTexts, string? assemblyName = "Tests") { IEnumerable references = [ @@ -49,7 +49,7 @@ public static Snapshot GetGeneratedSourceSnapshot(string[] sourceTexts) // Create a Roslyn compilation for the syntax tree. var compilation = CSharpCompilation.Create( - assemblyName: "Tests", + assemblyName: assemblyName, syntaxTrees: sourceTexts.Select(s => CSharpSyntaxTree.ParseText(s)), references); diff --git a/src/HotChocolate/Core/test/Types.Analyzers.Tests/TypeModuleSyntaxGeneratorTests.cs b/src/HotChocolate/Core/test/Types.Analyzers.Tests/TypeModuleSyntaxGeneratorTests.cs index 58923ba704b..4155cbbb04c 100644 --- a/src/HotChocolate/Core/test/Types.Analyzers.Tests/TypeModuleSyntaxGeneratorTests.cs +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/TypeModuleSyntaxGeneratorTests.cs @@ -157,4 +157,20 @@ public static async Task> GetObjectByIdAAsync( """ ]).MatchMarkdownAsync(); } + + [Fact] + public async Task GenerateSource_With_Problematic_Assembly_Name_MatchesSnapshot() + { + await TestHelper.GetGeneratedSourceSnapshot( + [ + """ + using HotChocolate.Types; + + namespace TestNamespace; + + internal class ATestBType: ObjectType; + internal record ATestB(int Id); + """ + ], assemblyName:"Custom-Module").MatchMarkdownAsync(); + } } diff --git a/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/TypeModuleSyntaxGeneratorTests.GenerateSource_With_Problematic_Assembly_Name_MatchesSnapshot.md b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/TypeModuleSyntaxGeneratorTests.GenerateSource_With_Problematic_Assembly_Name_MatchesSnapshot.md new file mode 100644 index 00000000000..74bc200fdac --- /dev/null +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/__snapshots__/TypeModuleSyntaxGeneratorTests.GenerateSource_With_Problematic_Assembly_Name_MatchesSnapshot.md @@ -0,0 +1,27 @@ +# GenerateSource_With_Problematic_Assembly_Name_MatchesSnapshot + +```csharp +// + +#nullable enable +#pragma warning disable + +using System; +using System.Runtime.CompilerServices; +using HotChocolate; +using HotChocolate.Types; +using HotChocolate.Execution.Configuration; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static partial class Custom_ModuleTypesRequestExecutorBuilderExtensions + { + public static IRequestExecutorBuilder AddCustom_ModuleTypes(this IRequestExecutorBuilder builder) + { + builder.AddType(); + return builder; + } + } +} + +```