fix(apigateway): prefer path parameters in VTL lookups (#3560)

Co-authored-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com>
This commit is contained in:
XuJianandxujiantop-crypto authored and GitHub committed 2026-09-14 10:53:33 -04:00
1 parent 1987bf0c96
commit 166b13a82f
3 files changed
+54 -7

No files matched your search

@@ -239,14 +239,14 @@ public class VtlTemplateEngine {
}
}
/** Searches all parameter types for the given name (querystring, path, header). */
/** Searches all parameter types for the given name (path, querystring, header). */
public String params(String paramName) {
if (ctx.queryParams() != null && ctx.queryParams().containsKey(paramName)) {
return ctx.queryParams().get(paramName);
}
if (ctx.pathParams() != null && ctx.pathParams().containsKey(paramName)) {
return ctx.pathParams().get(paramName);
}
if (ctx.queryParams() != null && ctx.queryParams().containsKey(paramName)) {
return ctx.queryParams().get(paramName);
}
if (ctx.headers() != null && ctx.headers().containsKey(paramName)) {
return ctx.headers().get(paramName);
}
@@ -6,6 +6,8 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
@@ -226,6 +228,30 @@ class ApiGatewayMockStatusCodeSelectionIntegrationTest {
.body("message", equalTo("Internal server error"));
}
@Test
void pathParameterWinsOverSameNamedQueryAndHeaderInRequestTemplate() {
resourceId = given().contentType(ContentType.JSON)
.body("{\"pathPart\":\"{id}\"}")
.post("/restapis/" + apiId + "/resources/" + resourceId)
.then().statusCode(201).extract().path("id");
putMethod("GET");
putMockIntegration("GET", "#if($input.params('id') == 'path-id')"
+ "{\"statusCode\":201}#else{\"statusCode\":500}#end");
for (String status : List.of("201", "500")) {
putIntegrationResponse("GET", status, status, "");
putMethodResponse("GET", status);
}
deploy();
for (String endpoint : List.of(
"/execute-api/" + apiId + "/" + STAGE,
"/restapis/" + apiId + "/" + STAGE + "/_user_request_")) {
given().queryParam("id", "query-id").header("id", "header-id")
.get(endpoint + "/items/path-id")
.then().statusCode(201);
}
}
private void putMethod(String httpMethod) {
given()
.contentType(ContentType.JSON)
@@ -384,15 +384,36 @@ class VtlTemplateEngineTest {
}
@Test
void inputParams_shorthand_querystringPrecedence() {
// querystring should take precedence over path and header
void inputParams_shorthand_pathPrecedence() {
// AWS searches path, query string, and headers in that order.
VtlTemplateEngine.VtlContext overlapCtx = new VtlTemplateEngine.VtlContext(
"{}", Map.of("shared", "header-val"),
Map.of("shared", "query-val"),
Map.of("shared", "path-val"),
"prod", "GET", "/", "req-1", "000000000000", Map.of(), Map.of());
String result = engine.evaluate("$input.params('shared')", overlapCtx).body();
assertEquals("query-val", result);
assertEquals("path-val", result);
}
@Test
void inputParams_shorthand_preservesAnEmptyPathValue() {
VtlTemplateEngine.VtlContext context = new VtlTemplateEngine.VtlContext(
"{}", Map.of("id", "header-id"), Map.of("id", "query-id"), Map.of("id", ""),
"prod", "GET", "/items/{id}", "req-123", "000000000000", Map.of());
assertEquals("", engine.evaluate("$input.params('id')", context).body());
}
@Test
void inputParams_shorthand_fallsBackToQueryThenHeaderThenEmpty() {
VtlTemplateEngine.VtlContext context = new VtlTemplateEngine.VtlContext(
"{}", Map.of("id", "header-id", "empty", "header-empty", "headerOnly", "header-value"),
Map.of("id", "query-id", "empty", ""), null,
"prod", "GET", "/items", "req-123", "000000000000", Map.of());
assertEquals("query-id||header-value|", engine.evaluate(
"$input.params('id')|$input.params('empty')|$input.params('headerOnly')|$input.params('missing')",
context).body());
}
@Test