fix(api-gateway): resolve sibling {proxy+} collision with longest-prefix matching (#811)

* fix(api-gateway): resolve sibling {proxy+} collision with longest-prefix matching

When multiple sibling {proxy+} resources exist (e.g. /auth/{proxy+}
and /products/{proxy+}), the first one registered always matched
every request regardless of the parent path. Now the proxy+ with
the longest parent prefix matching the request path is selected,
matching real AWS behaviour.

Closes #792

* test(api-gateway): add unit tests for proxy+ path matching

Add 7 focused tests for the matchResource method covering root proxy+,
sibling proxy+ routing, fallback behaviour, exact/template priority,
longest parent prefix, and no-match cases.

* fix(api-gateway): reject empty proxy+ segments and use cleaner path extraction

Adopt reviewer's suggestion: use substring before {proxy+} for parent
prefix extraction instead of lastIndexOf('/'), and require at least one
path segment after the prefix for non-root proxy+ resources. Root
/{proxy+} still matches / as a special case.

Adds tests for empty proxy segment edge case (/auth/ should not match
/auth/{proxy+}) and updates longestParentPrefixWins assertion for /api/
(should be null, not match).
This commit is contained in:
been-there-done-that authored and GitHub committed 2026-05-12 20:19:46 -05:00
1 parent e89777b8a2
commit 4b6b99c1e5
2 files changed
+149 -5

No files matched your search

@@ -1180,7 +1180,7 @@ public class ApiGatewayExecuteController {
* Finds the best-matching resource for {@code requestPath}.
* Priority: exact match > template path match (e.g. /items/{id}) > proxy+ wildcard.
*/
private ApiGatewayResource matchResource(List<ApiGatewayResource> resources, String requestPath) {
ApiGatewayResource matchResource(List<ApiGatewayResource> resources, String requestPath) {
// 1. Exact match
for (ApiGatewayResource r : resources) {
if (requestPath.equals(r.getPath())) {
@@ -1195,13 +1195,30 @@ public class ApiGatewayExecuteController {
}
}
}
// 3. Proxy+ wildcard — {proxy+} matches any remaining path
// 3. Proxy+ wildcard — {proxy+} matches longest parent prefix
// Requires at least one path segment after the parent prefix (except root /{proxy+})
ApiGatewayResource best = null;
int bestLen = -1;
for (ApiGatewayResource r : resources) {
if (r.getPathPart() != null && r.getPathPart().contains("{")) {
return r;
if (r.getPath() == null || !r.getPath().contains("{proxy+}")) continue;
String parentPrefix = r.getPath().substring(0, r.getPath().indexOf("{proxy+}"));
// Root /{proxy+} matches everything including /
if ("/".equals(parentPrefix)) {
if (best == null) {
best = r;
bestLen = 0;
}
continue;
}
// Non-root proxy+ requires at least one char after the prefix
if (requestPath.startsWith(parentPrefix)
&& requestPath.length() > parentPrefix.length()
&& parentPrefix.length() > bestLen) {
best = r;
bestLen = parentPrefix.length();
}
}
return null;
return best;
}
/**