diff --git a/src/Kiota.Builder/Extensions/StringExtensions.cs b/src/Kiota.Builder/Extensions/StringExtensions.cs index 17bfe9cbeb..3e81e1b822 100644 --- a/src/Kiota.Builder/Extensions/StringExtensions.cs +++ b/src/Kiota.Builder/Extensions/StringExtensions.cs @@ -144,18 +144,13 @@ public static string ReplaceDotsWithSlashInNamespaces(this string? namespaced) /// /// Cleanup regex that removes all special characters from ASCII 0-127 /// - private static readonly Regex propertyCleanupRegex = new(@"[""\s!#$%&'()*+,./:;<=>?@\[\]\\^`{}|~-](?\w)?", RegexOptions.Compiled, Constants.DefaultRegexTimeout); + private static readonly Regex propertyCleanupRegex = new(@"[""\s!#$%&'()*,./:;<=>?@\[\]\\^`{}|~-](?\w)?", RegexOptions.Compiled, Constants.DefaultRegexTimeout); private const string CleanupGroupName = "followingLetter"; public static string CleanupSymbolName(this string? original) { if (string.IsNullOrEmpty(original)) return string.Empty; - string result = (String)original.Clone(); - foreach (var norm in NormalizedTrailingSymbols) - { - if (result.StartsWith(norm.Key, StringComparison.OrdinalIgnoreCase)) - result = NormalizeTrailingSymbol(result, norm.Value.Item1, norm.Value.Item2); - } + string result = NormalizeSymbolsBeforeCleanup(original); result = propertyCleanupRegex.Replace(result, static x => x.Groups.Keys.Contains(CleanupGroupName) ? @@ -168,6 +163,7 @@ public static string CleanupSymbolName(this string? original) .Select(static x => SpelledOutNumbers[x]) .Aggregate(static (z, y) => z + y)); + result = NormalizeSymbolsAfterCleanup(result); return result; } private static readonly Regex NumbersSpellingRegex = new(@"^(?\d+)", RegexOptions.Compiled, Constants.DefaultRegexTimeout); @@ -183,16 +179,49 @@ public static string CleanupSymbolName(this string? original) {'8', "Eight"}, {'9', "Nine"}, }; - public static string NormalizeTrailingSymbol(string original, string symbol, string replacement) + /// + /// Normalizing logic for custom symbols handling before cleanup + /// + /// The original string + /// + private static string NormalizeSymbolsBeforeCleanup(string original) { + var result = (String)original.Clone(); + if (result.StartsWith("-", StringComparison.OrdinalIgnoreCase)) + { + result = string.Concat("minus_", result.AsSpan(1)); + } + + if (result.Contains('+', StringComparison.OrdinalIgnoreCase)) + { + result = result.Replace("+", "_plus_", StringComparison.OrdinalIgnoreCase); + } - Regex startRegex = new(@"^(?" + symbol + ")", RegexOptions.Compiled, Constants.DefaultRegexTimeout); - return startRegex.Replace((String)original.Clone(), replacement + "_"); + return result; + } + /// + /// Normalizing logic for custom symbols handling after cleanup + /// + /// The original string + /// + private static string NormalizeSymbolsAfterCleanup(string original) + { + var result = (String)original.Clone(); + if (result.EndsWith("minus_")) + { + result = result[..^1]; + } + if (result.StartsWith("_plus")) + { + result = result[1..]; + } + if (result.EndsWith("plus_")) + { + result = result[..^1]; + } + + return result; } - private static readonly Dictionary NormalizedTrailingSymbols = new() { - {"+", ("\\+", "plus")}, - {"-", ("\\-", "minus")}, - }; /// /// Cleanup the XML string /// diff --git a/tests/Kiota.Builder.Tests/Extensions/StringExtensionsTests.cs b/tests/Kiota.Builder.Tests/Extensions/StringExtensionsTests.cs index db3338d8da..dfc453994b 100644 --- a/tests/Kiota.Builder.Tests/Extensions/StringExtensionsTests.cs +++ b/tests/Kiota.Builder.Tests/Extensions/StringExtensionsTests.cs @@ -85,20 +85,19 @@ public void NormalizeNameSpaceName() Assert.Equal("Toto", "toto".NormalizeNameSpaceName("-")); Assert.Equal("Microsoft_Graph_Message_Content", "microsoft.Graph.Message.Content".NormalizeNameSpaceName("_")); } - [Fact] - public void NormalizeTrailingSymbols() - { - Assert.Equal("plus_1", StringExtensions.NormalizeTrailingSymbol("+1", "\\+", "plus")); - Assert.Equal("minus_1", StringExtensions.NormalizeTrailingSymbol("-1", "\\-", "minus")); - } - [InlineData("\" !#$%&'()*+,./:;<=>?@[]\\^`{}|~-", "")] + [InlineData("\" !#$%&'()*+,./:;<=>?@[]\\^`{}|~-", "plus")] [InlineData("unchanged", "unchanged")] [InlineData("@odata.changed", "OdataChanged")] [InlineData("specialLast@", "specialLast")] [InlineData("kebab-cased", "kebabCased")] [InlineData("123Spelled", "OneTwoThreeSpelled")] [InlineData("+1", "plus_1")] + [InlineData("+1+", "plus_1_plus")] + [InlineData("+1+1", "plus_1_plus_1")] [InlineData("-1", "minus_1")] + [InlineData("-1-", "minus_1")] + [InlineData("-1-1", "minus_11")] + [InlineData("-", "minus")] [Theory] public void CleansUpSymbolNames(string input, string expected) {