fix(stepfunctions): write a large whole number in full in $string (#2683)

$string switched to exponent notation at Long.MAX_VALUE, so $string(1e20)
returned "1e+20" where AWS returns "100000000000000000000". AWS's boundary is
1e21, on both signs. The execution succeeds either way, so a $string used as a
MessageGroupId, an S3 key or a DynamoDB key wrote a different key on each engine
without either reporting an error.

$string is now bound on the evaluation frame next to the six functions of the
Step Functions dialect, with the library's own signature so every arity and type
error stays what it was. Below the boundary a double is replaced by the exact
integer of the shortest decimal that reads back as it, which is the digit string
AWS prints; the library formats everything else.

Closes #2669

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-28 19:04:44 -04:00
1 parent e4c690351f
commit 2a7ee7a605
4 files changed
+164 -9

No files matched your search

@@ -549,4 +549,73 @@ class JsonataEvaluatorTest {
JsonNode result = evaluator.evaluate("{% $map(['{\"a\":1}', '{\"a\":2}'], $parse).a %}", statesVar);
assertEquals("[1,2]", result.toString());
}
@Test
void string_writesAWholeNumberInFullBelowTheExponentBoundary() {
// dashjoin stops writing digits at Long.MAX_VALUE, 9.223372036854776E18, and prints
// exponent notation from there. AWS keeps writing them until 1e21.
JsonNode statesVar = objectMapper.createObjectNode();
assertEquals("10000000000000000000", evaluator.evaluate("{% $string(1e19) %}", statesVar).asText());
assertEquals("100000000000000000000", evaluator.evaluate("{% $string(1e20) %}", statesVar).asText());
assertEquals("150000000000000000000", evaluator.evaluate("{% $string(1.5e20) %}", statesVar).asText());
// The largest double under the boundary.
assertEquals("999999999999999900000",
evaluator.evaluate("{% $string(999999999999999868928) %}", statesVar).asText());
}
@Test
void string_switchesToExponentNotationAt1e21() {
JsonNode statesVar = objectMapper.createObjectNode();
assertEquals("1e+21", evaluator.evaluate("{% $string(1e21) %}", statesVar).asText());
// 1e21 - 1 is not a double, so it is the same number as 1e21 and prints the same way.
assertEquals("1e+21", evaluator.evaluate("{% $string(1e21 - 1) %}", statesVar).asText());
assertEquals("1e+22", evaluator.evaluate("{% $string(1e22) %}", statesVar).asText());
assertEquals("1e+100", evaluator.evaluate("{% $string(1e100) %}", statesVar).asText());
}
@Test
void string_flipsAtTheSameBoundaryOnANegativeNumber() {
JsonNode statesVar = objectMapper.createObjectNode();
assertEquals("-100000000000000000000", evaluator.evaluate("{% $string(-1e20) %}", statesVar).asText());
assertEquals("-150000000000000000000", evaluator.evaluate("{% $string(-1.5e20) %}", statesVar).asText());
assertEquals("-1e+21", evaluator.evaluate("{% $string(-1e21) %}", statesVar).asText());
}
@Test
void string_writesTheShortestDigitsThatReadBackAsTheSameDouble() {
// The two sides of $parse's number model print differently across the long boundary: the
// long prints its exact value, the double prints the shortest decimal that reads back as
// itself, so 2^63 is 9223372036854776000 and not its exact 9223372036854775808.
JsonNode statesVar = objectMapper.createObjectNode();
assertEquals("9223372036854775807",
evaluator.evaluate("{% $string($parse('{\"n\":9223372036854775807}').n) %}", statesVar).asText());
assertEquals("9223372036854776000",
evaluator.evaluate("{% $string($parse('{\"n\":9223372036854775808}').n) %}", statesVar).asText());
}
@Test
void string_writesANumberInsideAnObjectOrAnArrayTheSameWay() {
JsonNode statesVar = objectMapper.createObjectNode();
assertEquals("{\"n\":100000000000000000000}",
evaluator.evaluate("{% $string({'n': 1e20}) %}", statesVar).asText());
assertEquals("[100000000000000000000,2]",
evaluator.evaluate("{% $string([1e20, 2]) %}", statesVar).asText());
}
@Test
void string_leavesEveryOtherValueToTheBuiltIn() {
JsonNode statesVar = objectMapper.createObjectNode();
// AWS switches to exponent notation on the small side too, under 1e-6, and keeps fifteen
// significant digits on a fractional number. Both already match.
assertEquals("0.000001", evaluator.evaluate("{% $string(1e-6) %}", statesVar).asText());
assertEquals("1e-7", evaluator.evaluate("{% $string(1e-7) %}", statesVar).asText());
assertEquals("0.333333333333333", evaluator.evaluate("{% $string(1/3) %}", statesVar).asText());
assertEquals("true", evaluator.evaluate("{% $string(true) %}", statesVar).asText());
assertEquals("abc", evaluator.evaluate("{% $string('abc') %}", statesVar).asText());
assertEquals("null", evaluator.evaluate("{% $string(null) %}", statesVar).asText());
assertEquals("{\n \"a\": 1\n}", evaluator.evaluate("{% $string({'a': 1}, true) %}", statesVar).asText());
assertTrue(evaluator.evaluate("{% $string() %}", statesVar).isNull());
assertEquals("[\"100000000000000000000\",\"1e+21\"]",
evaluator.evaluate("{% $map([1e20, 1e21], $string) %}", statesVar).toString());
}
}
@@ -52,6 +52,34 @@ class StepFunctionsJsonataIntegrationTest {
assertTrue(output.contains("42"));
}
@Test
void passStateWithJsonataStringOfALargeWholeNumber() throws Exception {
// $string of an id feeds a MessageGroupId, an S3 key or a DynamoDB key, so exponent
// notation writes a different key without failing the execution.
String definition = """
{
"QueryLanguage": "JSONata",
"StartAt": "Stringify",
"States": {
"Stringify": {
"Type": "Pass",
"Output": {
"underTheBoundary": "{% $string(1e20) %}",
"atTheBoundary": "{% $string(1e21) %}"
},
"End": true
}
}
}
""";
String smArn = createStateMachine("jsonata-string-large-whole-number", definition);
JsonNode output = objectMapper.readTree(waitForExecution(startExecution(smArn, "{}")));
assertEquals("100000000000000000000", output.get("underTheBoundary").asText());
assertEquals("1e+21", output.get("atTheBoundary").asText());
}
@Test
void choiceStateWithJsonataCondition() throws Exception {
// Choice state using JSONata Condition instead of Variable/StringEquals