diff --git a/docs/services/step-functions.md b/docs/services/step-functions.md index 3890a1bed7..285d0602b4 100644 --- a/docs/services/step-functions.md +++ b/docs/services/step-functions.md @@ -1082,9 +1082,12 @@ Floci refuses it too, from `CreateStateMachine`, `UpdateStateMachine` and `phone` does not put a bare `phone` in scope on AWS either. Only the first step of a path is read against the top-level context, so a name in a later step, -in a predicate, in a sort term or in an object grouping stays legal and -`$states.input.items[value > 3]` is accepted. A lambda body keeps the context of the expression -that defines it, so `$map($states.input.a, function($x){ b })` does name `b` at the top level. +in a predicate, in a sort term or in a grouping expression on that path stays legal: +`$states.input.items[value > 3]` and `$states.input.items{'k': value}` are accepted. A grouping +expression on a bare variable is different: `$p{$string(user_id): $max(id)}` names `user_id` and +`id` at the top level and is refused, as AWS refuses it. A lambda body keeps the context of the +expression that defines it, so `$map($states.input.a, function($x){ b })` does name `b` at the +top level. A JSONata expression that fails to parse, such as `{% a[1,2) %}`, is refused the same way, with `INVALID_JSONATA_EXPRESSION` and the parser's own message at the field's location. `$$`, the diff --git a/src/main/java/io/floci/aws/services/stepfunctions/JsonataTopLevelReferences.java b/src/main/java/io/floci/aws/services/stepfunctions/JsonataTopLevelReferences.java index d28ace06cd..3ed5ff2585 100644 --- a/src/main/java/io/floci/aws/services/stepfunctions/JsonataTopLevelReferences.java +++ b/src/main/java/io/floci/aws/services/stepfunctions/JsonataTopLevelReferences.java @@ -20,11 +20,13 @@ import static io.floci.aws.services.stepfunctions.JsonataSymbolReader.read; * {@code UNSUPPORTED_JSONATA_EXPRESSION: Reference to 'phone' at the top level is not supported.} * *

Only the first step of a path is evaluated against the top-level context. Every later step, - * every predicate, every sort term and every grouping expression runs against the value the step - * before it produced, so a name there is legal and this walk never descends into one. A lambda - * body, on the other hand, keeps the context of the expression that defines it, so - * {@code $map($states.input.a, function($x){ b })} does name {@code b} at the top level. Both - * halves are measured against AWS in {@code JsonataTopLevelReferencesTest}. + * every predicate, every sort term and every grouping expression on that path runs against the + * value the step before it produced, so a name there is legal and this walk never descends into + * one. A grouping expression on a bare variable is different: AWS reads its keys and values from + * the top-level context, so {@code $p{$string(user_id): $max(id)}} names {@code user_id} and + * {@code id}. A lambda body, on the other hand, keeps the context of the expression that defines + * it, so {@code $map($states.input.a, function($x){ b })} does name {@code b} at the top level. + * All of these are measured against AWS in {@code JsonataTopLevelReferencesTest}. * *

Nonpublic parse-tree fields are read through {@link JsonataSymbolReader}. */ @@ -89,7 +91,12 @@ final class JsonataTopLevelReferences { } } case "name" -> names.add(String.valueOf(read(node, "value"))); - case "variable" -> collectContextItem(node, names); + case "variable" -> { + collectContextItem(node, names); + if (node.group != null) { + collectEachPair(node.group.lhsObject, names); + } + } case "unary" -> collectUnary(node, names); case "binary", "apply" -> { collect(child(node, "lhs"), names); diff --git a/src/test/java/io/floci/aws/services/stepfunctions/JsonataTopLevelReferencesTest.java b/src/test/java/io/floci/aws/services/stepfunctions/JsonataTopLevelReferencesTest.java index 0f159a7f49..110c90f8bc 100644 --- a/src/test/java/io/floci/aws/services/stepfunctions/JsonataTopLevelReferencesTest.java +++ b/src/test/java/io/floci/aws/services/stepfunctions/JsonataTopLevelReferencesTest.java @@ -61,6 +61,7 @@ class JsonataTopLevelReferencesTest { "$states.input.items[phone > 3]", "$states.input.a^(phone)", "$states.input.a{'k': phone}", + "$states.input.a{$string(phone): $max(id)}", "$states.input.a.(phone)", "$states.input.a.$phone", "$phone", @@ -80,6 +81,23 @@ class JsonataTopLevelReferencesTest { assertEquals(List.of(), references(expression)); } + @ParameterizedTest + @ValueSource(strings = { + "$ids{$string($): true}", + "$ids[0]{$string($): true}", + "$ids{$string($): true}.x"}) + void namesTheContextItemInAGroupByOnAVariable(String expression) { + assertEquals(List.of("$"), references(expression)); + } + + @ParameterizedTest + @ValueSource(strings = { + "$p{$string(user_id): $max(id)}", + "($p := $states.input; $p{$string(user_id): $max(id)})"}) + void namesTheReferencesInAGroupByOnAVariable(String expression) { + assertEquals(List.of("user_id", "id"), references(expression)); + } + @ParameterizedTest @ValueSource(strings = {"$", "$.phone", "$[0]", "$string($)", "$map($states.input.a, function($x){ $ })"}) diff --git a/src/test/java/io/floci/aws/services/stepfunctions/StepFunctionsTopLevelReferenceIntegrationTest.java b/src/test/java/io/floci/aws/services/stepfunctions/StepFunctionsTopLevelReferenceIntegrationTest.java index 12a071c7e0..ba5c711723 100644 --- a/src/test/java/io/floci/aws/services/stepfunctions/StepFunctionsTopLevelReferenceIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/stepfunctions/StepFunctionsTopLevelReferenceIntegrationTest.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.floci.aws.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.Response; +import io.restassured.response.ValidatableResponse; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -49,15 +50,20 @@ class StepFunctionsTopLevelReferenceIntegrationTest { .when().post("/"); } - private static void expectTopLevelReference(String definition, String reference, String location) { - validate(definition).then().statusCode(200) + private static void expectTopLevelReference(String definition, String firstReference, + String location, String... moreReferences) { + ValidatableResponse response = validate(definition).then().statusCode(200) .body("result", equalTo("FAIL")) - .body("diagnostics", hasSize(1)) - .body("diagnostics[0].severity", equalTo("ERROR")) - .body("diagnostics[0].code", equalTo("UNSUPPORTED_JSONATA_EXPRESSION")) - .body("diagnostics[0].message", equalTo( - "Reference to '" + reference + "' at the top level is not supported.")) - .body("diagnostics[0].location", equalTo(location)); + .body("diagnostics", hasSize(1 + moreReferences.length)); + for (int index = 0; index <= moreReferences.length; index++) { + String reference = index == 0 ? firstReference : moreReferences[index - 1]; + String prefix = "diagnostics[" + index + "]."; + response.body(prefix + "severity", equalTo("ERROR")) + .body(prefix + "code", equalTo("UNSUPPORTED_JSONATA_EXPRESSION")) + .body(prefix + "message", equalTo( + "Reference to '" + reference + "' at the top level is not supported.")) + .body(prefix + "location", equalTo(location)); + } } private static void expectAccepted(String definition) { @@ -214,15 +220,8 @@ class StepFunctionsTopLevelReferenceIntegrationTest { @Test @DisplayName("each distinct name in one field gets its own diagnostic") void everyDistinctNameInOneField() { - validate(passWithOutput("{\"v\":\"{% aaa + bbb %}\"}")) - .then().statusCode(200) - .body("result", equalTo("FAIL")) - .body("diagnostics", hasSize(2)) - .body("diagnostics[0].message", equalTo( - "Reference to 'aaa' at the top level is not supported.")) - .body("diagnostics[1].message", equalTo( - "Reference to 'bbb' at the top level is not supported.")) - .body("diagnostics[1].location", equalTo("/States/E/Output/v")); + expectTopLevelReference(passWithOutput("{\"v\":\"{% aaa + bbb %}\"}"), + "aaa", "/States/E/Output/v", "bbb"); } @Test @@ -233,10 +232,34 @@ class StepFunctionsTopLevelReferenceIntegrationTest { } @Test - @DisplayName("a name reached through $states, a predicate or a variable is accepted") + void groupByOnAVariableNamesTheContextItem() { + expectTopLevelReference(passWithOutput("{\"v\":\"{% $ids{$string($): true} %}\"}"), + "$", "/States/E/Output/v"); + } + + @Test + void groupByOnAVariableNamesItsIdentifiers() { + expectTopLevelReference(passWithOutput("{\"v\":\"{% $p{$string(user_id): $max(id)} %}\"}"), + "user_id", "/States/E/Output/v", "id"); + } + + @Test + void createStateMachineRefusesAGroupByOnAVariable() { + create("group-by-on-variable-" + System.nanoTime(), + passWithOutput("{\"v\":\"{% $ids{$string($): true} %}\"}")) + .then().statusCode(400) + .body("__type", equalTo("InvalidDefinition")) + .body("message", equalTo("Invalid State Machine Definition: " + + "'UNSUPPORTED_JSONATA_EXPRESSION: Reference to '$' at the top level " + + "is not supported. at /States/E/Output/v'")); + } + + @Test + @DisplayName("a name reached through $states, a predicate, a variable or a path group-by is accepted") void anchoredExpressionsAreAccepted() { expectAccepted(passWithOutput("{\"v\":\"{% $states.input.phone %}\"}")); expectAccepted(passWithOutput("{\"v\":\"{% $states.input.items[phone > 3] %}\"}")); + expectAccepted(passWithOutput("{\"v\":\"{% $states.input.a{'k': phone} %}\"}")); expectAccepted(passWithOutput("{\"v\":\"{% $map($states.input.a, function($x){ $x.phone }) %}\"}")); expectAccepted(passWithOutput("{\"v\":\"{% $phone %}\"}")); }