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 2ae3f20378..3070c8bdd6 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 @@ -15,6 +15,7 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.jboss.logging.Logger; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.UUID; @@ -26,6 +27,7 @@ public class PipesTargetInvoker { private static final Logger LOG = Logger.getLogger(PipesTargetInvoker.class); private static final Pattern TEMPLATE_PLACEHOLDER = Pattern.compile("<(\\$[^>]+)>"); + private static final Pattern ARRAY_INDEX = Pattern.compile("^(.+?)\\[(\\d+)]$"); private final LambdaService lambdaService; private final SqsService sqsService; @@ -142,18 +144,46 @@ public class PipesTargetInvoker { return null; } try { - String path = jsonPath.startsWith("$") ? jsonPath.substring(1) : jsonPath; - // Translate array indices [N] to /N for Jackson JSON Pointer - path = path.replaceAll("\\[(\\d+)]", "/$1"); - String pointer = path.replace('.', '/'); - JsonNode node = objectMapper.readTree(json).at(pointer); - if (node.isMissingNode() || node.isNull()) { - return null; + String path = jsonPath.startsWith("$.") ? jsonPath.substring(2) + : jsonPath.startsWith("$") ? jsonPath.substring(1) + : jsonPath; + // Split into segments on dots, but expand array indices into separate segments + // e.g. "Records[0].body" -> ["Records", "0", "body"] + String[] rawSegments = path.split("\\."); + List segments = new ArrayList<>(); + for (String seg : rawSegments) { + Matcher am = ARRAY_INDEX.matcher(seg); + if (am.matches()) { + segments.add(am.group(1)); + segments.add(am.group(2)); + } else if (!seg.isEmpty()) { + segments.add(seg); + } } - if (node.isValueNode()) { - return objectMapper.writeValueAsString(node); + + JsonNode current = objectMapper.readTree(json); + for (String segment : segments) { + // AWS Pipes auto-parses string fields containing valid JSON + if (current.isTextual()) { + try { + current = objectMapper.readTree(current.asText()); + } catch (Exception ignored) { + return null; + } + } + if (current.isArray()) { + current = current.path(Integer.parseInt(segment)); + } else { + current = current.path(segment); + } + if (current.isMissingNode() || current.isNull()) { + return null; + } } - return node.toString(); + if (current.isValueNode()) { + return objectMapper.writeValueAsString(current); + } + return current.toString(); } catch (Exception e) { LOG.warnv("Failed to extract JSONPath {0}: {1}", jsonPath, e.getMessage()); return null; 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 8dd8fa1ccc..4070c7adef 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 @@ -197,4 +197,49 @@ class PipesTargetInvokerTest { String result = invoker.applyInputTemplate("{\"count\": <$.count>}", "{\"count\": 42}"); assertEquals("{\"count\": 42}", result); } + + // ──────────────────────────── nested JSON string resolution ──────────────────────────── + + @Test + void extractJsonPath_nestedJsonString_resolvesField() { + String json = "{\"body\": \"{\\\"message\\\": \\\"hello\\\", \\\"type\\\": \\\"greeting\\\"}\"}"; + assertEquals("\"hello\"", invoker.extractJsonPath("$.body.message", json)); + assertEquals("\"greeting\"", invoker.extractJsonPath("$.body.type", json)); + } + + @Test + void extractJsonPath_nestedJsonString_missingNestedField() { + String json = "{\"body\": \"{\\\"message\\\": \\\"hello\\\"}\"}"; + assertNull(invoker.extractJsonPath("$.body.nonexistent", json)); + } + + @Test + void extractJsonPath_nestedJsonString_nonJsonStringReturnsNull() { + String json = "{\"body\": \"plain text\"}"; + assertNull(invoker.extractJsonPath("$.body.message", json)); + } + + @Test + void extractJsonPath_nestedJsonString_objectValue() { + String json = "{\"body\": \"{\\\"data\\\": {\\\"id\\\": 1}}\"}"; + assertEquals("{\"id\":1}", invoker.extractJsonPath("$.body.data", json)); + } + + @Test + void inputTemplate_nestedJsonString_endToEnd() { + ObjectNode tp = MAPPER.createObjectNode(); + tp.put("InputTemplate", + "{\"message\": <$.body.message>, \"messageType\": <$.body.messageType>}"); + + Pipe pipe = createPipe("arn:aws:sqs:us-east-1:000000000000:target", tp); + String payload = "{\"body\": \"{\\\"message\\\": \\\"user registered\\\", \\\"messageType\\\": \\\"REGISTRATION\\\"}\"}"; + + invoker.invoke(pipe, payload, "us-east-1"); + + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(sqsService).sendMessage(anyString(), captor.capture(), eq(0)); + assertEquals( + "{\"message\": \"user registered\", \"messageType\": \"REGISTRATION\"}", + captor.getValue()); + } }