mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(stepfunctions): keep a JSONata expression's explicit null in the output (#2684)
* fix(stepfunctions): keep a JSONata expression's explicit null in the output
A JSONata expression evaluating to JSON null lost its key, exactly as an
expression that returned nothing did. Real AWS answers {"v":null} to
Output {"v":"{% null %}"} and keeps the null in a nested object, in an
array, in Assign and in a Task's Arguments.
dashjoin flattens JSONata's null marker into a Java null unless
outputConvertNulls is off, which made the two indistinguishable inside the
evaluator. With it off, $states and the workflow variables bind through
toJsonataValue so an input null stays a null the expression can see, the
result is walked back with the marker mapped to a JSON null and a Java null
mapped to a missing node, and resolveTemplate drops only the missing node.
Closes #2664
* fix(stepfunctions): resolve a whole JSONata field that returned nothing to a null
A MissingNode is how the evaluator says "this expression returned nothing", and
resolveTemplate handed it straight to the executor when the whole Output or
Arguments field was that one expression. exec.setOutput writes the state output
with toString(), and a MissingNode writes itself as the empty string, so
DescribeExecution answered "output": "" instead of JSON.
resolveTemplate is now the boundary and answers a JSON value: a whole field that
returned nothing is a null, which is what it was before the marker existed. The
recursion moves to the private resolveTemplateNode, where "returned nothing" is
still itself in the two positions that read it, an object field it drops and an
array element it fails the state on.
AWS fails such a state with States.QueryEvaluationError; that is #2665 and stays
open.
* test(stepfunctions): read $string() with no argument as nothing, not as null
* docs(stepfunctions): mark the key dropped by an expression that returned nothing as a deviation
---------
Co-authored-by: Facundo Paredes <25681464+facuparedes@users.noreply.github.com>
This commit is contained in:
1 parent
395401807b
commit
48e0ecd809
5 files changed
+310
-25
No files matched your search
+41
@@ -284,6 +284,47 @@ class AslExecutorHttpInvokeTest {
|
||||
assertTrue(body.path("active").asBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonataArgumentsSendAnExplicitNullAndOmitOnlyWhatReturnedNothing() throws Exception {
|
||||
// AWS keeps the null in the arguments a Task is invoked with: TestState TRACE reports
|
||||
// afterArguments {"FunctionName":"nope","Payload":{"v":null}} for Payload {"v":"{% null %}"}.
|
||||
Execution execution = run("""
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "CallHttp",
|
||||
"States": {
|
||||
"CallHttp": {
|
||||
"Type": "Task",
|
||||
"Resource": "arn:aws:states:::http:invoke",
|
||||
"Arguments": {
|
||||
"ApiEndpoint": "%s/text",
|
||||
"Method": "POST",
|
||||
"Authentication": {
|
||||
"ConnectionArn": "%s"
|
||||
},
|
||||
"RequestBody": {
|
||||
"fromInput": "{%% $lookup($states.input, 'customerId') %%}",
|
||||
"returnedNothing": "{%% $states.input.absent %%}",
|
||||
"active": true
|
||||
}
|
||||
},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""".formatted(baseUrl, CONNECTION_ARN), """
|
||||
{
|
||||
"customerId": null
|
||||
}
|
||||
""");
|
||||
|
||||
assertEquals("SUCCEEDED", execution.getStatus(), execution.getCause());
|
||||
JsonNode body = objectMapper.readTree(onlyRequest().body());
|
||||
assertTrue(body.path("fromInput").isNull(), body.toString());
|
||||
assertTrue(body.path("returnedNothing").isMissingNode(), body.toString());
|
||||
assertTrue(body.path("active").asBoolean());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonSuccessStatusFailsWithStatesHttpStatusCodeError() throws Exception {
|
||||
Execution execution = run("""
|
||||
|
||||
+97
-12
@@ -85,12 +85,12 @@ class JsonataEvaluatorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void evaluate_returnsNullForMissingField() throws Exception {
|
||||
void evaluate_returnsNothingForMissingField() throws Exception {
|
||||
JsonNode statesVar = objectMapper.readTree("""
|
||||
{"input": {"name": "Alice"}}
|
||||
""");
|
||||
JsonNode result = evaluator.evaluate("{% $states.input.missing %}", statesVar);
|
||||
assertTrue(result.isNull());
|
||||
assertTrue(result.isMissingNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -216,7 +216,7 @@ class JsonataEvaluatorTest {
|
||||
void parse_noArgumentReturnsUndefined() {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
JsonNode result = evaluator.evaluate("{% $parse() %}", statesVar);
|
||||
assertTrue(result.isNull());
|
||||
assertTrue(result.isMissingNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -260,14 +260,14 @@ class JsonataEvaluatorTest {
|
||||
void partition_emptyArrayReturnsUndefined() {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
JsonNode result = evaluator.evaluate("{% $partition([], 2) %}", statesVar);
|
||||
assertTrue(result.isNull());
|
||||
assertTrue(result.isMissingNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void partition_chunkSizeZeroReturnsUndefined() {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
JsonNode result = evaluator.evaluate("{% $partition([1,2,3], 0) %}", statesVar);
|
||||
assertTrue(result.isNull());
|
||||
assertTrue(result.isMissingNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -319,21 +319,21 @@ class JsonataEvaluatorTest {
|
||||
void range_wrongSignStepReturnsUndefined() {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
JsonNode result = evaluator.evaluate("{% $range(5, 1, 1) %}", statesVar);
|
||||
assertTrue(result.isNull());
|
||||
assertTrue(result.isMissingNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void range_stepZeroReturnsUndefined() {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
JsonNode result = evaluator.evaluate("{% $range(1, 5, 0) %}", statesVar);
|
||||
assertTrue(result.isNull());
|
||||
assertTrue(result.isMissingNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void range_missingStepReturnsUndefined() {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
JsonNode result = evaluator.evaluate("{% $range(1, 5) %}", statesVar);
|
||||
assertTrue(result.isNull());
|
||||
assertTrue(result.isMissingNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -375,7 +375,7 @@ class JsonataEvaluatorTest {
|
||||
void hash_missingAlgorithmReturnsUndefined() {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
JsonNode result = evaluator.evaluate("{% $hash('Hello') %}", statesVar);
|
||||
assertTrue(result.isNull());
|
||||
assertTrue(result.isMissingNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -450,7 +450,7 @@ class JsonataEvaluatorTest {
|
||||
assertEquals("[-2,-1,0]", evaluator.evaluate("{% $range(-2.9, 0, 1) %}", statesVar).toString());
|
||||
assertEquals("[5,4,3,2,1]", evaluator.evaluate("{% $range(5, 1, -1.5) %}", statesVar).toString());
|
||||
// -0.5 rounds to 0, which is undefined, while -2 stays negative and is an error.
|
||||
assertTrue(evaluator.evaluate("{% $partition([1,2,3,4], -0.5) %}", statesVar).isNull());
|
||||
assertTrue(evaluator.evaluate("{% $partition([1,2,3,4], -0.5) %}", statesVar).isMissingNode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -480,7 +480,7 @@ class JsonataEvaluatorTest {
|
||||
@Test
|
||||
void hash_missingAlgorithmIsUndefinedButNonStringOneIsASignatureError() {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
assertTrue(evaluator.evaluate("{% $hash('a') %}", statesVar).isNull());
|
||||
assertTrue(evaluator.evaluate("{% $hash('a') %}", statesVar).isMissingNode());
|
||||
AslExecutor.FailStateException ex = assertThrows(AslExecutor.FailStateException.class,
|
||||
() -> evaluator.evaluate("{% $hash('a', 5) %}", statesVar));
|
||||
assertTrue(ex.cause.contains("T0410"));
|
||||
@@ -683,7 +683,9 @@ class JsonataEvaluatorTest {
|
||||
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());
|
||||
// $string() with no argument returns nothing, which this change stops reporting as an
|
||||
// explicit null. AWS fails the state on it, which is #2665's subject and not this one's.
|
||||
assertTrue(evaluator.evaluate("{% $string() %}", statesVar).isMissingNode());
|
||||
assertEquals("[\"100000000000000000000\",\"1e+21\"]",
|
||||
evaluator.evaluate("{% $map([1e20, 1e21], $string) %}", statesVar).toString());
|
||||
}
|
||||
@@ -800,4 +802,87 @@ class JsonataEvaluatorTest {
|
||||
assertEquals("States.QueryEvaluationError", ex.error);
|
||||
assertTrue(ex.cause.contains("nope"), ex.cause);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExplicitNullKeepsItsKeyAndOnlyWhatReturnedNothingIsDropped() throws Exception {
|
||||
JsonNode statesVar = objectMapper.readTree("""
|
||||
{"input": {"bar": null}}
|
||||
""");
|
||||
JsonNode template = objectMapper.readTree("""
|
||||
{"fromInput": "{% $lookup($states.input, 'bar') %}",
|
||||
"returnedNothing": "{% $states.input.absent %}",
|
||||
"kept": 1}
|
||||
""");
|
||||
|
||||
assertEquals("{\"fromInput\":null,\"kept\":1}",
|
||||
evaluator.resolveTemplate(template, statesVar).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExplicitNullSurvivesInsideANestedObject() throws Exception {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
JsonNode template = objectMapper.readTree("""
|
||||
{"outer": {"fromExpression": "{% null %}", "kept": 1}}
|
||||
""");
|
||||
|
||||
assertEquals("{\"outer\":{\"fromExpression\":null,\"kept\":1}}",
|
||||
evaluator.resolveTemplate(template, statesVar).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWholeTemplateIsANullWhetherItEvaluatedToNullOrReturnedNothing() throws Exception {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
|
||||
assertTrue(evaluator.resolveTemplate(objectMapper.readTree("\"{% null %}\""), statesVar).isNull());
|
||||
// AWS fails the second one with States.QueryEvaluationError, which #2665 covers. Until then
|
||||
// it is a null and not a missing node: an Output or Arguments is serialized as it stands,
|
||||
// and a missing node writes itself as the empty string rather than as JSON.
|
||||
assertTrue(evaluator.resolveTemplate(objectMapper.readTree("\"{% $states.input.absent %}\""), statesVar)
|
||||
.isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExplicitNullIsAnArrayElementWhileNothingStillFailsTheState() throws Exception {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
|
||||
assertEquals("{\"values\":[null,1]}", evaluator.resolveTemplate(objectMapper.readTree("""
|
||||
{"values": ["{% null %}", 1]}
|
||||
"""), statesVar).toString());
|
||||
|
||||
AslExecutor.FailStateException failure = assertThrows(AslExecutor.FailStateException.class,
|
||||
() -> evaluator.resolveTemplate(objectMapper.readTree("""
|
||||
{"values": ["{% $states.input.absent %}"]}
|
||||
"""), statesVar));
|
||||
assertTrue(failure.cause.contains("returned nothing (undefined)"), failure.cause);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLiteralNullInTheTemplateKeepsItsKey() throws Exception {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
|
||||
assertEquals("{\"literal\":null,\"kept\":1}", evaluator.resolveTemplate(objectMapper.readTree("""
|
||||
{"literal": null, "kept": 1}
|
||||
"""), statesVar).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anInputNullIsAValueForExistsAndType() throws Exception {
|
||||
JsonNode statesVar = objectMapper.readTree("""
|
||||
{"input": {"bar": null}}
|
||||
""");
|
||||
|
||||
assertTrue(evaluator.evaluate("{% $exists($states.input.bar) %}", statesVar).asBoolean());
|
||||
assertEquals("null", evaluator.evaluate("{% $type($states.input.bar) %}", statesVar).asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAssignedNullReadsBackAsANull() throws Exception {
|
||||
JsonNode statesVar = objectMapper.createObjectNode();
|
||||
JsonNode variables = objectMapper.readTree("""
|
||||
{"nullVariable": null}
|
||||
""");
|
||||
|
||||
assertTrue(evaluator.evaluate("{% $nullVariable %}", statesVar, variables).isNull());
|
||||
assertTrue(evaluator.evaluate("{% $exists($nullVariable) %}", statesVar, variables).asBoolean());
|
||||
}
|
||||
}
|
||||
+98
@@ -1225,6 +1225,104 @@ class StepFunctionsJsonataIntegrationTest {
|
||||
assertTrue(output.contains("Jane Doe"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void outputKeepsAnExplicitNullInEveryPositionAndDropsOnlyWhatReturnedNothing() throws Exception {
|
||||
// AWS returns {"v":null} for an expression that evaluates to JSON null, in an object field,
|
||||
// nested, as an array element and as a literal. Only an expression that returned nothing
|
||||
// loses its key.
|
||||
String definition = """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "Transform",
|
||||
"States": {
|
||||
"Transform": {
|
||||
"Type": "Pass",
|
||||
"Output": {
|
||||
"fromInput": "{% $lookup($states.input, 'bar') %}",
|
||||
"fromLiteralExpression": "{% null %}",
|
||||
"literal": null,
|
||||
"nested": {"inner": "{% null %}", "kept": 1},
|
||||
"values": ["{% null %}", 1],
|
||||
"returnedNothing": "{% $states.input.absent %}"
|
||||
},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
String smArn = createStateMachine("jsonata-explicit-null-test", definition);
|
||||
String execArn = startExecution(smArn, "{\"bar\": null}");
|
||||
JsonNode output = objectMapper.readTree(waitForExecution(execArn));
|
||||
|
||||
assertTrue(output.path("fromInput").isNull(), output.toString());
|
||||
assertTrue(output.path("fromLiteralExpression").isNull(), output.toString());
|
||||
assertTrue(output.path("literal").isNull(), output.toString());
|
||||
assertTrue(output.path("nested").path("inner").isNull(), output.toString());
|
||||
assertEquals("[null,1]", output.path("values").toString());
|
||||
assertTrue(output.path("returnedNothing").isMissingNode(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anAssignedNullReadsBackAsANullInTheNextState() throws Exception {
|
||||
// AWS keeps the null in the variables map: TestState TRACE reports {"x":null} for
|
||||
// Assign {"x": "{% null %}"}. A state never sees its own Assign, so the read is one state on.
|
||||
String definition = """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "AssignNull",
|
||||
"States": {
|
||||
"AssignNull": {
|
||||
"Type": "Pass",
|
||||
"Assign": {"nullVariable": "{% $states.input.bar %}"},
|
||||
"Next": "ReadItBack"
|
||||
},
|
||||
"ReadItBack": {
|
||||
"Type": "Pass",
|
||||
"Output": {
|
||||
"fromVariable": "{% $nullVariable %}",
|
||||
"exists": "{% $exists($nullVariable) %}"
|
||||
},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
String smArn = createStateMachine("jsonata-assign-null-test", definition);
|
||||
String execArn = startExecution(smArn, "{\"bar\": null}");
|
||||
JsonNode output = objectMapper.readTree(waitForExecution(execArn));
|
||||
|
||||
assertTrue(output.path("fromVariable").isNull(), output.toString());
|
||||
assertTrue(output.path("exists").asBoolean(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWholeOutputThatReturnedNothingStaysReadableJson() throws Exception {
|
||||
// AWS fails this state with States.QueryEvaluationError, which #2665 covers. Until then the
|
||||
// execution succeeds, and what DescribeExecution hands back has to parse: an output field
|
||||
// holding the empty string is not JSON any client can read.
|
||||
String definition = """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "Transform",
|
||||
"States": {
|
||||
"Transform": {
|
||||
"Type": "Pass",
|
||||
"Output": "{% $states.input.absent %}",
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
String smArn = createStateMachine("jsonata-whole-output-nothing-test", definition);
|
||||
String execArn = startExecution(smArn, "{}");
|
||||
String output = waitForExecution(execArn);
|
||||
|
||||
assertTrue(objectMapper.readTree(output).isNull(), output);
|
||||
}
|
||||
|
||||
@Test
|
||||
void assignedVariablesSurviveBeyondNextStateOutput() throws Exception {
|
||||
// Variables set via Assign persist across states even after a later state replaces the output.
|
||||
|
||||
Reference in new issue
Block a user