fix(apigatewayv2): fix route matching for path-parameter routes (#368)

routeKeyMatchesPath() used String.replace() on Pattern.quote() output,
which performs literal string matching. Path-parameter placeholders like
{id} and {proxy+} were wrapped in \Q...\E by Pattern.quote(), making
them invisible to the replace() calls. All parameterized routes silently
failed to match.

Rewritten to iterate over placeholders with a regex Matcher, quoting
only the literal segments between them. {param} maps to [^/]+ and
{proxy+} maps to .+ as before.
This commit is contained in:
TiM authored and GitHub committed 2026-04-12 19:57:25 -05:00
1 parent f3c9314114
commit 9e79859cea
1 file changed
+13 -6
@@ -185,12 +185,19 @@ public class ApiGatewayV2Service {
String pattern = routeKey.substring(space + 1);
if (!method.equalsIgnoreCase(httpMethod)) return false;
// Convert path template to regex: {param} → [^/]+, {proxy+} → .+
String regex = "^" + Pattern.quote(pattern)
.replace("\\{proxy+\\}", "\\E.+\\Q")
.replace("\\{", "\\E[^/]+\\Q")
.replaceAll("\\\\Q\\\\E", "") + "$";
return path.matches(regex);
// Build regex from path template: {proxy+} -> .+, {param} -> [^/]+
// Quote literal segments to avoid regex injection from path patterns
StringBuilder regex = new StringBuilder("^");
java.util.regex.Matcher m = java.util.regex.Pattern.compile("\\{([^}]*)}").matcher(pattern);
int last = 0;
while (m.find()) {
regex.append(Pattern.quote(pattern.substring(last, m.start())));
regex.append(m.group(1).endsWith("+") ? ".+" : "[^/]+");
last = m.end();
}
regex.append(Pattern.quote(pattern.substring(last)));
regex.append("$");
return path.matches(regex.toString());
}
// ──────────────────────────── Integration CRUD ────────────────────────────