Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(apigateway): register native VTL map reflection (#4405)

vamsiak41d07f6e3bc7
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayVtlCompatibilityTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayVtlCompatibilityTest.java
index a7a582f514..eb7eec6c73 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayVtlCompatibilityTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayVtlCompatibilityTest.java
@@ -18,6 +18,76 @@ import static org.assertj.core.api.Assertions.assertThat;
@DisplayName("API Gateway REST VTL")
class ApiGatewayVtlCompatibilityTest {
+ @Test
+ @DisplayName("Parsed map and collection methods work in native REST response templates")
+ void responseTemplateMapMethodsWorkInNativeImage() throws Exception {
+ Assumptions.assumeFalse(TestFixtures.isRealAws(), "Floci native-image regression test");
+
+ ApiGatewayClient apiGateway = TestFixtures.apiGatewayClient();
+ String apiId = apiGateway.createRestApi(request -> request.name(TestFixtures.uniqueName("apigw-vtl-response"))).id();
+
+ try {
+ String rootId = apiGateway.getResources(request -> request.restApiId(apiId)).items().stream()
+ .filter(resource -> "/".equals(resource.path()))
+ .findFirst()
+ .map(Resource::id)
+ .orElseThrow();
+ String resourceId = apiGateway.createResource(request -> request
+ .restApiId(apiId)
+ .parentId(rootId)
+ .pathPart("response-map")).id();
+
+ apiGateway.putMethod(request -> request
+ .restApiId(apiId)
+ .resourceId(resourceId)
+ .httpMethod("POST")
+ .authorizationType("NONE"));
+ apiGateway.putIntegration(request -> request
+ .restApiId(apiId)
+ .resourceId(resourceId)
+ .httpMethod("POST")
+ .type(IntegrationType.MOCK)
+ .requestTemplates(Map.of("application/json", "{\"statusCode\": 200}")));
+ apiGateway.putMethodResponse(request -> request
+ .restApiId(apiId)
+ .resourceId(resourceId)
+ .httpMethod("POST")
+ .statusCode("200"));
+
+ String responseTemplate = "#set($parsed = $util.parseJson($input.body))"
+ + "#if($parsed.headers)$parsed.headers.isEmpty()|$parsed.headers.size()|"
+ + "#foreach($entry in $parsed.headers.entrySet())$entry.getKey()=$entry.getValue()#end|"
+ + "$parsed.items.isEmpty()|$parsed.items.size()|#foreach($item in $parsed.items)$item#end#end";
+ apiGateway.putIntegrationResponse(request -> request
+ .restApiId(apiId)
+ .resourceId(resourceId)
+ .httpMethod("POST")
+ .statusCode("200")
+ .responseTemplates(Map.of("application/json", responseTemplate)));
+
+ String deploymentId = apiGateway.createDeployment(request -> request.restApiId(apiId)).id();
+ apiGateway.createStage(request -> request
+ .restApiId(apiId)
+ .stageName("test")
+ .deploymentId(deploymentId));
+
+ HttpClient httpClient = TestFixtures.emulatorHttpClient();
+ URI uri = URI.create(TestFixtures.endpoint() + "/execute-api/" + apiId + "/test/response-map");
+ HttpRequest request = HttpRequest.newBuilder(uri)
+ .header("Content-Type", "application/json")
+ .POST(HttpRequest.BodyPublishers.ofString(
+ "{\"headers\":{\"x-test\":\"yes\"},\"items\":[\"a\",\"b\"]}"))
+ .build();
+ HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
+
+ assertThat(response.statusCode()).isEqualTo(200);
+ assertThat(response.body().trim()).isEqualTo("false|1|x-test=yes|false|2|ab");
+ } finally {
+ apiGateway.deleteRestApi(request -> request.restApiId(apiId));
+ apiGateway.close();
+ }
+ }
+
@Test
@DisplayName("Header map get and keySet work in the native image")
void headerMapMethodsWorkInNativeImage() throws Exception {
diff --git a/src/main/resources/META-INF/native-image/io.github.hectorvent/floci/reflect-config.json b/src/main/resources/META-INF/native-image/io.github.hectorvent/floci/reflect-config.json
new file mode 100644
index 0000000000..6da786e21f
--- /dev/null
+++ b/src/main/resources/META-INF/native-image/io.github.hectorvent/floci/reflect-config.json
@@ -0,0 +1,25 @@
+[
+ {
+ "name": "java.util.Map",
+ "methods": [
+ { "name": "isEmpty", "parameterTypes": [] },
+ { "name": "size", "parameterTypes": [] },
+ { "name": "entrySet", "parameterTypes": [] }
+ ]
+ },
+ {
+ "name": "java.util.Map$Entry",
+ "methods": [
+ { "name": "getKey", "parameterTypes": [] },
+ { "name": "getValue", "parameterTypes": [] }
+ ]
+ },
+ {
+ "name": "java.util.Collection",
+ "methods": [
+ { "name": "isEmpty", "parameterTypes": [] },
+ { "name": "size", "parameterTypes": [] },
+ { "name": "iterator", "parameterTypes": [] }
+ ]
+ }
+]
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.java
index 9594002aee..38600ba0b9 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
@@ -64,6 +64,19 @@ class VtlTemplateEngineTest {
assertEquals("\"Bob\"", result);
}
+ @Test
+ void parsedMapAndCollectionMethodsInResponseTemplate() {
+ String template = "#set($parsed = $util.parseJson($input.body))"
+ + "#if($parsed.headers)$parsed.headers.isEmpty()|$parsed.headers.size()|"
+ + "#foreach($entry in $parsed.headers.entrySet())$entry.getKey()=$entry.getValue()#end|"
+ + "$parsed.items.isEmpty()|$parsed.items.size()|#foreach($item in $parsed.items)$item#end#end";
+
+ String result = engine.evaluate(template, ctx("{\"headers\":{\"x-test\":\"yes\"},\"items\":[\"a\",\"b\"]}"))
+ .body().trim();
+
+ assertEquals("false|1|x-test=yes|false|2|ab", result);
+ }
+
@Test
void inputPath() {
String result = engine.evaluate("$input.path('$.name')", ctx("{\"name\":\"Carol\"}")).body();