Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(pipes): render InputTemplate values like AWS (#6352)

Jimmy Liu Jia94bdfa462b32
diff --git a/src/main/java/io/github/hectorvent/floci/services/pipes/PipesTargetInvoker.java b/src/main/java/io/github/hectorvent/floci/services/pipes/PipesTargetInvoker.java
index 4cf18b4ee8..f76cac868b 100644
--- a/src/main/java/io/github/hectorvent/floci/services/pipes/PipesTargetInvoker.java
+++ b/src/main/java/io/github/hectorvent/floci/services/pipes/PipesTargetInvoker.java
@@ -19,6 +19,7 @@ import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.nio.charset.StandardCharsets;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -31,7 +32,8 @@ import java.util.stream.Collectors;
public class PipesTargetInvoker {
private static final Logger LOG = Logger.getLogger(PipesTargetInvoker.class);
- private static final Pattern TEMPLATE_PLACEHOLDER = Pattern.compile("<(\\$[^>]+)>");
+ private static final Pattern TEMPLATE_PLACEHOLDER =
+ Pattern.compile("<(\\$[^>]+|aws\\.pipes\\.event\\.ingestion-time)>");
private static final Pattern ARRAY_INDEX = Pattern.compile("^(.+?)\\[(\\d+)]$");
private final LambdaService lambdaService;
@@ -217,16 +219,50 @@ public class PipesTargetInvoker {
String applyInputTemplate(String template, String payload) {
Matcher m = TEMPLATE_PLACEHOLDER.matcher(template);
+ String ingestionTime = objectMapper.valueToTree(Instant.now().toString()).toString();
StringBuilder sb = new StringBuilder();
while (m.find()) {
String jsonPath = m.group(1);
- String value = extractJsonPath(jsonPath, payload);
- m.appendReplacement(sb, Matcher.quoteReplacement(value != null ? value : ""));
+ boolean quoted = isInsideJsonString(template, m.start());
+ String value = "aws.pipes.event.ingestion-time".equals(jsonPath)
+ ? ingestionTime
+ : extractJsonPath(jsonPath, payload);
+ if (value == null) {
+ value = quoted ? "" : objectMapper.valueToTree("").toString();
+ } else if (quoted && value.length() >= 2 && value.startsWith("\"") && value.endsWith("\"")) {
+ // A JSON string placeholder already has quotes from extractJsonPath. The
+ // surrounding template quotes must remain the only pair of quotes.
+ try {
+ String text = objectMapper.readTree(value).asText();
+ value = objectMapper.writeValueAsString(text);
+ value = value.substring(1, value.length() - 1);
+ } catch (JsonProcessingException ignored) {
+ // Keep the extracted representation when it is not a JSON string.
+ }
+ }
+ m.appendReplacement(sb, Matcher.quoteReplacement(value));
}
m.appendTail(sb);
return sb.toString();
}
+ private static boolean isInsideJsonString(String template, int position) {
+ boolean quoted = false;
+ boolean escaped = false;
+ for (int i = 0; i < position; i++) {
+ char c = template.charAt(i);
+ if (c == '\\' && !escaped) {
+ escaped = true;
+ continue;
+ }
+ if (c == '"' && !escaped) {
+ quoted = !quoted;
+ }
+ escaped = false;
+ }
+ return quoted;
+ }
+
String extractJsonPath(String jsonPath, String json) {
if (jsonPath == null || json == null) {
return null;
@@ -250,11 +286,17 @@ public class PipesTargetInvoker {
}
JsonNode current = objectMapper.readTree(json);
+ String previousSegment = null;
+ boolean sourceFieldDecoded = false;
for (String segment : segments) {
- // AWS Pipes auto-parses string fields containing valid JSON
- if (current.isTextual()) {
+ // AWS Pipes parses an encoded source body when traversing into it.
+ if (!sourceFieldDecoded && isEncodedSourceField(previousSegment) && current.isTextual()) {
try {
- current = objectMapper.readTree(current.asText());
+ JsonNode decoded = objectMapper.readTree(current.asText());
+ if (decoded != null && (decoded.isObject() || decoded.isArray())) {
+ current = decoded;
+ }
+ sourceFieldDecoded = true;
} catch (Exception ignored) {
return null;
}
@@ -267,6 +309,20 @@ public class PipesTargetInvoker {
if (current.isMissingNode() || current.isNull()) {
return null;
}
+ previousSegment = segment;
+ }
+ if (!sourceFieldDecoded && isEncodedSourceField(previousSegment) && current.isTextual()) {
+ // SQS bodies commonly contain a JSON document as a string. Pipes input
+ // transformation resolves that document when the body itself is selected.
+ try {
+ JsonNode decoded = objectMapper.readTree(current.asText());
+ sourceFieldDecoded = true;
+ if (decoded != null && (decoded.isObject() || decoded.isArray())) {
+ return decoded.toString();
+ }
+ } catch (JsonProcessingException ignored) {
+ // Keep ordinary textual fields as JSON strings.
+ }
}
if (current.isValueNode()) {
return objectMapper.writeValueAsString(current);
@@ -278,6 +334,11 @@ public class PipesTargetInvoker {
}
}
+ private static boolean isEncodedSourceField(String segment) {
+ return "body".equals(segment) || "data".equals(segment)
+ || "key".equals(segment) || "value".equals(segment);
+ }
+
private static String extractRegionFromArn(String arn, String defaultRegion) {
return AwsArnUtils.regionOrDefault(arn, defaultRegion);
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/pipes/PipesTargetInvokerTest.java b/src/test/java/io/github/hectorvent/floci/services/pipes/PipesTargetInvokerTest.java
index 102a423bbe..bf6bb33486 100644
--- a/src/test/java/io/github/hectorvent/floci/services/pipes/PipesTargetInvokerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/pipes/PipesTargetInvokerTest.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.pipes;
+import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.config.EmulatorConfig;
@@ -18,6 +19,7 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
+import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -215,6 +217,90 @@ class PipesTargetInvokerTest {
assertEquals("{\"count\": 42}", result);
}
+ @Test
+ void inputTemplate_quotedStringPlaceholderIsNotQuotedTwice() throws Exception {
+ String result = invoker.applyInputTemplate("{\"messageId\": \"<$.messageId>\"}",
+ "{\"messageId\": \"abc-123\"}");
+
+ assertEquals("abc-123", MAPPER.readTree(result).path("messageId").asText());
+ }
+
+ @Test
+ void inputTemplate_missingPathBecomesEmptyJsonString() throws Exception {
+ String result = invoker.applyInputTemplate("{\"taskType\": <$.body.missing>}",
+ "{\"body\": \"{\\\"taskType\\\":\\\"x\\\"}\"}");
+
+ assertEquals("", MAPPER.readTree(result).path("taskType").asText());
+ }
+
+ @Test
+ void inputTemplate_decodesJsonSqsBodyAtItsRoot() throws Exception {
+ String result = invoker.applyInputTemplate("{\"body\": <$.body>}",
+ "{\"body\": \"{\\\"taskType\\\":\\\"x\\\"}\"}");
+
+ JsonNode body = MAPPER.readTree(result).path("body");
+ assertTrue(body.isObject());
+ assertEquals("x", body.path("taskType").asText());
+ }
+
+ @Test
+ void inputTemplate_ingestionTimeIsAnIsoInstant() throws Exception {
+ Instant before = Instant.now().minusSeconds(1);
+ String result = invoker.applyInputTemplate("{\"t\": <aws.pipes.event.ingestion-time>}", "{}");
+ Instant after = Instant.now().plusSeconds(1);
+
+ Instant ingestionTime = Instant.parse(MAPPER.readTree(result).path("t").asText());
+ assertFalse(ingestionTime.isBefore(before));
+ assertFalse(ingestionTime.isAfter(after));
+ }
+
+ @Test
+ void extractJsonPath_messageAttributeJsonLookingStringRemainsString() throws Exception {
+ String result = invoker.extractJsonPath("$.messageAttributes.traceId.stringValue",
+ "{\"messageAttributes\":{\"traceId\":{\"stringValue\":\"{\\\"nested\\\":true}\"}}}");
+
+ JsonNode value = MAPPER.readTree(result);
+ assertTrue(value.isTextual());
+ assertEquals("{\"nested\":true}", value.asText());
+ }
+
+ @Test
+ void extractJsonPath_nestedBodyValueRemainsString() throws Exception {
+ ObjectNode body = MAPPER.createObjectNode();
+ body.put("Message", "{\"role\":\"admin\"}");
+ ObjectNode payload = MAPPER.createObjectNode();
+ payload.put("body", body.toString());
+ String result = invoker.extractJsonPath("$.body.Message",
+ payload.toString());
+
+ JsonNode value = MAPPER.readTree(result);
+ assertTrue(value.isTextual());
+ assertEquals("{\"role\":\"admin\"}", value.asText());
+ }
+
+ @Test
+ void extractJsonPath_doesNotDecodeNestedBodyValueTwice() throws Exception {
+ ObjectNode body = MAPPER.createObjectNode();
+ body.put("key", "{\"id\":1}");
+ ObjectNode payload = MAPPER.createObjectNode();
+ payload.put("body", body.toString());
+ String result = invoker.extractJsonPath("$.body.key", payload.toString());
+
+ JsonNode value = MAPPER.readTree(result);
+ assertTrue(value.isTextual());
+ assertEquals("{\"id\":1}", value.asText());
+ }
+
+ @Test
+ void inputTemplate_reusesIngestionTimeForRepeatedPlaceholders() throws Exception {
+ String result = invoker.applyInputTemplate(
+ "{\"first\": <aws.pipes.event.ingestion-time>, "
+ + "\"second\": <aws.pipes.event.ingestion-time>}", "{}");
+
+ JsonNode document = MAPPER.readTree(result);
+ assertEquals(document.path("first").asText(), document.path("second").asText());
+ }
+
// ──────────────────────────── nested JSON string resolution ────────────────────────────
@Test