mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(stepfunctions): fail the state when a JSONata expression returns nothing (#2689)
* fix(stepfunctions): fail the state when a JSONata expression returns nothing An expression that returned nothing lost its key and the execution carried on down the happy path. Real AWS fails the state with States.QueryEvaluationError, names the field the expression was written in and a Catch on that error fires. Three of the positions changed control flow rather than data: a Choice took Default, a Wait did not wait, and a Task sent its request with the argument omitted. evaluateField evaluates one expression and fails the state on a missing node. resolveTemplate calls it at every leaf, carrying the field it is inside and extending it with /key per object key and [i] per array index, so the cause names Output/a/b[0][1] the way AWS does. Choices[n]/Condition, Seconds, Error, Cause, MaxConcurrency and Items call it directly. Three pieces go with it, all of them things a missing node used to need. An undefined array element was a States.Runtime special case with its own wording; AWS answers States.QueryEvaluationError and Output/values[0], the same shape as every other position. resolveTemplateNode existed to let resolveTemplate turn a whole field that returned nothing into a null, so the output would still serialize; there is no output to serialize once the state fails. And the two ResultWriter guards fired only on a key resolveTemplate had dropped, which can no longer happen. An explicit JSON null is a value and still succeeds, everywhere. Closes #2665 * fix(stepfunctions): name a Choice rule's and a Catch clause's own fields under their index A matched Choices[i] rule and a matching Catch[i] clause carry their own Assign and Output, and applyJsonataAssignAndOutput named them 'Output' and 'Assign' as if they were the state's. AWS names them under the rule or the clause: Choices[1]/Output/v, Choices[0]/Assign/x and Catch[1]/Output/v, each measured on the account. The index is the position in Choices or Catch, not the position among the entries that matched. The Catch label was unobservable until now. doExecute calls handleCatch from inside its own catch (FailStateException), so an expression failure raised while resolving the clause's own Output escaped both that clause and its sibling catch (Exception), reaching the method-level handler that answers States.Runtime with the raw message. The execution now fails with the clause's own error, which is what AWS reports and what the clause was catching no longer is. * test(stepfunctions): follow the field path through the crashing evaluator stub * test(stepfunctions): put the Catch clause under test on a Task, as AWS requires aCatchClauseNamesItsOwnOutputUnderTheClauseIndex carried Catch on a Pass state, which AWS refuses: validate-state-machine-definition answers SCHEMA_VALIDATION_FAILED, "Field 'Catch' is not supported" at /States/Boom. The label the test pins is right, Catch[1]/Output/v, and the state now fails on its own Arguments the way the measured execution did, so the definition is one AWS accepts. * docs(stepfunctions): drop the deviation this change removes The key dropped by an expression that returned nothing is no longer Floci's own behaviour: the state fails, as the section below records. --------- Co-authored-by: Facundo Paredes <25681464+facuparedes@users.noreply.github.com>
This commit is contained in:
1 parent
5534460492
commit
ea11e67719
8 files changed
+585
-131
No files matched your search
@@ -391,7 +391,16 @@ public class AslExecutor {
|
||||
currentStateName = null;
|
||||
}
|
||||
} catch (FailStateException e) {
|
||||
StateResult caught = handleCatch(stateDef, currentInput, e, jsonata, execContext, variables);
|
||||
StateResult caught = null;
|
||||
FailStateException failure = e;
|
||||
try {
|
||||
caught = handleCatch(stateDef, currentInput, e, jsonata, execContext, variables);
|
||||
} catch (FailStateException catchClauseFailure) {
|
||||
// A matching Catch clause carries its own Assign and Output, and an
|
||||
// expression there can fail. AWS reports that failure, not the error the
|
||||
// clause was catching, and no later clause catches it.
|
||||
failure = catchClauseFailure;
|
||||
}
|
||||
if (caught != null) {
|
||||
addEvent(history, eventId, stateExitedEventType(type), eventId.get() - 1,
|
||||
Map.of("name", currentStateName, "output", caught.output().toString()));
|
||||
@@ -399,7 +408,7 @@ public class AslExecutor {
|
||||
currentStateName = caught.nextState();
|
||||
continue;
|
||||
}
|
||||
failExecution(exec, history, eventId, e);
|
||||
failExecution(exec, history, eventId, failure);
|
||||
onUpdate.accept(exec, history);
|
||||
return;
|
||||
}
|
||||
@@ -593,7 +602,8 @@ public class AslExecutor {
|
||||
var effectiveInput = input;
|
||||
if (stateDef.has("Arguments")) {
|
||||
var statesVar = buildStatesVar(input, null, context);
|
||||
effectiveInput = jsonataEvaluator.resolveTemplate(stateDef.get("Arguments"), statesVar, variables);
|
||||
effectiveInput = jsonataEvaluator.resolveTemplate(
|
||||
stateDef.get("Arguments"), "Arguments", statesVar, variables);
|
||||
}
|
||||
taskResult = mockedSteps != null
|
||||
? mockedTaskResult(mockedSteps, stateName, attempt)
|
||||
@@ -1650,14 +1660,17 @@ public class AslExecutor {
|
||||
if (jsonata) {
|
||||
JsonNode statesVar = buildStatesVar(input, null, context);
|
||||
JsonNode choices = stateDef.path("Choices");
|
||||
for (JsonNode choice : choices) {
|
||||
for (int i = 0; i < choices.size(); i++) {
|
||||
JsonNode choice = choices.get(i);
|
||||
String condition = choice.path("Condition").asText(null);
|
||||
if (condition != null) {
|
||||
JsonNode result = jsonataEvaluator.evaluate(condition, statesVar, variables);
|
||||
JsonNode result = jsonataEvaluator.evaluateField(
|
||||
condition, "Choices[" + i + "]/Condition", statesVar, variables);
|
||||
if (result.isBoolean() && result.asBoolean()) {
|
||||
// A matched rule carries its own Assign and Output; the state-level ones
|
||||
// belong to the Default path and do not run here.
|
||||
JsonNode output = applyJsonataAssignAndOutput(choice, statesVar, input, variables);
|
||||
JsonNode output = applyJsonataAssignAndOutput(
|
||||
choice, "Choices[" + i + "]/", statesVar, input, variables);
|
||||
return new StateResult(output, choice.path("Next").asText());
|
||||
}
|
||||
}
|
||||
@@ -1665,7 +1678,7 @@ public class AslExecutor {
|
||||
String defaultState = stateDef.path("Default").asText(null);
|
||||
if (defaultState != null) {
|
||||
// No rule matched: the state-level Assign and Output apply on the Default path.
|
||||
JsonNode output = applyJsonataAssignAndOutput(stateDef, statesVar, input, variables);
|
||||
JsonNode output = applyJsonataAssignAndOutput(stateDef, "", statesVar, input, variables);
|
||||
return new StateResult(output, defaultState);
|
||||
}
|
||||
throw new FailStateException("States.NoChoiceMatched", "No choice rule matched and no default state");
|
||||
@@ -1777,7 +1790,8 @@ public class AslExecutor {
|
||||
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, variables);
|
||||
JsonNode result = jsonataEvaluator.evaluateField(
|
||||
secondsNode.asText(), "Seconds", statesVar, variables);
|
||||
seconds = Math.min(result.asInt(), MAX_WAIT_SECONDS);
|
||||
} else {
|
||||
seconds = Math.min(secondsNode.asInt(), MAX_WAIT_SECONDS);
|
||||
@@ -1818,10 +1832,10 @@ public class AslExecutor {
|
||||
if (jsonata) {
|
||||
JsonNode statesVar = buildStatesVar(input, null, context);
|
||||
if (error != null && JsonataEvaluator.isExpression(error)) {
|
||||
error = jsonataEvaluator.evaluate(error, statesVar, variables).asText();
|
||||
error = jsonataEvaluator.evaluateField(error, "Error", statesVar, variables).asText();
|
||||
}
|
||||
if (cause != null && JsonataEvaluator.isExpression(cause)) {
|
||||
cause = jsonataEvaluator.evaluate(cause, statesVar, variables).asText();
|
||||
cause = jsonataEvaluator.evaluateField(cause, "Cause", statesVar, variables).asText();
|
||||
}
|
||||
}
|
||||
throw new FailStateException(error, cause);
|
||||
@@ -2031,7 +2045,7 @@ public class AslExecutor {
|
||||
if (jsonata && value.isTextual() && JsonataEvaluator.isExpression(value.asText())) {
|
||||
jsonataExpression = true;
|
||||
JsonNode statesVar = buildStatesVar(mapInput, null, context);
|
||||
value = jsonataEvaluator.evaluate(value.asText(), statesVar, variables);
|
||||
value = jsonataEvaluator.evaluateField(value.asText(), "MaxConcurrency", statesVar, variables);
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
@@ -2118,19 +2132,8 @@ public class AslExecutor {
|
||||
// InputPath, which is supplied by executeMapState.
|
||||
JsonNode loc;
|
||||
if (jsonata && writer.has("Arguments")) {
|
||||
JsonNode arguments = writer.get("Arguments");
|
||||
loc = jsonataEvaluator.resolveTemplate(
|
||||
arguments, buildStatesVar(input, null, context), variables);
|
||||
if (arguments.isObject()) {
|
||||
if (arguments.has("Bucket") && !loc.has("Bucket")) {
|
||||
throw new FailStateException("States.QueryEvaluationError",
|
||||
"ResultWriter Bucket must resolve to a string");
|
||||
}
|
||||
if (arguments.has("Prefix") && !loc.has("Prefix")) {
|
||||
throw new FailStateException("States.QueryEvaluationError",
|
||||
"ResultWriter Prefix must resolve to a string");
|
||||
}
|
||||
}
|
||||
loc = jsonataEvaluator.resolveTemplate(writer.get("Arguments"), "ResultWriter/Arguments",
|
||||
buildStatesVar(input, null, context), variables);
|
||||
} else if (writer.has("Parameters")) {
|
||||
loc = resolveParameters(writer.get("Parameters"), input, context);
|
||||
} else {
|
||||
@@ -2287,7 +2290,8 @@ public class AslExecutor {
|
||||
JsonNode itemsNode = stateDef.get("Items");
|
||||
if (itemsNode.isTextual() && JsonataEvaluator.isExpression(itemsNode.asText())) {
|
||||
JsonNode statesVar = buildStatesVar(input, null, context);
|
||||
return new ResolvedMapItems(jsonataEvaluator.evaluate(itemsNode.asText(), statesVar, variables),
|
||||
return new ResolvedMapItems(
|
||||
jsonataEvaluator.evaluateField(itemsNode.asText(), "Items", statesVar, variables),
|
||||
MapItemsSource.DEFAULT);
|
||||
}
|
||||
return new ResolvedMapItems(itemsNode, MapItemsSource.DEFAULT);
|
||||
@@ -2323,7 +2327,8 @@ public class AslExecutor {
|
||||
JsonNode resolvedParameters;
|
||||
if (jsonata && itemReader.has("Arguments")) {
|
||||
JsonNode statesVar = buildStatesVar(input, null, context);
|
||||
resolvedParameters = jsonataEvaluator.resolveTemplate(itemReader.get("Arguments"), statesVar, variables);
|
||||
resolvedParameters = jsonataEvaluator.resolveTemplate(
|
||||
itemReader.get("Arguments"), "ItemReader/Arguments", statesVar, variables);
|
||||
} else {
|
||||
JsonNode parameters = itemReader.path("Parameters");
|
||||
resolvedParameters = resolveParameters(parameters, input, context);
|
||||
@@ -2519,28 +2524,35 @@ public class AslExecutor {
|
||||
private JsonNode applyJsonataOutput(JsonNode holder, JsonNode input, JsonNode result, JsonNode context,
|
||||
ObjectNode variables) {
|
||||
JsonNode statesVar = buildStatesVar(input, result, context);
|
||||
return applyJsonataAssignAndOutput(holder, statesVar, result != null ? result : input, variables);
|
||||
return applyJsonataAssignAndOutput(holder, "", statesVar, result != null ? result : input, variables);
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply the Assign and Output fields of anything that can carry them: a state, a Choice rule, or
|
||||
* a Catch block. {@code fallbackOutput} is the value that becomes the output when Output is absent.
|
||||
* a Catch clause. {@code fallbackOutput} is the value that becomes the output when Output is absent.
|
||||
*
|
||||
* <p>{@code holderPrefix} is what AWS puts before the holder's own field names in the cause of a
|
||||
* States.QueryEvaluationError: empty for a state, {@code "Choices[1]/"} for the second Choice
|
||||
* rule, {@code "Catch[1]/"} for the second Catch clause. AWS names a rule's own Output
|
||||
* {@code Choices[1]/Output/v}, not {@code Output/v}.
|
||||
*/
|
||||
private JsonNode applyJsonataAssignAndOutput(JsonNode holder, JsonNode statesVar, JsonNode fallbackOutput,
|
||||
ObjectNode variables) {
|
||||
JsonNode assigned = evaluateJsonataAssign(holder, statesVar, variables);
|
||||
private JsonNode applyJsonataAssignAndOutput(JsonNode holder, String holderPrefix, JsonNode statesVar,
|
||||
JsonNode fallbackOutput, ObjectNode variables) {
|
||||
JsonNode assigned = evaluateJsonataAssign(holder, holderPrefix, statesVar, variables);
|
||||
JsonNode output = holder.has("Output")
|
||||
? jsonataEvaluator.resolveTemplate(holder.get("Output"), statesVar, variables)
|
||||
? jsonataEvaluator.resolveTemplate(holder.get("Output"), holderPrefix + "Output", statesVar, variables)
|
||||
: fallbackOutput;
|
||||
commitJsonataAssign(assigned, variables);
|
||||
return output;
|
||||
}
|
||||
|
||||
private JsonNode evaluateJsonataAssign(JsonNode holder, JsonNode statesVar, ObjectNode variables) {
|
||||
private JsonNode evaluateJsonataAssign(JsonNode holder, String holderPrefix, JsonNode statesVar,
|
||||
ObjectNode variables) {
|
||||
if (!holder.has("Assign")) {
|
||||
return null;
|
||||
}
|
||||
JsonNode assigned = jsonataEvaluator.resolveTemplate(holder.get("Assign"), statesVar, variables);
|
||||
JsonNode assigned = jsonataEvaluator.resolveTemplate(
|
||||
holder.get("Assign"), holderPrefix + "Assign", statesVar, variables);
|
||||
if (assigned == null || !assigned.isObject()) {
|
||||
throw new FailStateException("States.Runtime", "Assign must evaluate to an object");
|
||||
}
|
||||
@@ -3385,7 +3397,8 @@ public class AslExecutor {
|
||||
}
|
||||
String error = failure.error != null ? failure.error : "States.Runtime";
|
||||
String cause = failure.cause != null ? failure.cause : "";
|
||||
for (JsonNode catcher : catchers) {
|
||||
for (int i = 0; i < catchers.size(); i++) {
|
||||
JsonNode catcher = catchers.get(i);
|
||||
if (!catchMatches(catcher, error)) {
|
||||
continue;
|
||||
}
|
||||
@@ -3401,7 +3414,8 @@ public class AslExecutor {
|
||||
// scope the catching state lives in — so for a Parallel or Map it lands in the outer
|
||||
// scope, not the branch scope that failed.
|
||||
JsonNode statesVar = buildCatchStatesVar(input, errorOutput, context);
|
||||
JsonNode output = applyJsonataAssignAndOutput(catcher, statesVar, errorOutput, variables);
|
||||
JsonNode output = applyJsonataAssignAndOutput(
|
||||
catcher, "Catch[" + i + "]/", statesVar, errorOutput, variables);
|
||||
return new StateResult(output, next);
|
||||
}
|
||||
return new StateResult(mergeResult(catcher, input, errorOutput), next);
|
||||
|
||||
+33
-35
@@ -256,33 +256,46 @@ public class JsonataEvaluator {
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk a JSON template (Arguments or Output), evaluating any {% %} strings found.
|
||||
* Evaluate the single expression an ASL field holds, failing the state when it returns nothing.
|
||||
* AWS names the field in the cause and a {@code Catch} on States.QueryEvaluationError fires:
|
||||
* a Wait whose Seconds returned nothing does not wait zero seconds, it fails.
|
||||
*
|
||||
* <p>{@code field} is the field's path relative to the state, as AWS writes it: {@code Seconds},
|
||||
* {@code Choices[1]/Condition}, {@code Output/a/b}.
|
||||
*/
|
||||
JsonNode evaluateField(String expression, String field, JsonNode statesVar, JsonNode variables) {
|
||||
JsonNode value = evaluate(expression, statesVar, variables);
|
||||
if (value.isMissingNode()) {
|
||||
throw new FailStateException("States.QueryEvaluationError",
|
||||
"The JSONata expression '" + (isExpression(expression) ? unwrap(expression) : expression)
|
||||
+ "' specified for the field '" + field + "' returned nothing (undefined).");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk a JSON template (Arguments, Output or Assign), 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.
|
||||
*
|
||||
* <p>{@code field} is the template's own field name, which the walk extends with {@code /key}
|
||||
* per object key and {@code [i]} per array index so a failing expression is named the way AWS
|
||||
* names it: {@code Output/a/b[0][1]}.
|
||||
*/
|
||||
JsonNode resolveTemplate(JsonNode template, JsonNode statesVar) {
|
||||
return resolveTemplate(template, statesVar, null);
|
||||
JsonNode resolveTemplate(JsonNode template, String field, JsonNode statesVar) {
|
||||
return resolveTemplate(template, field, statesVar, null);
|
||||
}
|
||||
|
||||
JsonNode resolveTemplate(JsonNode template, JsonNode statesVar, JsonNode variables) {
|
||||
JsonNode resolveTemplate(JsonNode template, String field, JsonNode statesVar, JsonNode variables) {
|
||||
if (template == null || template.isMissingNode()) {
|
||||
return template;
|
||||
}
|
||||
JsonNode resolved = resolveTemplateNode(template, statesVar, variables);
|
||||
// A resolved Output or Arguments is serialized as it stands, and a missing node writes
|
||||
// itself as the empty string, which no client can parse. "Returned nothing" is a value only
|
||||
// in the positions resolveTemplateNode reads it in: an object field it drops, an array
|
||||
// element it fails the state on. As the whole field there is nothing to drop it from.
|
||||
return resolved.isMissingNode() ? NullNode.getInstance() : resolved;
|
||||
}
|
||||
|
||||
private JsonNode resolveTemplateNode(JsonNode template, JsonNode statesVar, JsonNode variables) {
|
||||
if (template.isTextual()) {
|
||||
String text = template.asText();
|
||||
if (isExpression(text)) {
|
||||
return evaluate(text, statesVar, variables);
|
||||
return evaluateField(text, field, statesVar, variables);
|
||||
}
|
||||
return template;
|
||||
}
|
||||
@@ -291,30 +304,15 @@ public class JsonataEvaluator {
|
||||
Iterator<Map.Entry<String, JsonNode>> fields = template.fields();
|
||||
while (fields.hasNext()) {
|
||||
Map.Entry<String, JsonNode> entry = fields.next();
|
||||
JsonNode value = resolveTemplateNode(entry.getValue(), statesVar, variables);
|
||||
// Per JSONata spec: an expression that returned nothing is omitted from object
|
||||
// output, matching real AWS Step Functions behavior. A JSON null is a value and
|
||||
// keeps its key: AWS answers {"v":null} to Output {"v":"{% null %}"}.
|
||||
if (!value.isMissingNode()) {
|
||||
resolved.set(entry.getKey(), value);
|
||||
}
|
||||
resolved.set(entry.getKey(),
|
||||
resolveTemplate(entry.getValue(), field + "/" + entry.getKey(), statesVar, variables));
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
if (template.isArray()) {
|
||||
ArrayNode resolved = objectMapper.createArrayNode();
|
||||
for (int i = 0; i < template.size(); i++) {
|
||||
JsonNode element = template.get(i);
|
||||
JsonNode value = resolveTemplateNode(element, statesVar, variables);
|
||||
// Per real AWS behavior: an array element that returned nothing fails the execution.
|
||||
// Unlike object fields (which are omitted), undefined in an array is a runtime error.
|
||||
// A JSON null is a value and stays: AWS answers [null,1] to ["{% null %}",1].
|
||||
if (value.isMissingNode()) {
|
||||
String expr = element.isTextual() ? element.asText() : element.toString();
|
||||
throw new FailStateException("States.Runtime",
|
||||
"The JSONata expression '" + expr + "' at array index " + i + " returned nothing (undefined).");
|
||||
}
|
||||
resolved.add(value);
|
||||
resolved.add(resolveTemplate(template.get(i), field + "[" + i + "]", statesVar, variables));
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
@@ -336,9 +334,9 @@ public class JsonataEvaluator {
|
||||
|
||||
/**
|
||||
* The evaluation result. A Java null is the expression returning nothing, which
|
||||
* {@link #resolveTemplateNode} drops from an object; the JSONata null marker is a JSON null,
|
||||
* which it keeps. Nested inside an object or an array there is no undefined, so every null
|
||||
* there is a JSON null.
|
||||
* {@link #evaluateField} fails the state on; the JSONata null marker is a JSON null, which is a
|
||||
* value like any other. Nested inside an object or an array there is no undefined, so every
|
||||
* null there is a JSON null.
|
||||
*/
|
||||
private JsonNode toJsonNode(Object value) {
|
||||
return value == null ? MissingNode.getInstance() : fromJsonataValue(value);
|
||||
|
||||
Reference in new issue
Block a user