From 8ecf0065807158e2c0cc0149a4e25e8c46101187 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 23 Oct 2024 14:54:51 -0400 Subject: [PATCH] chore: reduces allocs for path parameters sanitation Signed-off-by: Vincent Biret --- .../Extensions/OpenApiUrlTreeNodeExtensions.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Kiota.Builder/Extensions/OpenApiUrlTreeNodeExtensions.cs b/src/Kiota.Builder/Extensions/OpenApiUrlTreeNodeExtensions.cs index fb0b1593a3..a49b82968a 100644 --- a/src/Kiota.Builder/Extensions/OpenApiUrlTreeNodeExtensions.cs +++ b/src/Kiota.Builder/Extensions/OpenApiUrlTreeNodeExtensions.cs @@ -261,15 +261,18 @@ private static IEnumerable>> GetRequestInfo yield return new KeyValuePair>(currentNode.GetUrlTemplate(null, false, false).TrimStart('/'), operations.Select(static x => x.Key.ToString().ToUpperInvariant()).ToHashSet(StringComparer.OrdinalIgnoreCase)); } } - [GeneratedRegex(@"{(?[^}]+)}", RegexOptions.Singleline, 500)] + [GeneratedRegex(@"{[^}]+}", RegexOptions.Singleline, 500)] private static partial Regex pathParamMatcher(); private static string SanitizePathParameterNamesForUrlTemplate(string original, HashSet reservedParameterNames) { if (string.IsNullOrEmpty(original) || !original.Contains('{', StringComparison.OrdinalIgnoreCase)) return original; - var parameters = pathParamMatcher().Matches(original); - foreach (var value in parameters.Select(x => x.Groups["paramname"].Value)) - original = original.Replace(value, (reservedParameterNames.Contains(value) ? "+" : string.Empty) + value.SanitizeParameterNameForUrlTemplate(), StringComparison.Ordinal); - return original; + var updated = original; + foreach (var match in pathParamMatcher().EnumerateMatches(original)) + { + var value = original[(match.Index + 1)..(match.Index + match.Length - 1)];// ignore the { and } + updated = updated.Replace(value, (reservedParameterNames.Contains(value) ? "+" : string.Empty) + value.SanitizeParameterNameForUrlTemplate(), StringComparison.Ordinal); + } + return updated; } public static string SanitizeParameterNameForUrlTemplate(this string original) {