From b245632a2e6b87da8e264f77fbdf479193650e16 Mon Sep 17 00:00:00 2001 From: Alex Banna Date: Tue, 21 Jul 2026 11:14:35 -0500 Subject: [PATCH] feat(stepfunctions): implement States.JsonMerge intrinsic (#1662) * feat(stepfunctions): implement States.JsonMerge intrinsic The DPS provisioning state machine uses States.JsonMerge, which AslExecutor rejected with "Unsupported intrinsic function: States.JsonMerge". Implement the shallow merge (second object's top-level fields win on conflict); reject the deep-merge form (third argument true) and non-object arguments, as AWS does. Also resolve boolean/null literal intrinsic arguments so the third argument evaluates correctly. Covered by AslExecutorJsonMergeTest. * fix(stepfunctions): reject a non-boolean States.JsonMerge third argument The third (deep) argument was read with JsonNode.asBoolean(), which silently coerces null/number/string/object nodes to false, so States.JsonMerge($.a,$.b,$.x) with a non-boolean $.x quietly performed a shallow merge instead of failing. Validate that the resolved third argument is a boolean and raise States.Runtime otherwise. Covered by AslExecutorJsonMergeTest.nonBooleanThirdArgumentRejected. * fix(stepfunctions): validate JsonMerge argument types before the deep-merge flag AWS validates the argument types of States.JsonMerge before evaluating the deep-merge flag, so two non-objects passed with true should report "requires two JSON objects" rather than "shallow merge only". Reorder the object-type check ahead of the deep-flag rejection and add a precedence test. * test(stepfunctions): adapt JsonMerge test to main's AslExecutor constructor main's AslExecutor constructor now takes EcsService and EcsJsonHandler; pass the two new mocks. Also generalize a comment that named a specific downstream system. * docs: regenerate action tables * fix(stepfunctions): thread context into States.JsonMerge intrinsic args The intrinsic pipeline gained a context argument on main (resolveIntrinsicArg / evaluateIntrinsic now take three parameters). Thread the surrounding case's root/context through the three States.JsonMerge argument resolutions so the branch compiles against current main. * fix: align JsonMerge intrinsic failures * fix(athena): harden compatibility query polling --- .../java/com/floci/test/TestFixtures.java | 27 +++++ .../test/java/com/floci/test/AthenaTest.java | 22 ++-- .../java/com/floci/test/DataLakeTest.java | 23 +--- .../services/stepfunctions/AslExecutor.java | 39 ++++++ .../AslExecutorJsonMergeTest.java | 113 ++++++++++++++++++ 5 files changed, 192 insertions(+), 32 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorJsonMergeTest.java diff --git a/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java b/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java index 6c9112e5bc..5540ebd97b 100644 --- a/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java +++ b/compatibility-tests/sdk-test-java/src/main/java/com/floci/test/TestFixtures.java @@ -39,6 +39,9 @@ import software.amazon.awssdk.services.sts.StsClient; import software.amazon.awssdk.services.kafka.KafkaClient; import software.amazon.awssdk.services.mq.MqClient; import software.amazon.awssdk.services.athena.AthenaClient; +import software.amazon.awssdk.services.athena.model.GetQueryExecutionRequest; +import software.amazon.awssdk.services.athena.model.QueryExecutionState; +import software.amazon.awssdk.services.athena.model.QueryExecutionStatus; import software.amazon.awssdk.services.glue.GlueClient; import software.amazon.awssdk.services.firehose.FirehoseClient; import software.amazon.awssdk.services.resourcegroupstaggingapi.ResourceGroupsTaggingApiClient; @@ -401,6 +404,30 @@ public final class TestFixtures { .build(); } + static QueryExecutionStatus awaitAthenaQueryTerminal( + AthenaClient athena, String queryExecutionId, Duration timeout) throws InterruptedException { + long deadline = System.nanoTime() + timeout.toNanos(); + while (true) { + QueryExecutionStatus status = athena.getQueryExecution( + GetQueryExecutionRequest.builder() + .queryExecutionId(queryExecutionId) + .build()) + .queryExecution() + .status(); + QueryExecutionState state = status.state(); + if (state != QueryExecutionState.QUEUED && state != QueryExecutionState.RUNNING) { + return status; + } + + long remainingNanos = deadline - System.nanoTime(); + if (remainingNanos <= 0) { + return status; + } + long sleepMillis = Math.min(500, Math.max(1, Duration.ofNanos(remainingNanos).toMillis())); + Thread.sleep(sleepMillis); + } + } + public static GlueClient glueClient() { return GlueClient.builder() .endpointOverride(ENDPOINT) diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AthenaTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AthenaTest.java index 167cc0ce1f..52880affaf 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AthenaTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/AthenaTest.java @@ -5,6 +5,7 @@ import software.amazon.awssdk.services.athena.AthenaClient; import software.amazon.awssdk.services.athena.model.*; import software.amazon.awssdk.services.glue.GlueClient; +import java.time.Duration; import java.time.Instant; import java.util.UUID; @@ -66,20 +67,12 @@ class AthenaTest { @Test @Order(3) @DisplayName("Get query results returns result set") - void getQueryResults() { - // Poll until succeeded (mock mode completes immediately, real duck may take a moment) - QueryExecutionState state = QueryExecutionState.RUNNING; - int attempts = 0; - while (state == QueryExecutionState.RUNNING && attempts++ < 20) { - GetQueryExecutionResponse exec = athena.getQueryExecution( - GetQueryExecutionRequest.builder().queryExecutionId(queryExecutionId).build()); - state = exec.queryExecution().status().state(); - if (state == QueryExecutionState.RUNNING) { - try { Thread.sleep(500); } catch (InterruptedException ignored) {} - } - } - - assertThat(state).isEqualTo(QueryExecutionState.SUCCEEDED); + void getQueryResults() throws InterruptedException { + QueryExecutionStatus status = TestFixtures.awaitAthenaQueryTerminal( + athena, queryExecutionId, Duration.ofSeconds(60)); + assertThat(status.state()) + .as("Athena query did not succeed: %s", status.stateChangeReason()) + .isEqualTo(QueryExecutionState.SUCCEEDED); GetQueryResultsResponse results = athena.getQueryResults( GetQueryResultsRequest.builder() @@ -177,4 +170,3 @@ class AthenaTest { glue.close(); } } - diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DataLakeTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DataLakeTest.java index baa205512c..b53e754d8e 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DataLakeTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/DataLakeTest.java @@ -11,6 +11,7 @@ import software.amazon.awssdk.services.glue.GlueClient; import software.amazon.awssdk.services.glue.model.*; import java.nio.charset.StandardCharsets; +import java.time.Duration; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -92,23 +93,11 @@ class DataLakeTest { String queryId = startResp.queryExecutionId(); - // Wait for query - int attempts = 0; - QueryExecutionStatus status = null; - while (attempts < 30) { - GetQueryExecutionResponse getResp = athena.getQueryExecution(GetQueryExecutionRequest.builder() - .queryExecutionId(queryId) - .build()); - status = getResp.queryExecution().status(); - if (status.state() == QueryExecutionState.SUCCEEDED) break; - if (status.state() == QueryExecutionState.FAILED) { - Assertions.fail("Query failed: " + status.stateChangeReason()); - } - Thread.sleep(1000); - attempts++; - } - - assertThat(status.state()).isEqualTo(QueryExecutionState.SUCCEEDED); + QueryExecutionStatus status = TestFixtures.awaitAthenaQueryTerminal( + athena, queryId, Duration.ofSeconds(60)); + assertThat(status.state()) + .as("Athena query did not succeed: %s", status.stateChangeReason()) + .isEqualTo(QueryExecutionState.SUCCEEDED); GetQueryResultsResponse results = athena.getQueryResults(GetQueryResultsRequest.builder() .queryExecutionId(queryId) 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 23ae450e2d..d85d47b752 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 @@ -2052,6 +2052,39 @@ public class AslExecutor { case "States.UUID" -> { yield objectMapper.getNodeFactory().textNode(java.util.UUID.randomUUID().toString()); } + case "States.JsonMerge" -> { + List parts = splitIntrinsicArgs(argsStr); + if (parts.size() != 3 || argsStr.stripTrailing().endsWith(",")) { + throw new FailStateException("States.IntrinsicFailure", + "States.JsonMerge requires exactly 3 arguments"); + } + JsonNode a = resolveIntrinsicArg(parts.get(0).trim(), root, context); + JsonNode b = resolveIntrinsicArg(parts.get(1).trim(), root, context); + JsonNode deepArg = resolveIntrinsicArg(parts.get(2).trim(), root, context); + if (!deepArg.isBoolean()) { + // AWS rejects a non-boolean third argument rather than coercing it to false. + throw new FailStateException("States.IntrinsicFailure", + "States.JsonMerge third argument must be a boolean"); + } + boolean deep = deepArg.asBoolean(); + // Validate argument types before rejecting the deep-merge flag, matching AWS error + // ordering: two non-objects passed with true yield "requires two JSON objects", not + // "shallow merge only". + if (!a.isObject() || !b.isObject()) { + throw new FailStateException("States.IntrinsicFailure", + "States.JsonMerge requires two JSON objects"); + } + if (deep) { + // AWS Step Functions only supports the shallow merge (third argument false). + throw new FailStateException("States.IntrinsicFailure", + "States.JsonMerge supports only shallow merge (third argument must be false)"); + } + // Shallow merge: second object's top-level fields override the first's. + var merged = objectMapper.createObjectNode(); + a.fields().forEachRemaining(e -> merged.set(e.getKey(), e.getValue())); + b.fields().forEachRemaining(e -> merged.set(e.getKey(), e.getValue())); + yield merged; + } default -> throw new FailStateException("States.Runtime", "Unsupported intrinsic function: " + fnName); }; @@ -2085,6 +2118,12 @@ public class AslExecutor { if (arg.startsWith("\"") && arg.endsWith("\"")) { return objectMapper.getNodeFactory().textNode(arg.substring(1, arg.length() - 1)); } + if ("true".equals(arg) || "false".equals(arg)) { + return objectMapper.getNodeFactory().booleanNode(Boolean.parseBoolean(arg)); + } + if ("null".equals(arg)) { + return objectMapper.getNodeFactory().nullNode(); + } try { return objectMapper.getNodeFactory().numberNode(Long.parseLong(arg)); } catch (NumberFormatException e1) { diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorJsonMergeTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorJsonMergeTest.java new file mode 100644 index 0000000000..97689185f5 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorJsonMergeTest.java @@ -0,0 +1,113 @@ +package io.github.hectorvent.floci.services.stepfunctions; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.services.cloudformation.CloudFormationQueryHandler; +import io.github.hectorvent.floci.services.dynamodb.DynamoDbJsonHandler; +import io.github.hectorvent.floci.services.dynamodb.DynamoDbService; +import io.github.hectorvent.floci.services.ec2.Ec2Service; +import io.github.hectorvent.floci.services.ecs.EcsJsonHandler; +import io.github.hectorvent.floci.services.ecs.EcsService; +import io.github.hectorvent.floci.services.lambda.LambdaExecutorService; +import io.github.hectorvent.floci.services.lambda.LambdaFunctionStore; +import io.github.hectorvent.floci.services.s3.S3Service; +import io.github.hectorvent.floci.services.sqs.SqsJsonHandler; +import jakarta.enterprise.inject.Instance; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; + +/** + * Regression + functionality coverage for the {@code States.JsonMerge} intrinsic. AWS Step Functions + * supports only the shallow merge form {@code States.JsonMerge($.a, $.b, false)} where the second + * object's top-level fields win on a key conflict; deep-merge ({@code true}) and non-object arguments + * are rejected. A state machine using this intrinsic previously failed with + * "Unsupported intrinsic function: States.JsonMerge". + */ +class AslExecutorJsonMergeTest { + + private final ObjectMapper mapper = new ObjectMapper(); + private AslExecutor executor; + + @BeforeEach + @SuppressWarnings("unchecked") + void setUp() { + executor = new AslExecutor( + mock(LambdaExecutorService.class), + mock(LambdaFunctionStore.class), + mock(DynamoDbService.class), + mock(DynamoDbJsonHandler.class), + mock(SqsJsonHandler.class), + mock(CloudFormationQueryHandler.class), + mock(Ec2Service.class), + mock(S3Service.class), + mock(EcsService.class), + mock(EcsJsonHandler.class), + mapper, + new JsonataEvaluator(mapper), + mock(Instance.class)); + } + + @Test + void shallowMergeSecondObjectWins() throws Exception { + JsonNode root = mapper.readTree("{\"a\":{\"x\":1,\"y\":2},\"b\":{\"y\":9,\"z\":3}}"); + JsonNode out = executor.resolvePath("States.JsonMerge($.a, $.b, false)", root); + assertTrue(out.isObject()); + assertEquals(1, out.path("x").asInt()); + assertEquals(9, out.path("y").asInt(), "second object's value should win on key conflict"); + assertEquals(3, out.path("z").asInt()); + } + + @Test + void deepMergeTrueIsRejected() throws Exception { + JsonNode root = mapper.readTree("{\"a\":{\"x\":1},\"b\":{\"y\":2}}"); + assertThrows(RuntimeException.class, + () -> executor.resolvePath("States.JsonMerge($.a, $.b, true)", root)); + } + + @Test + void nonObjectArgumentsRejected() throws Exception { + JsonNode root = mapper.readTree("{\"a\":[1,2],\"b\":{\"y\":2}}"); + assertThrows(RuntimeException.class, + () -> executor.resolvePath("States.JsonMerge($.a, $.b, false)", root)); + } + + @Test + void nonObjectArgumentsAreRejectedBeforeTheDeepMergeFlag() throws Exception { + // Two non-objects passed with deep=true must report the object-type error, not the + // "shallow merge only" error — AWS validates argument types before the deep-merge flag. + JsonNode root = mapper.readTree("{\"a\":[1,2],\"b\":[3,4]}"); + RuntimeException ex = assertThrows(RuntimeException.class, + () -> executor.resolvePath("States.JsonMerge($.a, $.b, true)", root)); + assertTrue(ex.getMessage().contains("requires two JSON objects"), + "expected the object-type error to take precedence, got: " + ex.getMessage()); + } + + @Test + void wrongArgumentCountUsesIntrinsicFailure() throws Exception { + JsonNode root = mapper.readTree("{\"a\":{\"x\":1},\"b\":{\"y\":2}}"); + AslExecutor.FailStateException exception = assertThrows(AslExecutor.FailStateException.class, + () -> executor.resolvePath("States.JsonMerge($.a, $.b)", root)); + assertEquals("States.IntrinsicFailure", exception.error); + } + + @Test + void trailingCommaUsesIntrinsicFailure() throws Exception { + JsonNode root = mapper.readTree("{\"a\":{\"x\":1},\"b\":{\"y\":2}}"); + AslExecutor.FailStateException exception = assertThrows(AslExecutor.FailStateException.class, + () -> executor.resolvePath("States.JsonMerge($.a, $.b, false,)", root)); + assertEquals("States.IntrinsicFailure", exception.error); + } + + @Test + void nonBooleanThirdArgumentRejected() throws Exception { + // A non-boolean third argument must be rejected, not silently coerced to false (shallow). + JsonNode root = mapper.readTree("{\"a\":{\"x\":1},\"b\":{\"y\":2},\"n\":3}"); + assertThrows(RuntimeException.class, + () -> executor.resolvePath("States.JsonMerge($.a, $.b, $.n)", root)); + } +}