Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2212 Incorrect calculation of placeholders, only the last placeholder should be allowed to match several segments #2213

Merged

Conversation

ggnaegi
Copy link
Member

@ggnaegi ggnaegi commented Nov 22, 2024

Fixes #2212

Proposed Changes

  • Ensuring that only the last placeholder is allowed to match multiple segments (separated by slashes).
  • Accomplishing this by counting the placeholders and using an index during the replacement process.
  • If the current index is not equal to the placeholder count, the placeholder cannot match forward slashes; if the index equals the placeholder count, it can match slashes.

…ssue and adding unit and acceptance tests verifying the behavior.
@ggnaegi
Copy link
Member Author

ggnaegi commented Nov 22, 2024

@raman-m ready for review

@ggnaegi ggnaegi requested review from RaynaldM and raman-m November 22, 2024 15:56
@raman-m raman-m added hotfix Gitflow: Hotfix issue, PR related to hotfix branch Routing Ocelot feature: Routing Nov'24 November 2024 release labels Nov 22, 2024
@raman-m raman-m added this to the November'24 milestone Nov 22, 2024
@ggnaegi
Copy link
Member Author

ggnaegi commented Nov 22, 2024

@raman-m could you please review this, so we can close it?

Copy link
Member

@raman-m raman-m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add one small commit with style-fixes, while you answering my questions...

{
var groupName = match.Groups[1].Value;
index++;
return index == placeHoldersCount ? $"(?<{groupName}>[^&]*)" : $"(?<{groupName}>[^/|&]*)";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this [^/|&] negative matching group filters out all slashes / after closing brace }, before catch all placeholder, right?
And, the last RX-match, for the last Catch-All, just accepts uncounted number of slashes before query string, right?
Query string processing is quite unclear... What's the final generated pattern?

@@ -501,6 +501,30 @@ public void ShouldNotMatchComplexQueriesCaseSensitive(string downstream, string
.BDDfy();
}

[Theory]
[Trait("Bug", "2212")]
[InlineData("/data-registers/{version}/it/{everything}", "/dati-registri/{version}/{everything}", "/dati-registri/v1.0/operatore/R80QQ5J9600/valida", "/data-registers/v1.0/it/operatore/R80QQ5J9600/valida")]
Copy link
Member

@raman-m raman-m Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test data from #2212, right?

Convert anonimous delegate to local function.
Copy link
Member

@raman-m raman-m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready for Delivery ✅

I am agreeable to merging; however, we will undoubtedly experience a loss in performance, a little bit... Nevertheless, intelligent logic must remain dynamic.

@@ -67,10 +67,12 @@ public Response<List<PlaceholderNameAndValue>> Find(string path, string query, s
private static List<Group> FindGroups(string path, string query, string template)
{
template = EscapeExceptBraces(template);
var regexPattern = $"^{RegexPlaceholders().Replace(template, match => $"(?<{match.Groups[1].Value}>[^&]*)")}";
var regexPattern = GenerateRegexPattern(template);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems this is dynamic Regex, so the pattern is dynamically generated, but the previous version aka RegexPlaceholders() returns static object.
Well... is it possible to convert somehow to static object to improve the performance?

return index == placeHoldersCount ? $"(?<{groupName}>[^&]*)" : $"(?<{groupName}>[^/|&]*)";
}

return $@"^{RegexPlaceholders().Replace(escapedTemplate, MatchEvaluator)}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to move this replacement to the static object? It appears not, due to the varying escapedTemplate that represents the current path template. This is unfortunate 😢 as it seems there are no options available.

@raman-m
Copy link
Member

raman-m commented Nov 22, 2024

I will release a patch after your pressing magic buttons here, and I needn't your approval 😝

@raman-m
Copy link
Member

raman-m commented Nov 22, 2024

It is that one may wish to press the magic buttons on incorrect PRs 😝 however, being offline means you don't wanna merge your own PRs 🤣

Happy Friday, Gui! 🥳

@ggnaegi
Copy link
Member Author

ggnaegi commented Nov 22, 2024

Ready for Delivery ✅

I am agreeable to merging; however, we will undoubtedly experience a loss in performance, a little bit... Nevertheless, intelligent logic must remain dynamic.

Yes, I think a few benchmarks are needed...

@ggnaegi ggnaegi merged commit 7a3771b into ThreeMammals:develop Nov 22, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hotfix Gitflow: Hotfix issue, PR related to hotfix branch Nov'24 November 2024 release Routing Ocelot feature: Routing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect calculation of placeholders
2 participants