From 5bf8075d52840b20ae4f0fa2d4efe4c46c2cc2fa Mon Sep 17 00:00:00 2001 From: XuJian Date: Fri, 11 Sep 2026 22:21:19 +0800 Subject: [PATCH] fix(sfn): support advanced JSONPath expressions (#3398) Co-authored-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com> --- pom.xml | 6 ++ .../services/stepfunctions/AslExecutor.java | 68 ++++++++++++++++++- .../AslExecutorPathIntrinsicsTest.java | 41 ++++++++++- .../AslExecutorStatePathTest.java | 10 +++ 4 files changed, 123 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index e24db825d7..9698c651ae 100644 --- a/pom.xml +++ b/pom.xml @@ -151,6 +151,12 @@ com.fasterxml.jackson.dataformat jackson-dataformat-yaml + + + com.jayway.jsonpath + json-path + 2.9.0 + diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java index e89c5ce4b2..b3af9f5996 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java @@ -46,6 +46,12 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.MissingNode; import com.fasterxml.jackson.databind.node.NullNode; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.InvalidPathException; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.PathNotFoundException; +import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider; +import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider; import io.vertx.core.http.HttpMethod; import io.vertx.core.impl.NoStackTraceTimeoutException; @@ -213,6 +219,7 @@ public class AslExecutor { private final SchedulerService schedulerService; private final SchedulerController schedulerController; private final ObjectMapper objectMapper; + private final Configuration jsonPathConfiguration; private final JsonataEvaluator jsonataEvaluator; private final Instance sfnService; private final WebClient webClient; @@ -253,6 +260,12 @@ public class AslExecutor { this.schedulerService = schedulerService; this.schedulerController = schedulerController; this.objectMapper = objectMapper; + this.jsonPathConfiguration = objectMapper == null + ? null + : Configuration.builder() + .jsonProvider(new JacksonJsonNodeJsonProvider(objectMapper)) + .mappingProvider(new JacksonMappingProvider(objectMapper)) + .build(); this.jsonataEvaluator = jsonataEvaluator; this.sfnService = sfnService; this.config = config; @@ -3139,6 +3152,9 @@ public class AslExecutor { if (!path.startsWith("$.") && !path.startsWith("$[")) { return MissingNode.getInstance(); } + if (isAdvancedJsonPath(path)) { + return resolveAdvancedJsonPath(path, root); + } return walkPath(splitPathSegments(path), 0, root); } @@ -3172,6 +3188,13 @@ public class AslExecutor { if (!path.startsWith("$.") && !path.startsWith("$[")) { return NullNode.getInstance(); } + if (isAdvancedJsonPath(path)) { + JsonNode value = resolveAdvancedJsonPath(path, searchRoot); + if (!value.isMissingNode()) { + return value; + } + throw unresolvedPayloadTemplateReference(path, fieldKey, reportedInput); + } String[] parts = splitPathSegments(path); if (containsWildcard(parts)) { JsonNode value = walkPath(parts, 0, searchRoot); @@ -3184,7 +3207,50 @@ public class AslExecutor { if (lookup.outOfRangeArrayIndex) { return NullNode.getInstance(); } - throw new FailStateException("States.Runtime", + throw unresolvedPayloadTemplateReference(path, fieldKey, reportedInput); + } + + private static boolean isAdvancedJsonPath(String path) { + char quote = 0; + boolean escaped = false; + for (int i = 0; i < path.length(); i++) { + char current = path.charAt(i); + if (quote != 0) { + if (escaped) { + escaped = false; + } else if (current == '\\') { + escaped = true; + } else if (current == quote) { + quote = 0; + } + continue; + } + if (current == '\'' || current == '"') { + quote = current; + } else if (current == '.' && i + 1 < path.length() && path.charAt(i + 1) == '.') { + return true; + } else if (current == '[' && i + 2 < path.length() + && path.charAt(i + 1) == '?' && path.charAt(i + 2) == '(') { + return true; + } + } + return false; + } + + private JsonNode resolveAdvancedJsonPath(String path, JsonNode root) { + try { + Object result = JsonPath.using(jsonPathConfiguration).parse(root).read(path); + return result instanceof JsonNode jsonNode ? jsonNode : objectMapper.valueToTree(result); + } catch (PathNotFoundException e) { + return MissingNode.getInstance(); + } catch (InvalidPathException e) { + throw new FailStateException("States.Runtime", "Invalid JSONPath '" + path + "': " + e.getMessage()); + } + } + + private static FailStateException unresolvedPayloadTemplateReference(String path, String fieldKey, + JsonNode reportedInput) { + return new FailStateException("States.Runtime", "The JSONPath '" + path + "' specified for the field '" + fieldKey + "' could not be found in the input '" + reportedInput + "'"); } diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorPathIntrinsicsTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorPathIntrinsicsTest.java index aff06ee86f..90ef4f7248 100644 --- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorPathIntrinsicsTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorPathIntrinsicsTest.java @@ -101,10 +101,49 @@ class AslExecutorPathIntrinsicsTest { @Test void bracketQuotedMembersSupportNamesOutsideDotShorthand() throws Exception { - JsonNode root = mapper.readTree("{\"config\":{\"max-limit\":2},\"a.b\":3}"); + JsonNode root = mapper.readTree( + "{\"config\":{\"max-limit\":2},\"a.b\":3,\"a..b\":4,\"[?(\":5}"); assertEquals(2, executor.resolvePath("$['config']['max-limit']", root).asInt()); assertEquals(3, executor.resolvePath("$['a.b']", root).asInt()); + assertEquals(4, executor.resolvePath("$['a..b']", root).asInt()); + assertEquals(5, executor.resolvePath("$['[?(']", root).asInt()); + } + + @Test + void filterExpressionsSupportExistenceAndComparisonPredicates() throws Exception { + JsonNode root = mapper.readTree(""" + {"list":[{"keep":true,"id":1},{"keep":false,"id":2},{"id":3}]} + """); + + assertEquals(mapper.readTree("[{\"keep\":true,\"id\":1},{\"keep\":false,\"id\":2}]"), + executor.resolvePath("$.list[?(@.keep)]", root)); + assertEquals(mapper.readTree("[{\"keep\":true,\"id\":1}]"), + executor.resolvePath("$.list[?(@.keep == true)]", root)); + } + + @Test + void recursiveDescentPreservesAndFlattensNestedArrayMatches() throws Exception { + JsonNode root = mapper.readTree("{\"d\":{\"x\":{\"a\":[1,2]}}}"); + + assertEquals(mapper.readTree("[[1,2]]"), executor.resolvePath("$.d..a", root)); + assertEquals(mapper.readTree("[1,2]"), executor.resolvePath("$.d..a[*]", root)); + } + + @Test + void advancedPathsWorkInPayloadTemplatesAndIntrinsics() throws Exception { + JsonNode root = mapper.readTree("{\"list\":[1,null,2]}"); + JsonNode parameters = mapper.readTree(""" + { + "filtered.$": "$.list[?(@ != null)]", + "count.$": "States.ArrayLength($.list[?(@ != null)])" + } + """); + + JsonNode resolved = executor.resolveParameters(parameters, root, mapper.createObjectNode()); + + assertEquals(mapper.readTree("[1,2]"), resolved.path("filtered")); + assertEquals(2, resolved.path("count").asInt()); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorStatePathTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorStatePathTest.java index 79261af9ff..c0b7852ab2 100644 --- a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorStatePathTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorStatePathTest.java @@ -122,6 +122,16 @@ class AslExecutorStatePathTest { "{\"kept\":true}"); } + @Test + void passOutputPathSupportsFilterExpressions() throws Exception { + assertOutput(""" + {"StartAt":"Pass","States":{ + "Pass":{"Type":"Pass","OutputPath":"$.Payload[?(@.title)]","End":true}}} + """, + "{\"Payload\":[{\"title\":\"first\"},{\"title\":false},{\"other\":1}]}", + "[{\"title\":\"first\"},{\"title\":false}]"); + } + @Test void succeedAppliesInputPathBeforeOutputPath() throws Exception { assertOutput("""