feat(stepfunctions): support JSONata workflow variables (Assign) (#1824)

* feat(stepfunctions): support JSONata workflow variables (Assign)

Add support for execution-scoped workflow variables in JSONata state
machines. States may declare an `Assign` field whose values are
evaluated as JSONata and stored as variables that later states
reference as top-level `$name` bindings.

An execution-scoped `variables` object is threaded through every state
executor and mutated in place, so assignments persist across states.
Parallel branches and Map iterations each receive an isolated copy, so
assignments inside them do not leak back to the parent scope, matching
AWS.

Within a state, `Assign` is evaluated before `Output`, so a state's own
`Output` (and all later states) observe the newly assigned variables,
matching AWS evaluation order.

Assign is currently supported for JSONata states only; JSONPath-mode
Assign is left as a follow-up.

Closes #1507

Co-authored-by: Juri Duval <27741935+Mulgish@users.noreply.github.com>

* fix(stepfunctions): resolve JSONata Assign against the state-entry scope

Variable references in a state, including the state's own Output, must
resolve against the values the variables held on state entry. Assign and
Output are now both evaluated against the pre-assignment scope, and the
new values are committed afterwards, so they first become visible to the
next state.

Verified against a recorded AWS execution (localstack's AWS-validated
BASE_EVALUATION_ORDER_PASS_STATE snapshot): a state that assigns
answer="<omitted>" while reading $answer in its own output still emits
the state-entry value 42.

Workflow variables are also bound alongside $states rather than inside
it, since AWS reserves $states for input/result/errorOutput/context.

Tests:
- assignedVariablesAreNotVisibleUntilTheNextState replaces
  assignedVariableVisibleToSameStateOutput, which asserted the inverse.
  It follows the evaluation-order example from the AWS docs: from $x=3,
  $a=6, the block {"x": "{% $a %}", "nextX": "{% $x %}"} ends with $x=6
  and $nextX=3 while the assigning state's Output still emits 3.
- variablesAssignedInsideMapDoNotLeakToParentScope now uses a distinct
  inner variable name, mirroring AWS's own BASE_SCOPE_MAP fixture; AWS
  rejects an inner scope reusing an outer-scope variable name.

* feat(stepfunctions): apply JSONata Assign in Choice rules, Wait states and Catch blocks

Assign was only committed via applyJsonataOutput, which Choice and Wait never
call, so an Assign on either state was silently dropped and Catch blocks could
not assign at all. AWS supports Assign on Pass, Task, Map, Parallel, Choice and
Wait, plus inside Choice rules and Catch fields.

- Choice: a matched rule applies its own Assign and Output; the state-level ones
  apply only on the Default path.
- Wait: applies Assign and Output.
- Catch: applies Assign and Output with $states.errorOutput bound, into the scope
  the catching state lives in.
- Bind $states after the workflow variables so a variable named "states" cannot
  clobber the reserved binding.

Assign, Choice-rule and Catch blocks all keep the AWS evaluation rule already in
place for states: every expression is evaluated against the values held on state
entry, and the new values are committed only afterwards.

---------

Co-authored-by: Juri Duval <27741935+Mulgish@users.noreply.github.com>
This commit is contained in:
Hector VenturaandJuri Duval authored and GitHub committed 2026-07-11 15:17:23 -05:00
1 parent f93e0290b7
commit 3ec4dc65f2
3 files changed
+467 -54

No files matched your search

@@ -1,5 +1,7 @@
package io.github.hectorvent.floci.services.stepfunctions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
@@ -15,6 +17,8 @@ class StepFunctionsJsonataIntegrationTest {
private static final String SFN_CONTENT_TYPE = "application/x-amz-json-1.0";
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/test-role";
private final ObjectMapper objectMapper = new ObjectMapper();
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
@@ -1193,6 +1197,303 @@ class StepFunctionsJsonataIntegrationTest {
assertTrue(output.contains("Jane Doe"));
}
@Test
void assignedVariablesSurviveBeyondNextStateOutput() throws Exception {
// Variables set via Assign persist across states even after a later state replaces the output.
String definition = """
{
"QueryLanguage": "JSONata",
"StartAt": "AssignVariables",
"States": {
"AssignVariables": {
"Type": "Pass",
"Assign": {
"CheckpointCount": "0",
"ExecutionWaitTimeInSeconds": "3"
},
"Output": {
"transient": 3
},
"Next": "UseAndReplaceOutput"
},
"UseAndReplaceOutput": {
"Type": "Pass",
"Output": {
"fromAssignedVariable": "{% $ExecutionWaitTimeInSeconds %}",
"fromPreviousOutput": "{% $states.input.transient %}"
},
"Next": "UseAssignedAgain"
},
"UseAssignedAgain": {
"Type": "Pass",
"Output": {
"checkpoint": "{% $CheckpointCount %}",
"fromAssignedVariable": "{% $ExecutionWaitTimeInSeconds %}",
"previousTransientStillPresent": "{% $exists($states.input.transient) %}"
},
"End": true
}
}
}
""";
String smArn = createStateMachine("jsonata-assign-vars-test", definition);
String execArn = startExecution(smArn, "{}");
String output = waitForExecution(execArn);
assertTrue(output.contains("\"checkpoint\":\"0\"") || output.contains("\"checkpoint\": \"0\""));
assertTrue(output.contains("\"fromAssignedVariable\":\"3\"") || output.contains("\"fromAssignedVariable\": \"3\""));
assertTrue(output.contains("\"previousTransientStillPresent\":false")
|| output.contains("\"previousTransientStillPresent\": false"));
}
@Test
void assignedVariablesAreNotVisibleUntilTheNextState() throws Exception {
// Every variable reference in a state resolves against the values held on state entry, so a
// state's own Output never sees that state's Assign; the new values land in the next state.
// Assignments within one Assign block are likewise independent of each other. This mirrors
// the evaluation-order example in the AWS docs: starting from $x=3 and $a=6,
// {"x": "{% $a %}", "nextX": "{% $x %}"} ends with $x=6 and $nextX=3.
String definition = """
{
"QueryLanguage": "JSONata",
"StartAt": "SeedVariables",
"States": {
"SeedVariables": {
"Type": "Pass",
"Assign": {
"x": 3,
"a": 6
},
"Next": "ReassignAndEmit"
},
"ReassignAndEmit": {
"Type": "Pass",
"Assign": {
"x": "{% $a %}",
"nextX": "{% $x %}"
},
"Output": {
"xSeenByAssigningState": "{% $x %}"
},
"Next": "ObserveAfterAssign"
},
"ObserveAfterAssign": {
"Type": "Pass",
"Output": {
"xSeenByAssigningState": "{% $states.input.xSeenByAssigningState %}",
"xAfter": "{% $x %}",
"nextXAfter": "{% $nextX %}"
},
"End": true
}
}
}
""";
String smArn = createStateMachine("jsonata-assign-evaluation-order-test", definition);
String execArn = startExecution(smArn, "{}");
JsonNode output = objectMapper.readTree(waitForExecution(execArn));
// The assigning state's own Output still sees the pre-assignment value of $x.
assertEquals(3, output.get("xSeenByAssigningState").asInt());
// The next state sees both new values: $x from $a, and $nextX from the old $x.
assertEquals(6, output.get("xAfter").asInt());
assertEquals(3, output.get("nextXAfter").asInt());
}
@Test
void variablesAssignedInsideMapDoNotLeakToParentScope() throws Exception {
// Map iterations can read outer-scope variables but keep their own workflow-local scope:
// a variable assigned inside an iteration goes out of scope once the Map completes.
// The inner variable deliberately uses a name distinct from the outer one — AWS rejects an
// inner-scope assignment that reuses an outer-scope variable name.
String definition = """
{
"QueryLanguage": "JSONata",
"StartAt": "SetOuter",
"States": {
"SetOuter": {
"Type": "Pass",
"Assign": {
"outerVar": 42
},
"Next": "MapState"
},
"MapState": {
"Type": "Map",
"Items": [1, 2],
"ItemProcessor": {
"ProcessorConfig": {
"Mode": "INLINE"
},
"StartAt": "AssignInner",
"States": {
"AssignInner": {
"Type": "Pass",
"Assign": {
"innerVar": "{% $outerVar %}"
},
"Output": {
"outerSeenFromIteration": "{% $outerVar %}"
},
"End": true
}
}
},
"Next": "CheckScope"
},
"CheckScope": {
"Type": "Pass",
"Output": {
"iterations": "{% $states.input %}",
"outerStillInScope": "{% $outerVar %}",
"innerLeaked": "{% $exists($innerVar) %}"
},
"End": true
}
}
}
""";
String smArn = createStateMachine("jsonata-assign-map-scope-test", definition);
String execArn = startExecution(smArn, "{}");
JsonNode output = objectMapper.readTree(waitForExecution(execArn));
// Each iteration could read the outer variable.
assertEquals(42, output.get("iterations").get(0).get("outerSeenFromIteration").asInt());
assertEquals(42, output.get("iterations").get(1).get("outerSeenFromIteration").asInt());
// The outer variable survives the Map, and the iteration-local one does not escape it.
assertEquals(42, output.get("outerStillInScope").asInt());
assertFalse(output.get("innerLeaked").asBoolean());
}
@Test
void assignInMatchedChoiceRuleApplies() throws Exception {
// A Choice rule carries its own Assign, which applies when that rule matches. The state-level
// Assign belongs to the Default path and must not run when a rule matches.
String definition = """
{
"QueryLanguage": "JSONata",
"StartAt": "ChoiceState",
"States": {
"ChoiceState": {
"Type": "Choice",
"Choices": [
{
"Condition": "{% $states.input.condition %}",
"Next": "Report",
"Assign": {
"assignment": "Condition assignment"
}
}
],
"Default": "Report",
"Assign": {
"assignment": "Default Assignment"
}
},
"Report": {
"Type": "Pass",
"Output": {
"assignment": "{% $assignment %}"
},
"End": true
}
}
}
""";
String smArn = createStateMachine("jsonata-assign-in-choice-test", definition);
JsonNode matched = objectMapper.readTree(
waitForExecution(startExecution(smArn, "{\"condition\": true}")));
assertEquals("Condition assignment", matched.get("assignment").asText());
JsonNode defaulted = objectMapper.readTree(
waitForExecution(startExecution(smArn, "{\"condition\": false}")));
assertEquals("Default Assignment", defaulted.get("assignment").asText());
}
@Test
void assignInWaitStateApplies() throws Exception {
String definition = """
{
"QueryLanguage": "JSONata",
"StartAt": "WaitState",
"States": {
"WaitState": {
"Type": "Wait",
"Seconds": 0,
"Assign": {
"foo": "oof"
},
"Next": "Report"
},
"Report": {
"Type": "Pass",
"Output": {
"foo": "{% $foo %}"
},
"End": true
}
}
}
""";
String smArn = createStateMachine("jsonata-assign-in-wait-test", definition);
JsonNode output = objectMapper.readTree(waitForExecution(startExecution(smArn, "{}")));
assertEquals("oof", output.get("foo").asText());
}
@Test
void assignInCatchAppliesAndSeesErrorOutput() throws Exception {
// A Catch block supports Assign and Output, and $states.errorOutput is bound inside it.
// The Task fails while evaluating its Arguments, which is a catchable States.QueryEvaluationError.
String definition = """
{
"QueryLanguage": "JSONata",
"StartAt": "Boom",
"States": {
"Boom": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Arguments": {
"bad": "{% $number('not-a-number') %}"
},
"Catch": [
{
"ErrorEquals": ["States.QueryEvaluationError"],
"Next": "Report",
"Assign": {
"caughtError": "{% $states.errorOutput.Error %}"
}
}
],
"End": true
},
"Report": {
"Type": "Pass",
"Output": {
"caughtError": "{% $caughtError %}",
"catchOutputError": "{% $states.input.Error %}"
},
"End": true
}
}
}
""";
String smArn = createStateMachine("jsonata-assign-in-catch-test", definition);
JsonNode output = objectMapper.readTree(waitForExecution(startExecution(smArn, "{}")));
// The Catch's Assign ran, and it could read $states.errorOutput.
assertEquals("States.QueryEvaluationError", output.get("caughtError").asText());
// With no Output on the Catch, the error output becomes the state output.
assertEquals("States.QueryEvaluationError", output.get("catchOutputError").asText());
}
@Test
void mixedModeDefaultJsonPathWithPerStateJsonata() throws Exception {
// Default JSONPath (no top-level QueryLanguage) with one state overriding to JSONata