Light, fluffy, and always free - The AWS Local Emulator alternative
fix(apigateway): prefer path parameters in VTL lookups (#3560)
XuJian166b13a82f35
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngine.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngine.javaindex dc3bef7f23..94bc21fde7 100644--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngine.java+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngine.java@@ -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);}diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayMockStatusCodeSelectionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayMockStatusCodeSelectionIntegrationTest.javaindex 7874e5d22e..660f8b9c31 100644--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayMockStatusCodeSelectionIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayMockStatusCodeSelectionIntegrationTest.java@@ -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)diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngineTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngineTest.javaindex 3d37728e4d..6c7b92f9c1 100644--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngineTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/VtlTemplateEngineTest.java@@ -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