fix(stepfunctions): bound $range allocation at the AWS memory limit (#2790)

* fix(stepfunctions): open a JSONata query evaluation cause with the AWS sentence

evaluateField now wraps a thrown JSONata failure with "The JSONata expression
'<expr>' specified for the field '<field>' threw an error during evaluation"
before the existing tail, matching real AWS. The separator is ". " when the
tail carries a JSONata error code and ": " when it is a bound message with
none. evaluate() keeps producing the bare tail unchanged for its direct
callers.

evaluate() stays the only place the JSONata engine is called and the only
catch around it: evaluateField calls it and enriches the failure it throws,
rather than reaching the engine itself. QueryEvaluationFailure carries the
separator across that throw, so a catch added to evaluate() later covers both
entry points. All 11 AslExecutor call sites reach the evaluator through
evaluateField or resolveTemplate, so a catch installed anywhere else would be
dead on every real execution path.

* fix(stepfunctions): catch Error from JSONata evaluation as States.QueryEvaluationError

An OutOfMemoryError or StackOverflowError raised while parsing or evaluating a JSONata
expression escaped JsonataEvaluator's catch (Exception) and reached AslExecutor's own
last-resort catch (Error), failing the whole execution as States.Runtime before the
state's own Retry and Catch ever ran. JsonataEvaluator.evaluate now also catches Error at
the same seam and translates it into a FailStateException with States.QueryEvaluationError,
so a Catch on the state (or on States.ALL) can recover, matching real AWS.

Closes #2738

* fix(stepfunctions): match AWS on the JSONata expressions it refuses

Four expression classes where Floci and AWS disagreed on whether the state
fails at all, all measured against us-east-1 with test-state.

The evaluation-depth ceiling drops from 500 to 100, which is AWS's own: five
nesting shapes bisected one n at a time all put its last accepted evaluation at
depth 100 and its first refusal at 101. An addition chain nests one level per
term and AWS accepts 100 terms; parentheses, brackets and the chain operator
nest one level more than their count and AWS accepts 99 of each; a
non-tail-recursive $f(n) nests 3n+5 and AWS accepts n=31, depth 98, refusing
n=32, depth 101.

The memory an expression may hold is now bounded, which it was not: one value an
evaluation step produces may hold 6,990,256 bytes, counting a number as eight
and a character as one. The two shapes AWS was bisected on, [1..873782] and a
string doubled 22 times, bracket a number at between five and nine bytes a
character: with the bound at 873,782 numbers, anything in that range leaves
4,194,304 characters inside it and 8,388,608 outside, and four or ten does not.
Eight is what a double costs, and it is inside the bracket. The check walks the
value and stops at the bound, and a lazy range is counted from its length rather
than materialised.

$eval is unbound, as AWS disables it, so a call to it answers T1006 the way AWS
does and $parse stays the replacement.

A non-finite number the expression itself evaluates to comes back as the string
AWS writes for it, since JSON has no literal for one: 1/0 and 1e308*10 are
"Infinity" and $parseInteger("abc","0") is "NaN", where the first two raised
D1001 and the third returned nothing and failed the field. One the library's
arithmetic buries inside a value, as in [1/0], still fails the state, because
the refusal is the only place the library leaves the value and it has already
unwound what was being built; one a function answers nests like any other value.
$formatNumber checks its picture string against the fourteen rules of XPath F&O
4.7.3 first, so $formatNumber(1, "x") fails with D3086 instead of answering "x1".
Which of the fourteen codes AWS reports is what fixes the order the rules are
read in: a picture with no digit breaks D3085 and D3086 at once and AWS reports
the second, so the last rule broken names the failure rather than the first.

Closes #2737

* fix(stepfunctions): bound $range allocation at the AWS memory limit

AWS accepts $range(1, 360145, 1), all 360,145 elements, and refuses one
element more on its own memory limit. Floci's $range had no such bound:
it built the array in full before the memory check on the value ever ran,
so a call far past AWS's limit spent gigabytes and either exhausted the
heap or hung instead of failing the state cleanly.

range() now refuses a call whose produced element count exceeds the
measured bound before allocating, throwing the same
States.QueryEvaluationError JException the held-memory check raises,
whose message is now a single shared constant. Other built arrays keep
the looser, byte-based bound.

* test(stepfunctions): pin the number model of the execution input

Measured against real AWS (test-state, 2026-08-30): a Pass state with
Output {"v": "{% $states.input.amount %}"} answers {"v": 1} for input
{"amount": 1.0} and {"v": 9.223372036854776E18} for input
{"amount": 9223372036854775808}. That is exactly what routing the
execution input through toJsonataValue/toJsonataNumber already
produces, so these two tests pin it against silent regression.

---------

Co-authored-by: Facundo Paredes <25681464+facuparedes@users.noreply.github.com>
This commit is contained in:
Facundo ParedesandFacundo Paredes authored and GitHub committed 2026-08-31 00:16:52 -04:00
1 parent ead70eae4d
commit f7f16cd2a3
6 files changed
+1102 -56

No files matched your search

@@ -0,0 +1,238 @@
package io.github.hectorvent.floci.services.stepfunctions;
import com.dashjoin.jsonata.JException;
import java.util.Map;
import java.util.regex.Pattern;
/**
* The picture string of {@code $formatNumber}, checked against the rules XPath F&amp;O 4.7.3 gives
* it, which is what AWS applies and the bundled JSONata library does not: it formats through
* {@link java.text.DecimalFormat} and answers {@code "x1"} for {@code $formatNumber(1, "x")} where
* AWS fails the state with D3086.
*
* <p>A picture may break more than one of the fourteen rules, and the code reported is the last
* one, which is the one AWS reports: a picture with no digit at all breaks two at once, the
* mantissa holding no digit (D3085) and the active part holding a passive character (D3086), and
* D3086 is the answer.
*
* <p>The symbols the rules are written in terms of are the built-in ones, each replaceable through
* the third argument of {@code $formatNumber}.
*/
final class FormatNumberPicture {
private final char decimalSeparator;
private final char exponentSeparator;
private final char groupingSeparator;
private final char digit;
private final char patternSeparator;
private final char percent;
private final char perMille;
private final char zeroDigit;
private FormatNumberPicture(Map<?, ?> options) {
decimalSeparator = symbol(options, "decimal-separator", '.');
exponentSeparator = symbol(options, "exponent-separator", 'e');
groupingSeparator = symbol(options, "grouping-separator", ',');
digit = symbol(options, "digit", '#');
patternSeparator = symbol(options, "pattern-separator", ';');
percent = symbol(options, "percent", '%');
perMille = symbol(options, "per-mille", '‰');
zeroDigit = symbol(options, "zero-digit", '0');
}
private static char symbol(Map<?, ?> options, String name, char builtIn) {
Object override = options == null ? null : options.get(name);
return override instanceof String text && text.length() == 1 ? text.charAt(0) : builtIn;
}
/**
* Fails with the code of the rule the picture breaks, or returns when it breaks none. The
* picture is split into sub-pictures first, one for positive numbers and an optional second
* one for negative numbers.
*/
static void validate(String picture, Map<?, ?> options) {
FormatNumberPicture symbols = new FormatNumberPicture(options);
String[] subPictures =
picture.split(Pattern.quote(String.valueOf(symbols.patternSeparator)), -1);
if (subPictures.length > 2) {
throw new JException("D3080", -1);
}
for (String subPicture : subPictures) {
String error = symbols.errorIn(symbols.split(subPicture));
if (error != null) {
throw new JException(error, -1);
}
}
}
/**
* The parts a sub-picture is read in. The prefix and the suffix are the passive characters at
* either edge, so the active part is what sits between them; the mantissa is the active part
* up to the exponent separator, and the integer and fractional parts are the mantissa either
* side of the decimal separator.
*/
private record Parts(String subPicture, String activePart, String mantissaPart,
String integerPart, String fractionalPart, String exponentPart) { }
private Parts split(String subPicture) {
int activeStart = 0;
while (activeStart < subPicture.length() && !isActiveOutsideExponent(subPicture.charAt(activeStart))) {
activeStart++;
}
int activeEnd = subPicture.length();
while (activeEnd > 0 && !isActiveOutsideExponent(subPicture.charAt(activeEnd - 1))) {
activeEnd--;
}
// A sub-picture with no active character at all is all active part and has no edges.
if (activeEnd == 0) {
activeStart = 0;
activeEnd = subPicture.length();
}
String activePart = subPicture.substring(activeStart, activeEnd);
int exponentAt = subPicture.indexOf(exponentSeparator, activeStart) - activeStart;
boolean hasExponent = exponentAt >= 0 && exponentAt <= activePart.length();
String mantissaPart = hasExponent ? upTo(activePart, exponentAt) : activePart;
String exponentPart = hasExponent ? from(activePart, exponentAt + 1) : null;
int decimalAt = mantissaPart.indexOf(decimalSeparator);
String integerPart = decimalAt < 0 ? mantissaPart : mantissaPart.substring(0, decimalAt);
String fractionalPart =
decimalAt < 0 ? subPicture.substring(activeEnd) : mantissaPart.substring(decimalAt + 1);
return new Parts(subPicture, activePart, mantissaPart, integerPart, fractionalPart, exponentPart);
}
/** The exponent separator is located in the sub-picture, so it can point past the active part. */
private static String upTo(String text, int end) {
return text.substring(0, Math.min(end, text.length()));
}
private static String from(String text, int start) {
return text.substring(Math.min(start, text.length()));
}
/**
* The code of the last rule the sub-picture breaks, or null when it breaks none. The rules are
* read in the order the codes number them, and a later one replaces what an earlier one found.
*/
private String errorIn(Parts parts) {
String error = separatorCountError(parts.subPicture());
error = later(error, digitError(parts));
error = later(error, groupingSeparatorError(parts));
error = later(error, optionalDigitError(parts));
error = later(error, exponentError(parts));
return error;
}
private static String later(String error, String found) {
return found != null ? found : error;
}
/** D3081 to D3084: a symbol that may appear once appears twice, or two that exclude each other. */
private String separatorCountError(String subPicture) {
String error = null;
if (subPicture.indexOf(decimalSeparator) != subPicture.lastIndexOf(decimalSeparator)) {
error = "D3081";
}
if (subPicture.indexOf(percent) != subPicture.lastIndexOf(percent)) {
error = "D3082";
}
if (subPicture.indexOf(perMille) != subPicture.lastIndexOf(perMille)) {
error = "D3083";
}
if (subPicture.indexOf(percent) >= 0 && subPicture.indexOf(perMille) >= 0) {
error = "D3084";
}
return error;
}
/** D3085 and D3086: the mantissa holds no digit, and the active part holds a passive character. */
private String digitError(Parts parts) {
String error = null;
if (!holdsDigit(parts.mantissaPart())) {
error = "D3085";
}
if (!parts.activePart().chars().allMatch(character -> isActive((char) character))) {
error = "D3086";
}
return error;
}
/** D3087 to D3089: a grouping separator next to the decimal separator, at the end, or doubled. */
private String groupingSeparatorError(Parts parts) {
String subPicture = parts.subPicture();
int decimalAt = subPicture.indexOf(decimalSeparator);
String error = null;
if (decimalAt >= 0 && (characterAt(subPicture, decimalAt - 1) == groupingSeparator
|| characterAt(subPicture, decimalAt + 1) == groupingSeparator)) {
error = "D3087";
} else if (decimalAt < 0 && !parts.integerPart().isEmpty()
&& parts.integerPart().charAt(parts.integerPart().length() - 1) == groupingSeparator) {
error = "D3088";
}
if (subPicture.indexOf("" + groupingSeparator + groupingSeparator) >= 0) {
error = "D3089";
}
return error;
}
/**
* D3090 and D3091: an optional digit with a mandatory one on the side the number grows from,
* which is to its left in the integer part and to its right in the fractional part.
*/
private String optionalDigitError(Parts parts) {
String error = null;
int firstOptional = parts.integerPart().indexOf(digit);
if (firstOptional >= 0 && holdsDecimalDigit(parts.integerPart().substring(0, firstOptional))) {
error = "D3090";
}
int lastOptional = parts.fractionalPart().lastIndexOf(digit);
if (lastOptional >= 0 && holdsDecimalDigit(parts.fractionalPart().substring(lastOptional))) {
error = "D3091";
}
return error;
}
/** D3092 and D3093: an exponent alongside a percent, and an exponent that is not all digits. */
private String exponentError(Parts parts) {
if (parts.exponentPart() == null) {
return null;
}
boolean scaled = parts.subPicture().indexOf(percent) >= 0
|| parts.subPicture().indexOf(perMille) >= 0;
String error = null;
if (!parts.exponentPart().isEmpty() && scaled) {
error = "D3092";
}
if (parts.exponentPart().isEmpty()
|| !parts.exponentPart().chars().allMatch(character -> isDecimalDigit((char) character))) {
error = "D3093";
}
return error;
}
private static char characterAt(String text, int index) {
return index >= 0 && index < text.length() ? text.charAt(index) : '\0';
}
private boolean holdsDigit(String text) {
return text.chars().anyMatch(character -> isDecimalDigit((char) character) || character == digit);
}
private boolean holdsDecimalDigit(String text) {
return text.chars().anyMatch(character -> isDecimalDigit((char) character));
}
private boolean isDecimalDigit(char character) {
return character >= zeroDigit && character < zeroDigit + 10;
}
private boolean isActive(char character) {
return isDecimalDigit(character) || character == decimalSeparator
|| character == exponentSeparator || character == groupingSeparator
|| character == digit || character == patternSeparator;
}
private boolean isActiveOutsideExponent(char character) {
return isActive(character) && character != exponentSeparator;
}
}
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.stepfunctions;
import com.dashjoin.jsonata.Functions;
import com.dashjoin.jsonata.JException;
import com.dashjoin.jsonata.Jsonata;
import com.dashjoin.jsonata.Utils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
@@ -13,6 +14,7 @@ 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.fasterxml.jackson.databind.node.TextNode;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -21,6 +23,7 @@ import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HexFormat;
import java.util.Iterator;
@@ -91,14 +94,54 @@ public class JsonataEvaluator {
static final long EVALUATION_TIMEOUT_MILLIS = 5_000;
/**
* How deep evaluation may nest before the state fails. A non-tail-recursive function nests at
* roughly {@code 3n+5} for {@code $f(n)}; measured against AWS with {@code test-state}, AWS
* accepts {@code n=30} (depth 95) and refuses {@code n=40} (depth 125), so its own ceiling
* sits near 100. Five times that leaves no expression AWS accepts able to reach this bound,
* and it still trips before the JVM stack does: on a 1 MB thread stack the bound raises a
* JSONata error while 1000 or more overflows the stack instead.
* How deep evaluation may nest before the state fails, which is the ceiling AWS itself holds:
* AWS accepts an evaluation at depth 100 and refuses one at 101, whatever the nesting is made
* of. An addition chain nests one level per term, parentheses, brackets and the chain operator
* nest one level more than their count, and a non-tail-recursive {@code $f(n)} nests
* {@code 3n+5}.
*
* <p>The bound trips before the JVM stack does: on a 1 MB thread stack it raises a JSONata
* error, which the state fails on, where 1000 or more overflows the stack instead.
*/
static final int MAX_EVALUATION_DEPTH = 500;
static final int MAX_EVALUATION_DEPTH = 100;
/** What one number, boolean or null of a value counts as against {@link #MAX_EXPRESSION_BYTES}. */
private static final int BYTES_PER_SCALAR = 8;
/**
* How much memory one value an expression builds may hold before the state fails, 6,990,256
* bytes, which is the bound AWS itself holds: AWS accepts {@code [1..873782]} and refuses one
* element more, and it accepts a string of 2^22 characters and refuses one of 2^23. Both of
* those refusals land just past this bound when a number counts as {@link #BYTES_PER_SCALAR}
* bytes and a character as one.
*
* <p>What is bounded is the largest single value an evaluation step produces, not the sum of
* everything alive at once. Checking it walks that value and stops as soon as the walk passes
* the bound, so one check costs at most 873,783 steps, and a lazy range is counted from its
* length without being materialised.
*/
static final long MAX_EXPRESSION_BYTES = 873_782L * BYTES_PER_SCALAR;
/**
* The largest number of elements a single {@code $range} call may produce before floci
* refuses it, the bound AWS itself holds specifically for {@code $range}: AWS accepts
* {@code $range(1, 360145, 1)}, returning all 360,145 elements, and refuses one element more
* with the same memory refusal as {@link #MAX_EXPRESSION_BYTES}. That general bound is looser
* (873,782 scalars) and is checked only on the value an evaluation step has already built; a
* lazy literal range such as {@code [1..873782]} is exempt from it on AWS, and stays exempt
* here. {@code $range} gets a bound of its own because it is the one construct that actually
* allocates a list of that size, so the general check would see the memory only after the
* allocation already happened.
*/
static final int MAX_RANGE_ELEMENTS = 360_145;
/**
* AWS's own sentence for an expression that exceeded its evaluation memory limit, shared by
* the bound on a built value ({@link #boundHeldMemory}) and the pre-allocation cap on
* {@code $range} ({@link #range}).
*/
private static final String MEMORY_LIMIT_EXCEEDED =
"Expression evaluation memory limit exceeded: Check for excessive memory usage";
private final ObjectMapper objectMapper;
private final ObjectReader strictJsonReader;
@@ -161,6 +204,7 @@ public class JsonataEvaluator {
JsonNode evaluate(String expression, JsonNode statesVar, JsonNode variables) {
String expr = isExpression(expression) ? unwrap(expression) : expression;
ExpressionNesting nesting = new ExpressionNesting();
try {
Jsonata jsonataExpr = jsonata(expr);
// Off, the library keeps JSONata's null marker in the result instead of flattening it
@@ -173,7 +217,12 @@ public class JsonataEvaluator {
// case loops for ever and the execution never leaves RUNNING. The bounds raise a
// JSONata error, which the catch below turns into States.QueryEvaluationError.
frame.setRuntimeBounds(evaluationTimeoutMillis, maxEvaluationDepth);
nesting.trackOn(frame);
boundHeldMemory(frame);
stepFunctionsExtensions.forEach(frame::bind);
// AWS disables $eval, and the library ships it. An unbound name called as a function is
// the library's own T1006, which is the code AWS answers. $parse is the replacement.
frame.bind("eval", (Object) null);
// Workflow variables (the Assign field) are referenced as top-level $name in AWS's
// JSONata dialect, e.g. $CheckpointCount. They are bound alongside $states, never
// inside it: AWS reserves $states for input/result/errorOutput/context only. $states is
@@ -190,36 +239,165 @@ public class JsonataEvaluator {
Object result = jsonataExpr.evaluate(null, frame);
return toJsonNode(result);
} catch (Exception e) {
throw new AslExecutor.FailStateException("States.QueryEvaluationError", queryEvaluationCause(e));
JsonNode nonFinite = nesting.isTheWholeExpression() ? nonFiniteRefusal(e) : null;
if (nonFinite != null) {
return nonFinite;
}
throw queryEvaluationFailure(e);
} catch (StackOverflowError | OutOfMemoryError e) {
// These two are the resource an expression itself spends: nesting deep enough to
// exhaust the parser's stack, and asking for a value larger than the JVM can hold.
// AWS bounds both and fails the state with States.QueryEvaluationError, so the state's
// own Retry and Catch (and a Catch on States.ALL) still see it. Every other Error says
// the runtime is broken rather than this expression, and it keeps going to
// AslExecutor's terminal catch (Error e), which fails the execution as States.Runtime
// and rethrows. The message is often null here, so toString() carries the diagnosis,
// the same way that terminal catch already renders it. QueryEvaluationFailure (rather
// than a bare FailStateException) is what lets evaluateField prepend AWS's sentence,
// colon-separated since this cause carries no JSONata code.
throw new QueryEvaluationFailure(e.toString(), ": ");
}
}
/**
* The cause AWS reports for a failed JSONata expression: the error code, then the message
* jsonata-js renders for it, as in {@code T0412: Argument 1 of function "sum" must be an array
* of "numbers"}. AWS puts a sentence naming the expression and the field in front of that,
* which needs the field path {@link #resolveTemplate} does not thread today (#2665).
*
* <p>The dashjoin port renders the message itself, and three things go wrong on the way. Its
* copy of the catalog has the word "function" replaced by "Object" throughout; its substituter
* fills the first two placeholders of a template and leaves a third one standing; and it quotes
* every value it inserts, where jsonata-js JSON-renders it. So the message is composed here
* from the code and the values {@link JException} carries instead.
* How deeply nested the evaluation step running is, the expression's own root node being 1.
* A step that fails never leaves, so once an exception has unwound the count is the depth of
* the step that raised it.
*/
private String queryEvaluationCause(Exception e) {
private static final class ExpressionNesting {
private int depth;
/**
* Counts on the same pair of callbacks the library installs its bounds through, keeping
* the library's own and running after it.
*/
void trackOn(Jsonata.Frame frame) {
Jsonata.EntryCallback boundsOnEntry = (Jsonata.EntryCallback) frame.lookup("__evaluate_entry");
frame.setEvaluateEntryCallback((expression, input, environment) -> {
boundsOnEntry.callback(expression, input, environment);
depth++;
});
afterEachEvaluationStep(frame, (expression, input, environment, result) -> depth--);
}
/** Whether the step that failed is the expression itself rather than a step inside it. */
boolean isTheWholeExpression() {
return depth == 1;
}
}
/**
* The value behind a D1001, which the library raises rather than carrying a non-finite number:
* {@code 1/0} and {@code 1e308*10} both fail there where AWS answers {@code "Infinity"}. The
* exception is the only place the library leaves the value, and it carries no way back to where
* the arithmetic ran, so this recovers the value only when the step that raised it is the whole
* expression. {@code [1/0]} fails the state instead of answering AWS's {@code ["Infinity"]},
* which is the conservative half of the divergence: a state that fails is one a Catch fires on.
*/
private JsonNode nonFiniteRefusal(Exception e) {
if (e instanceof JException jsonataError && "D1001".equals(jsonataError.getError())
&& jsonataError.getCurrent() instanceof Number value) {
return fromJsonataValue(value.doubleValue());
}
return null;
}
/**
* Adds the memory bound to the time and depth bounds the library installs. The exit callback
* carries the value every evaluation step produces, which is what the bound is on.
*/
private static void boundHeldMemory(Jsonata.Frame frame) {
afterEachEvaluationStep(frame, (expression, input, environment, result) -> {
if (heldBytes(result, 0) > MAX_EXPRESSION_BYTES) {
throw new JException(MEMORY_LIMIT_EXCEEDED, -1);
}
});
}
/**
* Runs {@code addition} after every evaluation step, keeping whatever the frame already runs
* there. The library installs its time and depth bounds through that same callback, so an
* addition extends them rather than replacing them.
*/
private static void afterEachEvaluationStep(Jsonata.Frame frame, Jsonata.ExitCallback addition) {
Jsonata.ExitCallback installed = (Jsonata.ExitCallback) frame.lookup("__evaluate_exit");
frame.setEvaluateExitCallback((expression, input, environment, result) -> {
installed.callback(expression, input, environment, result);
addition.callback(expression, input, environment, result);
});
}
/**
* What {@code value} adds to the {@code held} bytes already counted, stopping as soon as the
* total passes {@link #MAX_EXPRESSION_BYTES} so a value far over the bound costs no more to
* refuse than one just over it. A range is counted from its length: the library materialises it
* lazily, and AWS refuses it on the elements it stands for rather than on what it has built.
*/
private static long heldBytes(Object value, long held) {
if (held > MAX_EXPRESSION_BYTES) {
return held;
}
if (value instanceof String text) {
return held + text.length();
}
if (value instanceof Utils.RangeList range) {
return held + (long) range.size() * BYTES_PER_SCALAR;
}
if (value instanceof List<?> list) {
return elementsHeldBytes(list, held);
}
if (value instanceof Map<?, ?> map) {
return elementsHeldBytes(map.entrySet(), held);
}
if (value instanceof Map.Entry<?, ?> entry) {
return heldBytes(entry.getValue(), heldBytes(entry.getKey(), held));
}
return held + BYTES_PER_SCALAR;
}
private static long elementsHeldBytes(Iterable<?> elements, long held) {
long total = held;
for (Object element : elements) {
total = heldBytes(element, total);
if (total > MAX_EXPRESSION_BYTES) {
return total;
}
}
return total;
}
/**
* The state failure AWS reports for an expression that threw. Its cause is the error code and
* the message jsonata-js renders for it, as in {@code T0412: Argument 1 of function "sum" must
* be an array of "numbers"}; for the handful of failures with no JSONata code of their own (a
* timeout, a stack-depth bound) it is the bare message the exception came with.
*
* <p>The dashjoin port renders the coded message itself, and three things go wrong on the way.
* Its copy of the catalog has the word "function" replaced by "Object" throughout; its
* substituter fills the first two placeholders of a template and leaves a third one standing;
* and it quotes every value it inserts, where jsonata-js JSON-renders it. So the message is
* composed here from the code and the values {@link JException} carries instead.
*/
private QueryEvaluationFailure queryEvaluationFailure(Exception e) {
if (!(e instanceof JException jsonataError) || jsonataError.getError() == null) {
return e.getMessage();
return new QueryEvaluationFailure(e.getMessage(), ": ");
}
String code = jsonataError.getError();
Object current = jsonataError.getCurrent();
Object expected = jsonataError.getExpected();
// Both this class's own functions and a few library call sites throw with the whole
// message in the code slot. The catalog lookup then misses and dashjoin prefixes its own
// class name, which AWS never emits; the message is the code slot itself.
// class name, which AWS never emits; the message is the code slot itself. Most of these
// already open with a real code ("D3137: Invalid JSON"), a sentence of its own that joins
// AWS's with ". "; the two bound messages (a timeout, a stack-depth or memory ceiling) open
// with English instead and are bare, joining with ": " like any other uncoded message.
if ((UNKNOWN_CODE_PREFIX + code).equals(jsonataError.getMessage())) {
return code;
String bound = withoutDepthBoundSuffix(code);
String separator = bound.matches("[A-Z]\\d{4}:.*") ? ". " : ": ";
return new QueryEvaluationFailure(bound, separator);
}
return switch (code) {
String tail = switch (code) {
// The only two templates in the catalog with a third placeholder. Neither third value
// reaches the exception: T0412 carries the offending argument and the element type it
// wanted but not the argument index nor the function name, and T2009 carries the two
@@ -230,6 +408,32 @@ public class JsonataEvaluator {
+ " either side of the operator must be of the same data type";
default -> code + ": " + renderTemplate(code, current, expected);
};
return new QueryEvaluationFailure(tail, ". ");
}
/**
* The library's stack-depth bound names the depth and the ceiling it hit,
* {@code ". Depth=101 max=100"}, which AWS's own sentence for the same failure never carries.
* Every other bare message this method renders has no such suffix, so stripping it here is
* specific to that one failure without needing to name it.
*/
private static String withoutDepthBoundSuffix(String message) {
return message.replaceFirst("\\. Depth=\\d+ max=\\d+$", "");
}
/**
* A States.QueryEvaluationError the expression itself raised, carrying the punctuation that
* joins its cause to the sentence {@link #evaluateField} puts in front: {@code ". "} after a
* coded JSONata message, which is a sentence of its own, and {@code ": "} before a bare one.
*/
private static final class QueryEvaluationFailure extends FailStateException {
final String sentenceSeparator;
QueryEvaluationFailure(String cause, String sentenceSeparator) {
super("States.QueryEvaluationError", cause);
this.sentenceSeparator = sentenceSeparator;
}
}
/**
@@ -256,19 +460,29 @@ public class JsonataEvaluator {
}
/**
* 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.
* Evaluate the single expression an ASL field holds, failing the state when it returns nothing
* or when the expression itself throws. AWS names the expression and the field in the cause
* either way and a {@code Catch} on States.QueryEvaluationError fires: a Wait whose Seconds
* returned nothing does not wait zero seconds, it fails, and {@code $abs("x")} in a field fails
* it with AWS's "threw an error during evaluation" sentence in front of the JSONata message.
*
* <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);
String expr = isExpression(expression) ? unwrap(expression) : expression;
JsonNode value;
try {
value = evaluate(expression, statesVar, variables);
} catch (QueryEvaluationFailure failure) {
throw new FailStateException(failure.error,
"The JSONata expression '" + expr + "' specified for the field '" + field
+ "' threw an error during evaluation" + failure.sentenceSeparator + failure.cause);
}
if (value.isMissingNode()) {
throw new FailStateException("States.QueryEvaluationError",
"The JSONata expression '" + (isExpression(expression) ? unwrap(expression) : expression)
+ "' specified for the field '" + field + "' returned nothing (undefined).");
"The JSONata expression '" + expr + "' specified for the field '" + field
+ "' returned nothing (undefined).");
}
return value;
}
@@ -356,16 +570,31 @@ public class JsonataEvaluator {
list.forEach(element -> array.add(fromJsonataValue(element)));
return array;
}
// JSON has no literal for a non-finite number, so AWS writes each one as the string
// JavaScript names it by: 1/0 comes back as "Infinity" and $parseInteger("abc","0") as
// "NaN".
if (value instanceof Double number && !Double.isFinite(number)) {
return TextNode.valueOf(nonFiniteName(number));
}
return objectMapper.valueToTree(value);
}
private static String nonFiniteName(double value) {
if (Double.isNaN(value)) {
return "NaN";
}
return value > 0 ? "Infinity" : "-Infinity";
}
/**
* The JSONata functions Floci binds on the evaluation frame. Six of them are what the Step
* Functions dialect adds on top of the JSONata language, per
* https://docs.aws.amazon.com/step-functions/latest/dg/transforming-data.html: five the
* dashjoin library does not have at all, and $random it has with the wrong arity, so the
* binding shadows it to accept AWS's optional seed. The seventh, $string, is a JSONata
* function the library has but writes with a different number notation than AWS.
* binding shadows it to accept AWS's optional seed. The other three are JSONata functions the
* library has and answers differently from AWS: $string writes a number in another notation,
* $parseInteger answers nothing where AWS answers NaN, and $formatNumber accepts picture
* strings AWS refuses.
*
* <p>Each of the six declares one optional slot more than the arity AWS documents. The library
* pads a short call with Java nulls up to the declared arity and refuses a call longer than
@@ -385,7 +614,42 @@ public class JsonataEvaluator {
"hash", new Jsonata.JFunction((input, arguments) -> hash(arguments), "<x?x?x?:x>"),
"random", new Jsonata.JFunction((input, arguments) -> random(arguments), "<x?x?:x>"),
"uuid", new Jsonata.JFunction((input, arguments) -> uuid(arguments), "<x?:x>"),
"string", new Jsonata.JFunction((input, arguments) -> string(arguments), "<x-b?:s>"));
"string", new Jsonata.JFunction((input, arguments) -> string(arguments), "<x-b?:s>"),
"parseInteger", new Jsonata.JFunction((input, arguments) -> parseInteger(arguments), "<s-s:n>"),
"formatNumber", new Jsonata.JFunction((input, arguments) -> formatNumber(arguments), "<n-so?:s>"));
}
/**
* $parseInteger(value, picture): the JSONata function, answering NaN where the picture cannot
* read the value, which is what AWS answers. The library answers nothing there, and an ASL
* field that evaluates to nothing fails the state.
*/
private static Object parseInteger(List<Object> arguments) {
Object value = argument(arguments, 0);
if (value == null) {
return null;
}
try {
Number parsed = Functions.parseInteger((String) value, (String) argument(arguments, 1));
return parsed == null ? Double.NaN : parsed;
} catch (ParseException e) {
throw new JException(e.getMessage(), -1);
}
}
/**
* $formatNumber(value, picture, options): the JSONata function, with the picture string checked
* against the rules AWS applies to it first. See {@link FormatNumberPicture}.
*/
private static Object formatNumber(List<Object> arguments) {
Object value = argument(arguments, 0);
if (value == null) {
return null;
}
Map<?, ?> options = (Map<?, ?>) argument(arguments, 2);
String picture = (String) argument(arguments, 1);
FormatNumberPicture.validate(picture, options);
return Functions.formatNumber((Number) value, picture, options);
}
/**
@@ -607,6 +871,9 @@ public class JsonataEvaluator {
.subtract(BigInteger.valueOf(start))
.divide(BigInteger.valueOf(step))
.add(BigInteger.ONE);
if (elements.compareTo(BigInteger.valueOf(MAX_RANGE_ELEMENTS)) > 0) {
throw new JException(MEMORY_LIMIT_EXCEEDED, -1);
}
List<Object> values = new ArrayList<>();
long value = start;
for (BigInteger emitted = BigInteger.ZERO;