fix(apigateway): route greedy resources below a path parameter (#6690)

A REST API resource such as /v1/admin/catalog/{kind}/{name+} never
matched: greedy resources were matched by the literal text prefix
"/v1/admin/catalog/{kind}/", which no request path starts with, and the
template matcher skipped greedy resources. Every parameterised resource
also ranked ahead of every greedy one.

Match every resource segment by segment and rank all matches the way
AWS does: more literal segments, then more segments before the greedy
parameter, then the leftmost literal. extractPathParams now yields the
parameters before a greedy one, so {kind} reaches the proxy event,
$input.params and integration parameter mappings.

Closes #6686
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-27 10:24:07 -04:00
1 parent 108110563e
commit 7ca109b595
5 files changed
+202 -51

No files matched your search

@@ -39,7 +39,7 @@ class ApiGatewayGreedyPathParameterIntegrationTest {
}
@Test @Order(2)
void setupAssetsGreedyResource() {
void setupGreedyResources() {
String rootId = given()
.when().get("/restapis/" + apiId + "/resources")
.then()
@@ -94,6 +94,38 @@ class ApiGatewayGreedyPathParameterIntegrationTest {
+ "/methods/ANY/integration/responses/200")
.then()
.statusCode(201);
// /v1/admin/catalog/{kind} lists, /v1/admin/catalog/{kind}/{name+} serves one item
String catalogId = createResource(createResource(createResource(rootId, "v1"), "admin"), "catalog");
String kindId = createResource(catalogId, "{kind}");
mockEcho(kindId, "{\"matched\":\"catalog-list\",\"kind\":\"$input.params('kind')\"}");
mockEcho(createResource(kindId, "{name+}"), "{\"matched\":\"catalog-item\","
+ "\"kind\":\"$input.params('kind')\",\"name\":\"$input.params('name')\"}");
}
private static String createResource(String parentId, String pathPart) {
return given()
.contentType(ContentType.JSON)
.body("{\"pathPart\":\"" + pathPart + "\"}")
.when().post("/restapis/" + apiId + "/resources/" + parentId)
.then()
.statusCode(201)
.extract().path("id");
}
private static void mockEcho(String resourceId, String responseTemplate) {
String method = "/restapis/" + apiId + "/resources/" + resourceId + "/methods/GET";
given().contentType(ContentType.JSON).body("{\"authorizationType\":\"NONE\"}")
.when().put(method).then().statusCode(201);
given().contentType(ContentType.JSON).body("{\"responseParameters\":{}}")
.when().put(method + "/responses/200").then().statusCode(201);
given().contentType(ContentType.JSON)
.body("{\"type\":\"MOCK\",\"requestTemplates\":{\"application/json\":\"{\\\"statusCode\\\": 200}\"}}")
.when().put(method + "/integration").then().statusCode(201);
given().contentType(ContentType.JSON)
.body("{\"selectionPattern\":\"\",\"responseTemplates\":{\"application/json\":\""
+ responseTemplate.replace("\"", "\\\"") + "\"}}")
.when().put(method + "/integration/responses/200").then().statusCode(201);
}
@Test @Order(3)
@@ -161,6 +193,29 @@ class ApiGatewayGreedyPathParameterIntegrationTest {
.body("message", equalTo("Missing Authentication Token"));
}
@Test @Order(8)
void greedyResourceBelowAPathParameterReceivesBothParameters() {
// Used to fall through to the no-match 403: the greedy matcher compared the request with
// the literal text "/v1/admin/catalog/{kind}/", which no real path starts with.
given()
.when().get("/execute-api/" + apiId + "/test/v1/admin/catalog/scopes/teos/sur-df")
.then()
.statusCode(200)
.body("matched", equalTo("catalog-item"))
.body("kind", equalTo("scopes"))
.body("name", equalTo("teos/sur-df"));
}
@Test @Order(9)
void parentResourceStillServesItsOwnPath() {
given()
.when().get("/execute-api/" + apiId + "/test/v1/admin/catalog/scopes")
.then()
.statusCode(200)
.body("matched", equalTo("catalog-list"))
.body("kind", equalTo("scopes"));
}
@Test @Order(99)
void cleanup() {
given().when().delete("/restapis/" + apiId).then().statusCode(202);
@@ -220,6 +220,77 @@ class ApiGatewayProxyMatchTest {
assertNull(ctrl.matchResource(resources, "/items/1/sub"));
}
// ──────────────────────────── greedy below a path parameter ────────────────────────────
// Measured on AWS: every matching resource, exact, parameterised or greedy, is ranked by its
// literal segments, then by its segments before the greedy parameter, then by the leftmost
// literal. A greedy resource is no longer special-cased behind every parameterised one.
@Test
void greedyResourceBelowAPathParameterMatches() {
ApiGatewayResource list = resource("r1", "root", "{kind}", "/v1/admin/catalog/{kind}");
ApiGatewayResource item = resource("r2", "r1", "{name+}", "/v1/admin/catalog/{kind}/{name+}");
List<ApiGatewayResource> resources = List.of(item, list);
assertSame(item, ctrl.matchResource(resources, "/v1/admin/catalog/scopes/teos/sur-df"));
assertSame(item, ctrl.matchResource(resources, "/v1/admin/catalog/scopes/teos"));
assertSame(list, ctrl.matchResource(resources, "/v1/admin/catalog/scopes"));
assertNull(ctrl.matchResource(List.of(item), "/v1/admin/catalog/scopes/"));
assertNull(ctrl.matchResource(List.of(item), "/v1/admin/other/scopes/teos"));
}
@Test
void moreLiteralSegmentsWinAcrossParameterisedAndGreedyResources() {
ApiGatewayResource greedy = resource("r1", "root", "{proxy+}", "/s/b/{proxy+}");
ApiGatewayResource template = resource("r2", "root", "{y}", "/s/{x}/{y}");
ApiGatewayResource deepTemplate = resource("r3", "root", "{v}", "/s/{x}/{y}/{z}/{v}");
List<ApiGatewayResource> resources = List.of(template, deepTemplate, greedy);
assertSame(greedy, ctrl.matchResource(resources, "/s/b/c"));
assertSame(greedy, ctrl.matchResource(resources, "/s/b/c/d/e"));
assertSame(template, ctrl.matchResource(resources, "/s/a/c"));
}
@Test
void moreLiteralSegmentsBeatADeeperGreedyParent() {
ApiGatewayResource shallow = resource("r1", "root", "{proxy+}", "/q/b/{proxy+}");
ApiGatewayResource deep = resource("r2", "root", "{p+}", "/q/{x}/{y}/{p+}");
ApiGatewayResource early = resource("r3", "root", "{p+}", "/m/b/{y}/{z}/{p+}");
ApiGatewayResource late = resource("r4", "root", "{q+}", "/m/{x}/c/d/{q+}");
List<ApiGatewayResource> resources = List.of(deep, shallow, early, late);
assertSame(shallow, ctrl.matchResource(resources, "/q/b/c/d"));
assertSame(deep, ctrl.matchResource(resources, "/q/a/c/d"));
assertSame(late, ctrl.matchResource(resources, "/m/b/c/d/e"));
}
@Test
void deeperGreedyParentWinsAtEqualLiteralSegments() {
ApiGatewayResource nested = resource("r1", "root", "{y+}", "/a/{x}/{y+}");
ApiGatewayResource literal = resource("r2", "root", "{proxy+}", "/a/b/{proxy+}");
ApiGatewayResource deeper = resource("r3", "root", "{z+}", "/a/{x}/c/{z+}");
List<ApiGatewayResource> resources = List.of(nested, literal, deeper);
assertSame(nested, ctrl.matchResource(resources, "/a/one/two/three"));
assertSame(literal, ctrl.matchResource(resources, "/a/b/c"));
assertEquals(List.of(deeper, literal, nested), ctrl.matchResources(resources, "/a/b/c/d"));
}
@Test
void leftmostLiteralBreaksATie() {
ApiGatewayResource leftLiteral = resource("r1", "root", "{p+}", "/n/b/{y}/{p+}");
ApiGatewayResource rightLiteral = resource("r2", "root", "{q+}", "/n/{x}/c/{q+}");
assertSame(leftLiteral, ctrl.matchResource(List.of(rightLiteral, leftLiteral), "/n/b/c/d"));
assertSame(leftLiteral, ctrl.matchResource(List.of(leftLiteral, rightLiteral), "/n/b/c/d"));
}
@Test
void pathParameterMatchesAnEmptySegment() {
ApiGatewayResource template = resource("r1", "root", "two", "/a/{x}/two");
assertSame(template, ctrl.matchResource(List.of(template), "/a//two"));
}
// ──────────────────────────── trailing-slash preservation (#1557) ────────────────────────────
@Test
@@ -112,4 +112,14 @@ class ProxyEventPathParametersTest {
assertEquals(List.of("rest"), keysOf(pp));
assertEquals("img/logo.png", pp.get("rest").asText());
}
@Test
void greedyResourceBelowAPathParameterReceivesBothParameters() throws Exception {
JsonNode pp = pathParametersFor("/v1/admin/catalog/scopes/teos/sur-df",
"/v1/admin/catalog/{kind}/{name+}");
assertEquals(List.of("kind", "name"), keysOf(pp));
assertEquals("scopes", pp.get("kind").asText());
assertEquals("teos/sur-df", pp.get("name").asText());
}
}