Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(appsync): supply reflection config so VTL runs on native image (#4729)

TATSUNO “Taz” Yasuhiro7654a7cbec59
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AppSyncTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AppSyncTest.java
index 5e1b57152e..ca95afe51e 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AppSyncTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AppSyncTest.java
@@ -1,5 +1,6 @@
package com.floci.test;
+import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.*;
import software.amazon.awssdk.core.SdkBytes;
@@ -345,6 +346,79 @@ class AppSyncTest {
assertThat(resp.resolver().responseMappingTemplate()).contains("updated");
}
+ @Test
+ @Order(45)
+ void resolverRunsItsVtlTemplate() throws Exception {
+ // One query reaches everything a template calls through reflection: the #return
+ // directive, $ctx and the JDK maps, lists and strings behind it, and each $util helper.
+ updateHelloResolver("""
+ $util.qr($ctx.stash.put("fromRequest", "stashed"))
+ {"version": "2018-05-29", "payload": {}}
+ """, """
+ #set($m = {})
+ $util.qr($m.put("stash", $ctx.stash.fromRequest))
+ $util.qr($m.put("sourceIsNull", $util.isNull($ctx.source)))
+ #set($l = [])
+ $util.qr($l.add("a"))
+ $util.qr($m.put("listSize", $l.size()))
+ #set($s = "abc")
+ $util.qr($m.put("startsWith", $s.startsWith("ab")))
+ $util.qr($m.put("upper", $util.str.toUpper("abc")))
+ $util.qr($m.put("epochIsPositive", $util.time.nowEpochSeconds() > 0))
+ $util.qr($m.put("round", $util.math.roundNum(1.6)))
+ $util.qr($m.put("ddb", $util.dynamodb.toDynamoDB("x")))
+ $util.qr($m.put("filter", $util.transform.toDynamoDBFilterExpression({"a": {"eq": 1}})))
+ $util.qr($m.put("list", $util.list.copyAndRemoveAll([1, 2], [2])))
+ $util.qr($m.put("map", $util.map.copyAndRetainAllKeys({"a": 1, "b": 2}, ["a"])))
+ #return($util.toJson($m))
+ """);
+
+ JsonNode body = queryHello();
+ assertThat(body.path("errors").isMissingNode()).as(body.toString()).isTrue();
+ JsonNode hello = mapper.readTree(body.path("data").path("hello").asText());
+ assertThat(hello.path("stash").asText()).isEqualTo("stashed");
+ assertThat(hello.path("sourceIsNull").asBoolean()).isTrue();
+ assertThat(hello.path("listSize").asInt()).isEqualTo(1);
+ assertThat(hello.path("startsWith").asBoolean()).isTrue();
+ assertThat(hello.path("upper").asText()).isEqualTo("ABC");
+ assertThat(hello.path("epochIsPositive").asBoolean()).isTrue();
+ assertThat(hello.path("round").asInt()).isEqualTo(2);
+ assertThat(hello.path("ddb").path("S").asText()).isEqualTo("x");
+ assertThat(hello.path("filter").isObject()).as(hello.toString()).isTrue();
+ assertThat(hello.path("list").toString()).isEqualTo("[1]");
+ assertThat(hello.path("map").toString()).isEqualTo("{\"a\":1}");
+
+ updateHelloResolver("{\"version\": \"2018-05-29\", \"payload\": {}}",
+ "$util.error(\"boom\", \"CustomError\")");
+
+ JsonNode failed = queryHello();
+ assertThat(failed.path("errors").path(0).path("message").asText()).isEqualTo("boom");
+ assertThat(failed.path("errors").path(0).path("errorType").asText()).isEqualTo("CustomError");
+ }
+
+ private void updateHelloResolver(String requestTemplate, String responseTemplate) {
+ client.updateResolver(UpdateResolverRequest.builder()
+ .apiId(apiId)
+ .typeName("Query")
+ .fieldName("hello")
+ .dataSourceName("none-ds")
+ .requestMappingTemplate(requestTemplate)
+ .responseMappingTemplate(responseTemplate)
+ .build());
+ }
+
+ private JsonNode queryHello() throws Exception {
+ HttpRequest req = HttpRequest.newBuilder()
+ .uri(URI.create(TestFixtures.endpoint() + "/v1/apis/" + apiId + "/graphql"))
+ .header("Content-Type", "application/json")
+ .header("x-api-key", apiKeyValue)
+ .POST(HttpRequest.BodyPublishers.ofString("{\"query\":\"{ hello }\"}"))
+ .build();
+ HttpResponse<String> resp = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
+ assertThat(resp.statusCode()).isEqualTo(200);
+ return mapper.readTree(resp.body());
+ }
+
@Test
@Order(44)
void deleteResolver() {
diff --git a/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/AppSyncVtlEngine.java b/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/AppSyncVtlEngine.java
index 6d13c2ccec..2b22fce3c8 100644
--- a/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/AppSyncVtlEngine.java
+++ b/src/main/java/io/github/hectorvent/floci/services/appsync/graphql/AppSyncVtlEngine.java
@@ -5,7 +5,15 @@ import io.github.hectorvent.floci.services.apigateway.BoundedWriter;
import io.github.hectorvent.floci.services.apigateway.VtlExecutionGuard;
import io.github.hectorvent.floci.services.apigateway.VtlSandbox;
import io.github.hectorvent.floci.services.appsync.graphql.util.AppSyncUtil;
+import io.github.hectorvent.floci.services.appsync.graphql.util.DynamoDbUtil;
+import io.github.hectorvent.floci.services.appsync.graphql.util.ListUtil;
+import io.github.hectorvent.floci.services.appsync.graphql.util.MapUtil;
+import io.github.hectorvent.floci.services.appsync.graphql.util.MathUtil;
+import io.github.hectorvent.floci.services.appsync.graphql.util.StrUtil;
+import io.github.hectorvent.floci.services.appsync.graphql.util.TimeUtil;
+import io.github.hectorvent.floci.services.appsync.graphql.util.TransformUtil;
import io.github.hectorvent.floci.services.appsync.graphql.util.VtlErrorSignal;
+import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.apache.velocity.VelocityContext;
@@ -14,9 +22,31 @@ import org.apache.velocity.runtime.RuntimeConstants;
import java.io.StringWriter;
import java.time.Duration;
+import java.util.Collection;
import java.util.List;
import java.util.Map;
+/**
+ * Velocity reaches the directive by class name and every method a template calls through
+ * reflection, so the native image needs them registered. That includes the JDK maps, lists and
+ * strings that $ctx, literals and $util results are made of.
+ */
+@RegisterForReflection(targets = {
+ ReturnDirective.class,
+ AppSyncUtil.class,
+ StrUtil.class,
+ TimeUtil.class,
+ MathUtil.class,
+ DynamoDbUtil.class,
+ TransformUtil.class,
+ ListUtil.class,
+ MapUtil.class,
+ Map.class,
+ Map.Entry.class,
+ List.class,
+ Collection.class,
+ String.class,
+})
@ApplicationScoped
public class AppSyncVtlEngine {