feat: add JSONata query language support for Step Functions (#84)

Thanks again, @thegagne!
This is a great addition to Floci. Your continued support really makes a difference. Hope to see you around soon!

---
* feat: add JSONata query language support for Step Functions

Add JSONata as an alternative query language for Step Functions, matching
the AWS feature launched in late 2024. When a state machine definition
sets "QueryLanguage": "JSONata", the JSONPath fields (InputPath,
Parameters, ResultSelector, ResultPath, OutputPath) are replaced by
Arguments and Output, with expressions wrapped in {% %} delimiters.

Implementation:
- Add dashjoin jsonata-java 0.9.9 dependency (zero-dep, 160KB)
- New JsonataEvaluator: handles {% %} expression evaluation, Jackson↔Map
  conversion, $states variable binding, recursive template resolution
- AslExecutor: dual query language branches for all state types (Pass,
  Task, Choice, Wait, Map, Parallel, Succeed, Fail)
- $states.input, $states.result, $states.context (Execution, StateMachine,
  State metadata) all supported
- Definition validation: rejects JSONPath-only fields in JSONata mode,
  rejects JSONata→JSONPath per-state override (only JSONPath→JSONata
  is allowed, matching AWS behavior)

All behavior validated against real AWS Step Functions.

* fix: return error and cause fields in DescribeExecution response

DescribeExecution was not returning the error and cause fields for
failed executions. AWS includes these in the response so the SDK can
surface them via DescribeExecutionResponse.error() and .cause().

Add error/cause fields to the Execution model and include them in the
JSON handler response when present.
This commit is contained in:
thegagne authored and GitHub committed 2026-03-26 13:18:32 -05:00
1 parent 26facf26e5
commit f82b370ab2
9 files changed
+1041 -37

No files matched your search

@@ -34,9 +34,12 @@ public class AslExecutor {
private static final Logger LOG = Logger.getLogger(AslExecutor.class);
private static final int MAX_WAIT_SECONDS = 30;
private static final String QUERY_LANGUAGE_JSONATA = "JSONata";
private final LambdaExecutorService lambdaExecutor;
private final LambdaFunctionStore functionStore;
private final ObjectMapper objectMapper;
private final JsonataEvaluator jsonataEvaluator;
private final ExecutorService executor = Executors.newCachedThreadPool(r -> {
Thread t = new Thread(r, "sfn-executor");
t.setDaemon(true);
@@ -45,10 +48,11 @@ public class AslExecutor {
@Inject
public AslExecutor(LambdaExecutorService lambdaExecutor, LambdaFunctionStore functionStore,
ObjectMapper objectMapper) {
ObjectMapper objectMapper, JsonataEvaluator jsonataEvaluator) {
this.lambdaExecutor = lambdaExecutor;
this.functionStore = functionStore;
this.objectMapper = objectMapper;
this.jsonataEvaluator = jsonataEvaluator;
}
/**
@@ -62,7 +66,9 @@ public class AslExecutor {
JsonNode definition = objectMapper.readTree(sm.getDefinition());
JsonNode states = definition.path("States");
String startAt = definition.path("StartAt").asText();
String topLevelQueryLanguage = definition.path("QueryLanguage").asText("JSONPath");
JsonNode currentInput = parseInput(exec.getInput());
JsonNode execContext = buildContext(exec, sm);
String currentStateName = startAt;
while (currentStateName != null) {
@@ -75,8 +81,13 @@ public class AslExecutor {
addEvent(history, eventId, stateEnteredEventType(type), null,
Map.of("name", currentStateName, "input", currentInput.toString()));
// Update per-state context fields
updateStateContext(execContext, currentStateName);
try {
StateResult stateResult = executeState(currentStateName, type, stateDef, currentInput, history, eventId, sm);
boolean jsonata = isJsonata(stateDef, topLevelQueryLanguage);
StateResult stateResult = executeState(currentStateName, type, stateDef, currentInput,
history, eventId, sm, jsonata, topLevelQueryLanguage, execContext);
addEvent(history, eventId, stateExitedEventType(type), eventId.get() - 1,
Map.of("name", currentStateName, "output", stateResult.output().toString()));
@@ -89,16 +100,23 @@ public class AslExecutor {
} catch (FailStateException e) {
exec.setStatus("FAILED");
exec.setStopDate(System.currentTimeMillis() / 1000L);
String failError = e.error != null ? e.error : "States.Runtime";
String failCause = e.cause != null ? e.cause : "";
exec.setError(failError);
exec.setCause(failCause);
addEvent(history, eventId, "ExecutionFailed", null,
Map.of("error", e.error != null ? e.error : "States.Runtime",
"cause", e.cause != null ? e.cause : ""));
Map.of("error", failError, "cause", failCause));
onUpdate.accept(exec, history);
return;
} catch (Exception e) {
exec.setStatus("FAILED");
exec.setStopDate(System.currentTimeMillis() / 1000L);
String runtimeError = "States.Runtime";
String runtimeCause = e.getMessage() != null ? e.getMessage() : "Unknown error";
exec.setError(runtimeError);
exec.setCause(runtimeCause);
addEvent(history, eventId, "ExecutionFailed", null,
Map.of("error", "States.Runtime", "cause", e.getMessage() != null ? e.getMessage() : "Unknown error"));
Map.of("error", runtimeError, "cause", runtimeCause));
onUpdate.accept(exec, history);
return;
}
@@ -121,22 +139,28 @@ public class AslExecutor {
}
private StateResult executeState(String name, String type, JsonNode stateDef, JsonNode input,
List<HistoryEvent> history, AtomicLong eventId, StateMachine sm) throws Exception {
List<HistoryEvent> history, AtomicLong eventId, StateMachine sm,
boolean jsonata, String topLevelQueryLanguage, JsonNode context) throws Exception {
return switch (type) {
case "Pass" -> executePassState(stateDef, input);
case "Task" -> executeTaskState(name, stateDef, input, history, eventId, sm);
case "Choice" -> executeChoiceState(stateDef, input);
case "Wait" -> executeWaitState(stateDef, input);
case "Succeed" -> new StateResult(applyOutputPath(stateDef, input, input), null);
case "Fail" -> throw new FailStateException(
stateDef.path("Error").asText(null), stateDef.path("Cause").asText(null));
case "Parallel" -> executeParallelState(name, stateDef, input, sm);
case "Map" -> executeMapState(name, stateDef, input, sm);
case "Pass" -> executePassState(stateDef, input, jsonata, context);
case "Task" -> executeTaskState(name, stateDef, input, history, eventId, sm, jsonata, context);
case "Choice" -> executeChoiceState(stateDef, input, jsonata, context);
case "Wait" -> executeWaitState(stateDef, input, jsonata, context);
case "Succeed" -> executeSucceedState(stateDef, input, jsonata, context);
case "Fail" -> executeFail(stateDef, input, jsonata, context);
case "Parallel" -> executeParallelState(name, stateDef, input, sm, jsonata, topLevelQueryLanguage, context);
case "Map" -> executeMapState(name, stateDef, input, sm, jsonata, topLevelQueryLanguage, context);
default -> new StateResult(input, stateDef.path("Next").asText(null));
};
}
private StateResult executePassState(JsonNode stateDef, JsonNode input) throws Exception {
private StateResult executePassState(JsonNode stateDef, JsonNode input, boolean jsonata, JsonNode context) throws Exception {
if (jsonata) {
JsonNode result = stateDef.has("Result") ? stateDef.get("Result") : input;
JsonNode output = applyJsonataOutput(stateDef, input, result, context);
return new StateResult(output, stateDef.path("Next").asText(null));
}
JsonNode effectiveInput = applyInputPath(stateDef, input);
JsonNode result;
@@ -152,7 +176,22 @@ public class AslExecutor {
}
private StateResult executeTaskState(String stateName, JsonNode stateDef, JsonNode input,
List<HistoryEvent> history, AtomicLong eventId, StateMachine sm) throws Exception {
List<HistoryEvent> history, AtomicLong eventId, StateMachine sm,
boolean jsonata, JsonNode context) throws Exception {
if (jsonata) {
JsonNode effectiveInput = input;
if (stateDef.has("Arguments")) {
JsonNode statesVar = buildStatesVar(input, null, context);
effectiveInput = jsonataEvaluator.resolveTemplate(stateDef.get("Arguments"), statesVar);
}
String resource = stateDef.path("Resource").asText();
JsonNode taskResult = invokeResource(resource, effectiveInput, stateDef, sm);
JsonNode output = applyJsonataOutput(stateDef, input, taskResult, context);
return new StateResult(output, stateDef.path("Next").asText(null));
}
JsonNode effectiveInput = applyInputPath(stateDef, input);
if (stateDef.has("Parameters")) {
effectiveInput = resolveParameters(stateDef.get("Parameters"), effectiveInput);
@@ -222,7 +261,26 @@ public class AslExecutor {
return input;
}
private StateResult executeChoiceState(JsonNode stateDef, JsonNode input) throws Exception {
private StateResult executeChoiceState(JsonNode stateDef, JsonNode input, boolean jsonata, JsonNode context) throws Exception {
if (jsonata) {
JsonNode statesVar = buildStatesVar(input, null, context);
JsonNode choices = stateDef.path("Choices");
for (JsonNode choice : choices) {
String condition = choice.path("Condition").asText(null);
if (condition != null) {
JsonNode result = jsonataEvaluator.evaluate(condition, statesVar);
if (result.isBoolean() && result.asBoolean()) {
return new StateResult(input, choice.path("Next").asText());
}
}
}
String defaultState = stateDef.path("Default").asText(null);
if (defaultState != null) {
return new StateResult(input, defaultState);
}
throw new FailStateException("States.NoChoiceMatched", "No choice rule matched and no default state");
}
JsonNode choices = stateDef.path("Choices");
for (JsonNode choice : choices) {
if (evaluateCondition(choice, input)) {
@@ -318,13 +376,26 @@ public class AslExecutor {
return false;
}
private StateResult executeWaitState(JsonNode stateDef, JsonNode input) throws InterruptedException {
private StateResult executeWaitState(JsonNode stateDef, JsonNode input, boolean jsonata, JsonNode context) throws InterruptedException {
int seconds = 0;
if (stateDef.has("Seconds")) {
seconds = Math.min(stateDef.get("Seconds").asInt(), MAX_WAIT_SECONDS);
} else if (stateDef.has("SecondsPath")) {
JsonNode val = resolvePath(stateDef.get("SecondsPath").asText(), input);
seconds = Math.min(val.asInt(), MAX_WAIT_SECONDS);
if (jsonata) {
if (stateDef.has("Seconds")) {
JsonNode secondsNode = stateDef.get("Seconds");
if (secondsNode.isTextual() && JsonataEvaluator.isExpression(secondsNode.asText())) {
JsonNode statesVar = buildStatesVar(input, null, context);
JsonNode result = jsonataEvaluator.evaluate(secondsNode.asText(), statesVar);
seconds = Math.min(result.asInt(), MAX_WAIT_SECONDS);
} else {
seconds = Math.min(secondsNode.asInt(), MAX_WAIT_SECONDS);
}
}
} else {
if (stateDef.has("Seconds")) {
seconds = Math.min(stateDef.get("Seconds").asInt(), MAX_WAIT_SECONDS);
} else if (stateDef.has("SecondsPath")) {
JsonNode val = resolvePath(stateDef.get("SecondsPath").asText(), input);
seconds = Math.min(val.asInt(), MAX_WAIT_SECONDS);
}
}
// Timestamp and TimestampPath: wait until that time or now, whichever is sooner
if (seconds > 0) {
@@ -333,7 +404,31 @@ public class AslExecutor {
return new StateResult(input, stateDef.path("Next").asText(null));
}
private StateResult executeParallelState(String name, JsonNode stateDef, JsonNode input, StateMachine sm) throws Exception {
private StateResult executeSucceedState(JsonNode stateDef, JsonNode input, boolean jsonata, JsonNode context) {
if (jsonata) {
JsonNode output = applyJsonataOutput(stateDef, input, input, context);
return new StateResult(output, null);
}
return new StateResult(applyOutputPath(stateDef, input, input), null);
}
private StateResult executeFail(JsonNode stateDef, JsonNode input, boolean jsonata, JsonNode context) {
String error = stateDef.path("Error").asText(null);
String cause = stateDef.path("Cause").asText(null);
if (jsonata) {
JsonNode statesVar = buildStatesVar(input, null, context);
if (error != null && JsonataEvaluator.isExpression(error)) {
error = jsonataEvaluator.evaluate(error, statesVar).asText();
}
if (cause != null && JsonataEvaluator.isExpression(cause)) {
cause = jsonataEvaluator.evaluate(cause, statesVar).asText();
}
}
throw new FailStateException(error, cause);
}
private StateResult executeParallelState(String name, JsonNode stateDef, JsonNode input, StateMachine sm,
boolean jsonata, String topLevelQueryLanguage, JsonNode context) throws Exception {
JsonNode branches = stateDef.path("Branches");
List<Future<JsonNode>> futures = new ArrayList<>();
@@ -342,7 +437,7 @@ public class AslExecutor {
JsonNode branchStates = branch.path("States");
JsonNode capturedInput = input;
futures.add(executor.submit(() -> executeBranch(startAt, branchStates, capturedInput, sm)));
futures.add(executor.submit(() -> executeBranch(startAt, branchStates, capturedInput, sm, topLevelQueryLanguage, context)));
}
ArrayNode results = objectMapper.createArrayNode();
@@ -350,26 +445,49 @@ public class AslExecutor {
results.add(future.get(60, TimeUnit.SECONDS));
}
if (jsonata) {
JsonNode output = applyJsonataOutput(stateDef, input, results, context);
return new StateResult(output, stateDef.path("Next").asText(null));
}
JsonNode output = mergeResult(stateDef, input, results);
output = applyOutputPath(stateDef, input, output);
return new StateResult(output, stateDef.path("Next").asText(null));
}
private StateResult executeMapState(String name, JsonNode stateDef, JsonNode input, StateMachine sm) throws Exception {
JsonNode itemsPath = stateDef.path("ItemsPath");
JsonNode items = itemsPath.isMissingNode() ? input : resolvePath(itemsPath.asText("$"), input);
if (!items.isArray()) {
throw new FailStateException("States.Runtime", "ItemsPath must reference an array");
private StateResult executeMapState(String name, JsonNode stateDef, JsonNode input, StateMachine sm,
boolean jsonata, String topLevelQueryLanguage, JsonNode context) throws Exception {
JsonNode items;
if (jsonata && stateDef.has("Items")) {
JsonNode itemsNode = stateDef.get("Items");
if (itemsNode.isTextual() && JsonataEvaluator.isExpression(itemsNode.asText())) {
JsonNode statesVar = buildStatesVar(input, null, context);
items = jsonataEvaluator.evaluate(itemsNode.asText(), statesVar);
} else {
items = itemsNode;
}
} else {
JsonNode itemsPath = stateDef.path("ItemsPath");
items = itemsPath.isMissingNode() ? input : resolvePath(itemsPath.asText("$"), input);
}
JsonNode iterator = stateDef.path("Iterator");
if (!items.isArray()) {
throw new FailStateException("States.Runtime", "Items must reference an array");
}
// Support both Iterator (legacy) and ItemProcessor (current AWS naming)
JsonNode iterator = stateDef.has("ItemProcessor") ? stateDef.get("ItemProcessor") : stateDef.path("Iterator");
String startAt = iterator.path("StartAt").asText();
JsonNode iteratorStates = iterator.path("States");
ArrayNode results = objectMapper.createArrayNode();
for (JsonNode item : items) {
results.add(executeBranch(startAt, iteratorStates, item, sm));
results.add(executeBranch(startAt, iteratorStates, item, sm, topLevelQueryLanguage, context));
}
if (jsonata) {
JsonNode output = applyJsonataOutput(stateDef, input, results, context);
return new StateResult(output, stateDef.path("Next").asText(null));
}
JsonNode output = mergeResult(stateDef, input, results);
@@ -377,7 +495,8 @@ public class AslExecutor {
return new StateResult(output, stateDef.path("Next").asText(null));
}
private JsonNode executeBranch(String startAt, JsonNode states, JsonNode input, StateMachine sm) throws Exception {
private JsonNode executeBranch(String startAt, JsonNode states, JsonNode input, StateMachine sm,
String topLevelQueryLanguage, JsonNode context) throws Exception {
List<HistoryEvent> ignored = new ArrayList<>();
AtomicLong eventId = new AtomicLong(0);
JsonNode currentInput = input;
@@ -389,7 +508,9 @@ public class AslExecutor {
throw new RuntimeException("State not found: " + currentState);
}
String type = stateDef.path("Type").asText();
StateResult result = executeState(currentState, type, stateDef, currentInput, ignored, eventId, sm);
boolean stateJsonata = isJsonata(stateDef, topLevelQueryLanguage);
StateResult result = executeState(currentState, type, stateDef, currentInput, ignored, eventId, sm,
stateJsonata, topLevelQueryLanguage, context);
currentInput = result.output();
currentState = result.nextState();
if ("Succeed".equals(type) || stateDef.path("End").asBoolean(false)) {
@@ -399,6 +520,72 @@ public class AslExecutor {
return currentInput;
}
// ──────────────────────────── JSONata helpers ────────────────────────────
private boolean isJsonata(JsonNode stateDef, String topLevelQueryLanguage) {
String stateQL = stateDef.path("QueryLanguage").asText(null);
return QUERY_LANGUAGE_JSONATA.equals(stateQL != null ? stateQL : topLevelQueryLanguage);
}
private JsonNode buildStatesVar(JsonNode input, JsonNode result) {
return buildStatesVar(input, result, null);
}
private JsonNode buildStatesVar(JsonNode input, JsonNode result, JsonNode context) {
ObjectNode states = objectMapper.createObjectNode();
states.set("input", input);
if (result != null) {
states.set("result", result);
}
if (context != null) {
states.set("context", context);
}
return states;
}
/**
* Build the $states.context object for an execution.
* Contains Execution metadata (Id, Input, Name, RoleArn, StartTime).
*/
private JsonNode buildContext(Execution exec, StateMachine sm) {
ObjectNode context = objectMapper.createObjectNode();
ObjectNode execution = objectMapper.createObjectNode();
execution.put("Id", exec.getExecutionArn());
execution.put("Name", exec.getName());
execution.put("RoleArn", sm.getRoleArn());
execution.put("StartTime", java.time.Instant.ofEpochSecond(exec.getStartDate()).toString());
if (exec.getInput() != null) {
execution.set("Input", parseInput(exec.getInput()));
}
context.set("Execution", execution);
ObjectNode stateMachine = objectMapper.createObjectNode();
stateMachine.put("Id", sm.getStateMachineArn());
stateMachine.put("Name", sm.getName());
context.set("StateMachine", stateMachine);
return context;
}
private void updateStateContext(JsonNode execContext, String stateName) {
ObjectNode context = (ObjectNode) execContext;
ObjectNode state = objectMapper.createObjectNode();
state.put("Name", stateName);
state.put("EnteredTime", java.time.Instant.now().toString());
state.put("RetryCount", 0);
context.set("State", state);
}
/**
* Apply JSONata Output field. If Output is present, resolve it as a template with $states bound.
* If absent, use the result directly (or input if result is null).
*/
private JsonNode applyJsonataOutput(JsonNode stateDef, JsonNode input, JsonNode result, JsonNode context) {
if (!stateDef.has("Output")) {
return result != null ? result : input;
}
JsonNode statesVar = buildStatesVar(input, result, context);
return jsonataEvaluator.resolveTemplate(stateDef.get("Output"), statesVar);
}
// ──────────────────────────── Path resolution ────────────────────────────
private JsonNode applyInputPath(JsonNode stateDef, JsonNode input) {
@@ -0,0 +1,118 @@
package io.github.hectorvent.floci.services.stepfunctions;
import com.dashjoin.jsonata.Jsonata;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.util.Iterator;
import java.util.Map;
import static com.dashjoin.jsonata.Jsonata.jsonata;
/**
* Evaluates JSONata expressions for Step Functions.
* Handles {% expression %} delimiters, $states variable binding,
* and recursive template resolution for Arguments/Output fields.
*
* Only pure expressions are evaluated: "{% $states.input.name %}" → any type.
* Strings that are not a single {% %} expression pass through unchanged
* (AWS does not support string interpolation with multiple {% %} blocks).
*/
@ApplicationScoped
public class JsonataEvaluator {
private final ObjectMapper objectMapper;
@Inject
public JsonataEvaluator(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
/**
* Check if the string is a JSONata expression (starts with {% and ends with %}).
*/
static boolean isExpression(String value) {
return value != null && value.startsWith("{%") && value.endsWith("%}");
}
/**
* Strip {% %} delimiters and return the inner expression, trimmed.
*/
static String unwrap(String value) {
return value.substring(2, value.length() - 2).trim();
}
/**
* Evaluate a single JSONata expression string with $states bound.
* The expression may or may not have {% %} delimiters.
*/
JsonNode evaluate(String expression, JsonNode statesVar) {
String expr = isExpression(expression) ? unwrap(expression) : expression;
try {
Jsonata jsonataExpr = jsonata(expr);
Jsonata.Frame frame = jsonataExpr.createFrame();
frame.bind("states", toObject(statesVar));
Object result = jsonataExpr.evaluate(null, frame);
return toJsonNode(result);
} catch (Exception e) {
throw new AslExecutor.FailStateException("States.QueryEvaluationError", e.getMessage());
}
}
/**
* Walk a JSON template (Arguments or Output), evaluating any {% %} strings found.
* Non-expression values pass through unchanged.
*
* Only pure {% expression %} strings are evaluated (can return any JSON type).
* All other strings pass through unchanged.
*/
JsonNode resolveTemplate(JsonNode template, JsonNode statesVar) {
if (template == null || template.isNull() || template.isMissingNode()) {
return template;
}
if (template.isTextual()) {
String text = template.asText();
if (isExpression(text)) {
return evaluate(text, statesVar);
}
return template;
}
if (template.isObject()) {
ObjectNode resolved = objectMapper.createObjectNode();
Iterator<Map.Entry<String, JsonNode>> fields = template.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> entry = fields.next();
resolved.set(entry.getKey(), resolveTemplate(entry.getValue(), statesVar));
}
return resolved;
}
if (template.isArray()) {
ArrayNode resolved = objectMapper.createArrayNode();
for (JsonNode element : template) {
resolved.add(resolveTemplate(element, statesVar));
}
return resolved;
}
// Primitives (number, boolean) pass through
return template;
}
private Object toObject(JsonNode node) {
if (node == null || node.isNull() || node.isMissingNode()) {
return null;
}
return objectMapper.convertValue(node, Object.class);
}
private JsonNode toJsonNode(Object value) {
if (value == null) {
return NullNode.getInstance();
}
return objectMapper.valueToTree(value);
}
}
@@ -116,6 +116,8 @@ public class StepFunctionsJsonHandler {
if (exec.getStopDate() != null) response.put("stopDate", exec.getStopDate());
if (exec.getInput() != null) response.put("input", exec.getInput());
if (exec.getOutput() != null) response.put("output", exec.getOutput());
if (exec.getError() != null) response.put("error", exec.getError());
if (exec.getCause() != null) response.put("cause", exec.getCause());
return Response.ok(response).build();
}
@@ -5,6 +5,8 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.services.stepfunctions.model.Execution;
import io.github.hectorvent.floci.services.stepfunctions.model.HistoryEvent;
import io.github.hectorvent.floci.services.stepfunctions.model.StateMachine;
@@ -25,16 +27,21 @@ public class StepFunctionsService {
private final Map<String, List<HistoryEvent>> historyCache = new ConcurrentHashMap<>();
private final RegionResolver regionResolver;
private final AslExecutor aslExecutor;
private final ObjectMapper objectMapper;
private static final Set<String> JSONPATH_ONLY_FIELDS = Set.of(
"InputPath", "OutputPath", "ResultPath", "ResultSelector", "Parameters", "Result", "ItemsPath");
@Inject
public StepFunctionsService(StorageFactory storageFactory, RegionResolver regionResolver,
AslExecutor aslExecutor) {
AslExecutor aslExecutor, ObjectMapper objectMapper) {
this.stateMachineStore = storageFactory.create("stepfunctions", "sfn-state-machines.json",
new TypeReference<Map<String, StateMachine>>() {});
this.executionStore = storageFactory.create("stepfunctions", "sfn-executions.json",
new TypeReference<Map<String, Execution>>() {});
this.regionResolver = regionResolver;
this.aslExecutor = aslExecutor;
this.objectMapper = objectMapper;
}
// ──────────────────────────── State Machines ────────────────────────────
@@ -45,6 +52,8 @@ public class StepFunctionsService {
throw new AwsException("StateMachineAlreadyExists", "State machine already exists: " + arn, 400);
}
validateDefinition(definition);
StateMachine sm = new StateMachine();
sm.setStateMachineArn(arn);
sm.setName(name);
@@ -161,4 +170,54 @@ public class StepFunctionsService {
public void sendTaskHeartbeat(String taskToken) {
LOG.debugv("Task heartbeat for token {0}", taskToken);
}
// ──────────────────────────── Validation ────────────────────────────
private void validateDefinition(String definition) {
JsonNode def;
try {
def = objectMapper.readTree(definition);
} catch (Exception e) {
throw new AwsException("InvalidDefinition",
"Invalid State Machine Definition: '" + e.getMessage() + "'", 400);
}
String topLevelQL = def.path("QueryLanguage").asText("JSONPath");
boolean topLevelJsonata = "JSONata".equals(topLevelQL);
JsonNode states = def.path("States");
if (states.isObject()) {
var fields = states.fields();
List<String> errors = new ArrayList<>();
while (fields.hasNext()) {
var entry = fields.next();
validateState(entry.getKey(), entry.getValue(), topLevelJsonata, errors);
}
if (!errors.isEmpty()) {
throw new AwsException("InvalidDefinition",
"Invalid State Machine Definition: 'SCHEMA_VALIDATION_FAILED: "
+ String.join(", ", errors) + "'", 400);
}
}
}
private void validateState(String stateName, JsonNode stateDef, boolean topLevelJsonata, List<String> errors) {
String stateQL = stateDef.path("QueryLanguage").asText(null);
boolean stateIsJsonata = stateQL != null ? "JSONata".equals(stateQL) : topLevelJsonata;
// Cannot override to JSONPath when top-level is JSONata
if (topLevelJsonata && "JSONPath".equals(stateQL)) {
errors.add("'QueryLanguage' can not be 'JSONPath' if set to 'JSONata' for whole state machine at /States/" + stateName);
}
// JSONPath-only fields are not allowed when the state uses JSONata
if (stateIsJsonata) {
for (String field : JSONPATH_ONLY_FIELDS) {
if (stateDef.has(field)) {
errors.add("The QueryLanguage is set to 'JSONata', but field '" + field
+ "' is only supported for the 'JSONPath' QueryLanguage at /States/" + stateName);
}
}
}
}
}
@@ -14,6 +14,8 @@ public class Execution {
private String output;
private long startDate;
private Long stopDate;
private String error;
private String cause;
public Execution() {
this.startDate = System.currentTimeMillis() / 1000L;
@@ -42,4 +44,10 @@ public class Execution {
public Long getStopDate() { return stopDate; }
public void setStopDate(Long stopDate) { this.stopDate = stopDate; }
public String getError() { return error; }
public void setError(String error) { this.error = error; }
public String getCause() { return cause; }
public void setCause(String cause) { this.cause = cause; }
}