From 84bfa80a76aff8140a3a1a9bc5776af639ca36b3 Mon Sep 17 00:00:00 2001 From: Daniel Rebouta Date: Tue, 10 Dec 2024 23:57:10 +0100 Subject: [PATCH 1/3] Add C# identifier sanitization --- .../src/Types.Analyzers/Helpers/GeneratorUtils.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/HotChocolate/Core/src/Types.Analyzers/Helpers/GeneratorUtils.cs b/src/HotChocolate/Core/src/Types.Analyzers/Helpers/GeneratorUtils.cs index a8342c0c3a3..83becb1f6e2 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; @@ -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; + } } From 2f639c646ac28016f512c4db4e76c4b39bca512e Mon Sep 17 00:00:00 2001 From: Daniel Rebouta Date: Wed, 11 Dec 2024 00:15:02 +0100 Subject: [PATCH 2/3] Use SanitizeIdentifier on module names --- .../Types.Analyzers/Generators/DataLoaderModuleGenerator.cs | 2 +- .../Core/src/Types.Analyzers/Helpers/GeneratorUtils.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 83becb1f6e2..bea2a4f5267 100644 --- a/src/HotChocolate/Core/src/Types.Analyzers/Helpers/GeneratorUtils.cs +++ b/src/HotChocolate/Core/src/Types.Analyzers/Helpers/GeneratorUtils.cs @@ -17,7 +17,7 @@ public static ModuleInfo GetModuleInfo( if (syntaxInfo is ModuleInfo module) { defaultModule = false; - return module; + return new ModuleInfo(SanitizeIdentifier(module.ModuleName), module.Options); } } @@ -62,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) { From cc12a3a5c1e037262672963138fcf982e45ae068 Mon Sep 17 00:00:00 2001 From: Daniel Rebouta Date: Wed, 11 Dec 2024 00:46:14 +0100 Subject: [PATCH 3/3] Add GenerateSource_With_Problematic_Assembly_Name test --- .../test/Types.Analyzers.Tests/TestHelper.cs | 4 +-- .../TypeModuleSyntaxGeneratorTests.cs | 16 +++++++++++ ...oblematic_Assembly_Name_MatchesSnapshot.md | 27 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 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/test/Types.Analyzers.Tests/TestHelper.cs b/src/HotChocolate/Core/test/Types.Analyzers.Tests/TestHelper.cs index f6bef502886..6200dc1fc47 100644 --- a/src/HotChocolate/Core/test/Types.Analyzers.Tests/TestHelper.cs +++ b/src/HotChocolate/Core/test/Types.Analyzers.Tests/TestHelper.cs @@ -22,7 +22,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 = [ @@ -47,7 +47,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; + } + } +} + +```