Skip to content

Commit

Permalink
chore: reduces allocs for path parameters sanitation
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Oct 23, 2024
1 parent a774b49 commit 8ecf006
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Kiota.Builder/Extensions/OpenApiUrlTreeNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,18 @@ private static IEnumerable<KeyValuePair<string, HashSet<string>>> GetRequestInfo
yield return new KeyValuePair<string, HashSet<string>>(currentNode.GetUrlTemplate(null, false, false).TrimStart('/'), operations.Select(static x => x.Key.ToString().ToUpperInvariant()).ToHashSet(StringComparer.OrdinalIgnoreCase));
}
}
[GeneratedRegex(@"{(?<paramname>[^}]+)}", RegexOptions.Singleline, 500)]
[GeneratedRegex(@"{[^}]+}", RegexOptions.Singleline, 500)]
private static partial Regex pathParamMatcher();
private static string SanitizePathParameterNamesForUrlTemplate(string original, HashSet<string> 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)
{
Expand Down

0 comments on commit 8ecf006

Please sign in to comment.