mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(sfn): JSONata improvements, States.* intrinsics, DynamoDB ConditionExpression, StartSyncExecution (#205)
* feat(sfn): add StartSyncExecution and fix per-state QueryLanguage override - Remove incorrect validation that blocked per-state QueryLanguage: JSONPath overrides when the top-level machine uses JSONata — AWS allows this - Implement StartSyncExecution for EXPRESS state machines: extract doExecute from executeAsync, add executeSync that blocks until the execution finishes, wire up handler and service method - Document remaining DynamoDB output shape issues (singleton unwrap, getItem key format) in issues/ for follow-up
This commit is contained in:
1 parent
858775c38e
commit
53dd7a68dd
10 files changed
+814
-147
No files matched your search
+75
-40
@@ -32,25 +32,25 @@ class VtlTemplateEngineTest {
|
||||
|
||||
@Test
|
||||
void passthrough_nullTemplate() {
|
||||
String result = engine.evaluate(null, ctx("{\"key\":\"value\"}"));
|
||||
String result = engine.evaluate(null, ctx("{\"key\":\"value\"}")).body();
|
||||
assertEquals("{\"key\":\"value\"}", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void passthrough_emptyTemplate() {
|
||||
String result = engine.evaluate("", ctx("{\"key\":\"value\"}"));
|
||||
String result = engine.evaluate("", ctx("{\"key\":\"value\"}")).body();
|
||||
assertEquals("{\"key\":\"value\"}", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputBody() {
|
||||
String result = engine.evaluate("$input.body()", ctx("{\"key\":\"value\"}"));
|
||||
String result = engine.evaluate("$input.body()", ctx("{\"key\":\"value\"}")).body();
|
||||
assertEquals("{\"key\":\"value\"}", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputJson_root() {
|
||||
String result = engine.evaluate("$input.json('$')", ctx("{\"name\":\"Alice\",\"age\":30}"));
|
||||
String result = engine.evaluate("$input.json('$')", ctx("{\"name\":\"Alice\",\"age\":30}")).body();
|
||||
// Should return the full body as JSON
|
||||
assertTrue(result.contains("Alice"));
|
||||
assertTrue(result.contains("30"));
|
||||
@@ -58,38 +58,38 @@ class VtlTemplateEngineTest {
|
||||
|
||||
@Test
|
||||
void inputJson_nested() {
|
||||
String result = engine.evaluate("$input.json('$.name')", ctx("{\"name\":\"Bob\"}"));
|
||||
String result = engine.evaluate("$input.json('$.name')", ctx("{\"name\":\"Bob\"}")).body();
|
||||
assertEquals("\"Bob\"", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputPath() {
|
||||
String result = engine.evaluate("$input.path('$.name')", ctx("{\"name\":\"Carol\"}"));
|
||||
String result = engine.evaluate("$input.path('$.name')", ctx("{\"name\":\"Carol\"}")).body();
|
||||
assertEquals("Carol", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputParams() {
|
||||
String result = engine.evaluate("$input.params().querystring.limit", ctx("{}"));
|
||||
String result = engine.evaluate("$input.params().querystring.limit", ctx("{}")).body();
|
||||
assertEquals("10", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void utilEscapeJavaScript_singleQuotes() {
|
||||
String body = "{\"msg\":\"hello 'world'\"}";
|
||||
String result = engine.evaluate("$util.escapeJavaScript($input.body())", ctx(body));
|
||||
String result = engine.evaluate("$util.escapeJavaScript($input.body())", ctx(body)).body();
|
||||
assertTrue(result.contains("\\'world\\'"), "Single quotes should be escaped: " + result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void utilEscapeJavaScript_doubleQuotes() {
|
||||
String result = engine.evaluate("$util.escapeJavaScript('he said \"hi\"')", ctx("{}"));
|
||||
String result = engine.evaluate("$util.escapeJavaScript('he said \"hi\"')", ctx("{}")).body();
|
||||
assertEquals("he said \\\"hi\\\"", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void utilEscapeJavaScript_forwardSlash() {
|
||||
String result = engine.evaluate("$util.escapeJavaScript('a/b/c')", ctx("{}"));
|
||||
String result = engine.evaluate("$util.escapeJavaScript('a/b/c')", ctx("{}")).body();
|
||||
assertEquals("a\\/b\\/c", result);
|
||||
}
|
||||
|
||||
@@ -124,19 +124,19 @@ class VtlTemplateEngineTest {
|
||||
|
||||
@Test
|
||||
void utilUrlEncodeDecode() {
|
||||
String result = engine.evaluate("$util.urlEncode('hello world')", ctx("{}"));
|
||||
String result = engine.evaluate("$util.urlEncode('hello world')", ctx("{}")).body();
|
||||
assertEquals("hello+world", result);
|
||||
|
||||
String decoded = engine.evaluate("$util.urlDecode('hello+world')", ctx("{}"));
|
||||
String decoded = engine.evaluate("$util.urlDecode('hello+world')", ctx("{}")).body();
|
||||
assertEquals("hello world", decoded);
|
||||
}
|
||||
|
||||
@Test
|
||||
void utilBase64EncodeDecode() {
|
||||
String encoded = engine.evaluate("$util.base64Encode('test data')", ctx("{}"));
|
||||
String encoded = engine.evaluate("$util.base64Encode('test data')", ctx("{}")).body();
|
||||
assertEquals("dGVzdCBkYXRh", encoded);
|
||||
|
||||
String decoded = engine.evaluate("$util.base64Decode('dGVzdCBkYXRh')", ctx("{}"));
|
||||
String decoded = engine.evaluate("$util.base64Decode('dGVzdCBkYXRh')", ctx("{}")).body();
|
||||
assertEquals("test data", decoded);
|
||||
}
|
||||
|
||||
@@ -144,13 +144,13 @@ class VtlTemplateEngineTest {
|
||||
void contextVariables() {
|
||||
String result = engine.evaluate(
|
||||
"$context.stage:$context.httpMethod:$context.resourcePath:$context.requestId",
|
||||
ctx("{}"));
|
||||
ctx("{}")).body();
|
||||
assertEquals("prod:POST:/users:req-123", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void contextIdentity() {
|
||||
String result = engine.evaluate("$context.identity.sourceIp", ctx("{}"));
|
||||
String result = engine.evaluate("$context.identity.sourceIp", ctx("{}")).body();
|
||||
assertEquals("127.0.0.1", result);
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ class VtlTemplateEngineTest {
|
||||
VtlTemplateEngine.VtlContext svCtx = new VtlTemplateEngine.VtlContext(
|
||||
"{}", Map.of(), Map.of(), Map.of(), "prod", "GET", "/",
|
||||
"req-1", "000000000000", Map.of("tableName", "my-table"));
|
||||
String result = engine.evaluate("$stageVariables.tableName", svCtx);
|
||||
String result = engine.evaluate("$stageVariables.tableName", svCtx).body();
|
||||
assertEquals("my-table", result);
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ class VtlTemplateEngineTest {
|
||||
String template = """
|
||||
{"stateMachineArn": "arn:aws:states:us-east-1:000:sm:test", "input": "$util.escapeJavaScript($input.json('$'))"}""";
|
||||
String body = "{\"id\":\"123\",\"message\":\"hello\"}";
|
||||
String result = engine.evaluate(template, ctx(body));
|
||||
String result = engine.evaluate(template, ctx(body)).body();
|
||||
|
||||
assertTrue(result.contains("arn:aws:states:us-east-1:000:sm:test"));
|
||||
assertTrue(result.contains("\\\"id\\\""));
|
||||
@@ -183,34 +183,34 @@ class VtlTemplateEngineTest {
|
||||
String template = """
|
||||
#set($body = $input.path('$'))
|
||||
{"value": "$body.name"}""";
|
||||
String result = engine.evaluate(template, ctx("{\"name\":\"Dave\"}")).trim();
|
||||
String result = engine.evaluate(template, ctx("{\"name\":\"Dave\"}")).body().trim();
|
||||
assertTrue(result.contains("Dave"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputJson_arrayIndex() {
|
||||
String body = "{\"items\":[{\"id\":\"first\"},{\"id\":\"second\"}]}";
|
||||
String result = engine.evaluate("$input.json('$.items[0].id')", ctx(body));
|
||||
String result = engine.evaluate("$input.json('$.items[0].id')", ctx(body)).body();
|
||||
assertEquals("\"first\"", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputJson_arrayIndexNested() {
|
||||
String body = "{\"data\":[[[\"deep\"]]]}";
|
||||
String result = engine.evaluate("$input.json('$.data[0][0][0]')", ctx(body));
|
||||
String result = engine.evaluate("$input.json('$.data[0][0][0]')", ctx(body)).body();
|
||||
assertEquals("\"deep\"", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputPath_arrayIndex() {
|
||||
String body = "{\"users\":[{\"name\":\"Eve\"},{\"name\":\"Frank\"}]}";
|
||||
String result = engine.evaluate("$input.path('$.users[1].name')", ctx(body));
|
||||
String result = engine.evaluate("$input.path('$.users[1].name')", ctx(body)).body();
|
||||
assertEquals("Frank", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullBody() {
|
||||
String result = engine.evaluate("$input.body()", ctx(null));
|
||||
String result = engine.evaluate("$input.body()", ctx(null)).body();
|
||||
assertEquals("", result);
|
||||
}
|
||||
|
||||
@@ -221,7 +221,7 @@ class VtlTemplateEngineTest {
|
||||
String body = "{\"names\":[\"Alice\",\"Bob\",\"Carol\"]}";
|
||||
String template = "#set($names = $input.path('$.names'))\n"
|
||||
+ "[#foreach($name in $names)\"$name\"#if($foreach.hasNext),#end#end]";
|
||||
String result = engine.evaluate(template, ctx(body)).trim();
|
||||
String result = engine.evaluate(template, ctx(body)).body().trim();
|
||||
assertEquals("[\"Alice\",\"Bob\",\"Carol\"]", result);
|
||||
}
|
||||
|
||||
@@ -233,7 +233,7 @@ class VtlTemplateEngineTest {
|
||||
+ "{\"results\": [#foreach($item in $items)"
|
||||
+ "{\"key\": \"$item.id\"}#if($foreach.hasNext),#end"
|
||||
+ "#end]}";
|
||||
String result = engine.evaluate(template, ctx(body)).trim();
|
||||
String result = engine.evaluate(template, ctx(body)).body().trim();
|
||||
assertDoesNotThrow(() -> new ObjectMapper().readTree(result));
|
||||
var node = assertDoesNotThrow(() -> new ObjectMapper().readTree(result));
|
||||
assertEquals(2, node.path("results").size());
|
||||
@@ -247,7 +247,7 @@ class VtlTemplateEngineTest {
|
||||
String body = "{\"type\":\"premium\"}";
|
||||
String template = "#set($type = $input.path('$.type'))\n"
|
||||
+ "#if($type == 'premium')PREMIUM#else STANDARD#end";
|
||||
String result = engine.evaluate(template, ctx(body)).trim();
|
||||
String result = engine.evaluate(template, ctx(body)).body().trim();
|
||||
assertEquals("PREMIUM", result);
|
||||
}
|
||||
|
||||
@@ -256,7 +256,7 @@ class VtlTemplateEngineTest {
|
||||
String body = "{\"type\":\"basic\"}";
|
||||
String template = "#set($type = $input.path('$.type'))\n"
|
||||
+ "#if($type == 'premium')PREMIUM#else STANDARD#end";
|
||||
String result = engine.evaluate(template, ctx(body)).trim();
|
||||
String result = engine.evaluate(template, ctx(body)).body().trim();
|
||||
assertEquals("STANDARD", result);
|
||||
}
|
||||
|
||||
@@ -266,7 +266,7 @@ class VtlTemplateEngineTest {
|
||||
String body = "{\"name\":\"test\"}";
|
||||
String template = "#set($desc = $input.path('$.description'))\n"
|
||||
+ "#if($desc && $desc != '')HAS_DESC#else NO_DESC#end";
|
||||
String result = engine.evaluate(template, ctx(body)).trim();
|
||||
String result = engine.evaluate(template, ctx(body)).body().trim();
|
||||
assertEquals("NO_DESC", result);
|
||||
}
|
||||
|
||||
@@ -278,7 +278,7 @@ class VtlTemplateEngineTest {
|
||||
String body = "{\"orderId\":\"123\",\"amount\":99.95}";
|
||||
String template = """
|
||||
{"QueueUrl": "http://localhost:4566/000000000000/my-queue", "MessageBody": "$util.escapeJavaScript($input.json('$'))"}""";
|
||||
String result = engine.evaluate(template, ctx(body));
|
||||
String result = engine.evaluate(template, ctx(body)).body();
|
||||
assertDoesNotThrow(() -> new ObjectMapper().readTree(result));
|
||||
var node = assertDoesNotThrow(() -> new ObjectMapper().readTree(result));
|
||||
assertEquals("http://localhost:4566/000000000000/my-queue", node.path("QueueUrl").asText());
|
||||
@@ -295,7 +295,7 @@ class VtlTemplateEngineTest {
|
||||
"req-456", "000000000000", Map.of("tableName", "orders"));
|
||||
String template = """
|
||||
{"TableName": "$stageVariables.tableName", "KeyConditionExpression": "pk = :pk", "ExpressionAttributeValues": {":pk": {"S": "$input.params().querystring.userId"}}}""";
|
||||
String result = engine.evaluate(template, queryCtx);
|
||||
String result = engine.evaluate(template, queryCtx).body();
|
||||
var node = assertDoesNotThrow(() -> new ObjectMapper().readTree(result));
|
||||
assertEquals("orders", node.path("TableName").asText());
|
||||
assertEquals("user-42", node.path("ExpressionAttributeValues").path(":pk").path("S").asText());
|
||||
@@ -305,13 +305,13 @@ class VtlTemplateEngineTest {
|
||||
|
||||
@Test
|
||||
void inputParams_headerAccess() {
|
||||
String result = engine.evaluate("$input.params().header.Authorization", ctx("{}"));
|
||||
String result = engine.evaluate("$input.params().header.Authorization", ctx("{}")).body();
|
||||
assertEquals("Bearer xyz", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputParams_pathAccess() {
|
||||
String result = engine.evaluate("$input.params().path.proxy", ctx("{}"));
|
||||
String result = engine.evaluate("$input.params().path.proxy", ctx("{}")).body();
|
||||
assertEquals("users/123", result);
|
||||
}
|
||||
|
||||
@@ -319,7 +319,7 @@ class VtlTemplateEngineTest {
|
||||
void inputParams_allTypes() {
|
||||
// Access all three param types in one template
|
||||
String template = "$input.params().querystring.limit|$input.params().path.proxy|$input.params().header.get('Content-Type')";
|
||||
String result = engine.evaluate(template, ctx("{}"));
|
||||
String result = engine.evaluate(template, ctx("{}")).body();
|
||||
assertTrue(result.startsWith("10|users/123|"), "Should contain all param types: " + result);
|
||||
}
|
||||
|
||||
@@ -329,25 +329,25 @@ class VtlTemplateEngineTest {
|
||||
|
||||
@Test
|
||||
void inputParams_shorthand_querystring() {
|
||||
String result = engine.evaluate("$input.params('limit')", ctx("{}"));
|
||||
String result = engine.evaluate("$input.params('limit')", ctx("{}")).body();
|
||||
assertEquals("10", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputParams_shorthand_path() {
|
||||
String result = engine.evaluate("$input.params('proxy')", ctx("{}"));
|
||||
String result = engine.evaluate("$input.params('proxy')", ctx("{}")).body();
|
||||
assertEquals("users/123", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputParams_shorthand_header() {
|
||||
String result = engine.evaluate("$input.params('Authorization')", ctx("{}"));
|
||||
String result = engine.evaluate("$input.params('Authorization')", ctx("{}")).body();
|
||||
assertEquals("Bearer xyz", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inputParams_shorthand_notFound() {
|
||||
String result = engine.evaluate("$input.params('nonexistent')", ctx("{}"));
|
||||
String result = engine.evaluate("$input.params('nonexistent')", ctx("{}")).body();
|
||||
assertEquals("", result);
|
||||
}
|
||||
|
||||
@@ -359,21 +359,56 @@ class VtlTemplateEngineTest {
|
||||
Map.of("shared", "query-val"),
|
||||
Map.of("shared", "path-val"),
|
||||
"prod", "GET", "/", "req-1", "000000000000", Map.of());
|
||||
String result = engine.evaluate("$input.params('shared')", overlapCtx);
|
||||
String result = engine.evaluate("$input.params('shared')", overlapCtx).body();
|
||||
assertEquals("query-val", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void utilParseJson_navigateResult() {
|
||||
String template = "#set($parsed = $util.parseJson('{\"a\":{\"b\":\"deep\"}}'))\n$parsed.a.b";
|
||||
String result = engine.evaluate(template, ctx("{}")).trim();
|
||||
String result = engine.evaluate(template, ctx("{}")).body().trim();
|
||||
assertEquals("deep", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void utilParseJson_withArray() {
|
||||
String template = "#set($parsed = $util.parseJson('[1,2,3]'))\n$parsed.size()";
|
||||
String result = engine.evaluate(template, ctx("{}")).trim();
|
||||
String result = engine.evaluate(template, ctx("{}")).body().trim();
|
||||
assertEquals("3", result);
|
||||
}
|
||||
|
||||
// ──────────── $context.responseOverride ────────────
|
||||
|
||||
@Test
|
||||
void responseOverride_status() {
|
||||
String template = "#set($context.responseOverride.status = 404)\nnot found";
|
||||
VtlTemplateEngine.EvaluateResult result = engine.evaluate(template, ctx("{}"));
|
||||
assertEquals(404, result.statusOverride());
|
||||
assertTrue(result.body().contains("not found"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void responseOverride_header() {
|
||||
String template = "#set($context.responseOverride.header[\"Content-Type\"] = \"application/problem+json\")\nerror";
|
||||
VtlTemplateEngine.EvaluateResult result = engine.evaluate(template, ctx("{}"));
|
||||
assertEquals("application/problem+json", result.headerOverrides().get("Content-Type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void responseOverride_statusAndHeader() {
|
||||
String template = """
|
||||
#set($context.responseOverride.status = 500)
|
||||
#set($context.responseOverride.header["X-Error"] = "internal")
|
||||
body""";
|
||||
VtlTemplateEngine.EvaluateResult result = engine.evaluate(template, ctx("{}"));
|
||||
assertEquals(500, result.statusOverride());
|
||||
assertEquals("internal", result.headerOverrides().get("X-Error"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void responseOverride_notSet_returnsNulls() {
|
||||
VtlTemplateEngine.EvaluateResult result = engine.evaluate("hello", ctx("{}"));
|
||||
assertNull(result.statusOverride());
|
||||
assertTrue(result.headerOverrides().isEmpty());
|
||||
}
|
||||
}
|
||||
+95
-11
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.dashjoin.jsonata.Jsonata.jsonata;
|
||||
@@ -53,10 +54,6 @@ class JsonataEdgeCaseTest {
|
||||
|
||||
Object result = expr.evaluate(null, frame);
|
||||
|
||||
// System.out.println("[Test 2] missing field result class: " + (result == null ? "null (Java null)" : result.getClass().getName()));
|
||||
// System.out.println("[Test 2] missing field result value: " + result);
|
||||
|
||||
// Key question: is it Java null?
|
||||
assertNull(result, "Accessing a missing field should return Java null");
|
||||
}
|
||||
|
||||
@@ -68,9 +65,6 @@ class JsonataEdgeCaseTest {
|
||||
|
||||
Object result = expr.evaluate(null, frame);
|
||||
|
||||
// System.out.println("[Test 2b] unbound variable result class: " + (result == null ? "null (Java null)" : result.getClass().getName()));
|
||||
// System.out.println("[Test 2b] unbound variable result value: " + result);
|
||||
|
||||
assertNull(result, "Accessing an unbound variable should return Java null");
|
||||
}
|
||||
|
||||
@@ -97,7 +91,6 @@ class JsonataEdgeCaseTest {
|
||||
frame1.bind("states", nested);
|
||||
Object city = expr1.evaluate(null, frame1);
|
||||
|
||||
// System.out.println("[Test 3a] deep path city: " + city);
|
||||
assertEquals("Springfield", city);
|
||||
|
||||
// Access into an array element
|
||||
@@ -106,7 +99,6 @@ class JsonataEdgeCaseTest {
|
||||
frame2.bind("states", nested);
|
||||
Object firstTag = expr2.evaluate(null, frame2);
|
||||
|
||||
// System.out.println("[Test 3b] deep path tags[0]: " + firstTag);
|
||||
assertEquals("admin", firstTag);
|
||||
|
||||
// Construct an object from deep paths
|
||||
@@ -115,7 +107,6 @@ class JsonataEdgeCaseTest {
|
||||
frame3.bind("states", nested);
|
||||
Object combined = expr3.evaluate(null, frame3);
|
||||
|
||||
// System.out.println("[Test 3c] combined object: " + combined);
|
||||
assertInstanceOf(Map.class, combined);
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> combinedMap = (Map<String, Object>) combined;
|
||||
@@ -140,7 +131,100 @@ class JsonataEdgeCaseTest {
|
||||
frame.bind("states", asMap);
|
||||
Object result = expr.evaluate(null, frame);
|
||||
|
||||
// System.out.println("[Test 4] sum of scores: " + result);
|
||||
assertEquals(60, ((Number) result).intValue());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// 5. Singleton sequence: 1-element array accessed via path
|
||||
// ---------------------------------------------------------------
|
||||
@Test
|
||||
void singleElementArray_propertyAccess_preservedNotReduced() throws Exception {
|
||||
// JSONata singleton-sequence rule: when path navigation through an array yields
|
||||
// a 1-element sequence, it is reduced to the single element.
|
||||
// BUT direct property access on an object (not an array) should NOT reduce.
|
||||
//
|
||||
// $states.result.Items where Items = [{...}] — $states.result is a plain object,
|
||||
// so .Items should return the List as-is, NOT the single element.
|
||||
var input = Map.of("result", Map.of(
|
||||
"Items", List.of(Map.of("id", "item1", "name", "Widget One")),
|
||||
"Count", 1
|
||||
));
|
||||
|
||||
var expr = jsonata("$states.result.Items");
|
||||
var frame = expr.createFrame();
|
||||
frame.bind("states", input);
|
||||
Object result = expr.evaluate(null, frame);
|
||||
|
||||
// The result must be the List itself, not the single element within it.
|
||||
assertInstanceOf(List.class, result,
|
||||
"1-element Items array must be returned as a List (no singleton reduction)");
|
||||
assertEquals(1, ((List<?>) result).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void multiElementArray_propertyAccess_returnsList() throws Exception {
|
||||
var input = Map.of("result", Map.of(
|
||||
"Items", List.of(
|
||||
Map.of("id", "item1"),
|
||||
Map.of("id", "item2")
|
||||
)
|
||||
));
|
||||
|
||||
var expr = jsonata("$states.result.Items");
|
||||
var frame = expr.createFrame();
|
||||
frame.bind("states", input);
|
||||
Object result = expr.evaluate(null, frame);
|
||||
|
||||
assertInstanceOf(List.class, result,
|
||||
"Multi-element Items array must remain a List");
|
||||
assertEquals(2, ((List<?>) result).size());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------
|
||||
// 6. Path-mapping transformation: 1-element sequence from object construction
|
||||
// e.g. items.{"field": value} — this CAN singleton-reduce in JSONata spec
|
||||
// ---------------------------------------------------------------
|
||||
@Test
|
||||
void objectMapping_singleElement_behaviorDocumented() throws Exception {
|
||||
// JSONata spec: when you do array.{key: value}, you get a sequence.
|
||||
// A 1-element sequence IS singleton-reduced to the single element.
|
||||
// This is correct JSONata behavior (and what AWS does too).
|
||||
// To force an array, callers should use [$states.result.Items.{"id": id}]
|
||||
// or $toArray(...) if available.
|
||||
var input = Map.of("result", Map.of(
|
||||
"Items", List.of(Map.of("id", "item1", "name", "Widget One"))
|
||||
));
|
||||
|
||||
var expr = jsonata("$states.result.Items.{\"id\": id, \"name\": name}");
|
||||
var frame = expr.createFrame();
|
||||
frame.bind("states", input);
|
||||
Object result = expr.evaluate(null, frame);
|
||||
|
||||
// 1-element object-mapping sequence IS singleton-reduced to a plain object (Map).
|
||||
// This matches both the JSONata spec and real AWS Step Functions behavior.
|
||||
// Callers that need an array must wrap in []: [$states.result.Items.{"id": id}]
|
||||
assertInstanceOf(Map.class, result, "1-element object-mapping should be singleton-reduced to a Map");
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> mapped = (Map<String, Object>) result;
|
||||
assertEquals("item1", mapped.get("id"));
|
||||
assertEquals("Widget One", mapped.get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void objectMapping_singleElement_wrappedInArray_forcesArray() throws Exception {
|
||||
// Workaround: wrap the mapping expression in [] to force array output.
|
||||
// [$states.result.Items.{"id": id}] — even for 1-element sequences, result is an array.
|
||||
var input = Map.of("result", Map.of(
|
||||
"Items", List.of(Map.of("id", "item1", "name", "Widget One"))
|
||||
));
|
||||
|
||||
var expr = jsonata("[$states.result.Items.{\"id\": id, \"name\": name}]");
|
||||
var frame = expr.createFrame();
|
||||
frame.bind("states", input);
|
||||
Object result = expr.evaluate(null, frame);
|
||||
|
||||
assertInstanceOf(List.class, result,
|
||||
"Wrapping in [] must force array output even for 1-element sequence");
|
||||
assertEquals(1, ((List<?>) result).size());
|
||||
}
|
||||
}
|
||||
+51
@@ -492,6 +492,57 @@ class StepFunctionsDynamoDbIntegrationTest {
|
||||
assertEquals("200", result.path("Attributes").path("score").path("N").asText());
|
||||
}
|
||||
|
||||
// ──────────────── Optimized Integration: putItem + getItem ────────────────
|
||||
|
||||
@Test
|
||||
@Order(71)
|
||||
void optimized_putAndGetItem() throws Exception {
|
||||
// Put via optimized integration
|
||||
executeSfnOptimized("opt-put", "dynamodb:putItem", """
|
||||
{
|
||||
"TableName": "%s",
|
||||
"Item": {
|
||||
"pk": {"S": "opt-get-test"},
|
||||
"sk": {"S": "row"},
|
||||
"value": {"S": "hello"}
|
||||
}
|
||||
}
|
||||
""".formatted(TABLE_NAME));
|
||||
|
||||
// Get via optimized integration — must return the item, not {}
|
||||
String output = executeSfnOptimized("opt-get", "dynamodb:getItem", """
|
||||
{
|
||||
"TableName": "%s",
|
||||
"Key": {
|
||||
"pk": {"S": "opt-get-test"},
|
||||
"sk": {"S": "row"}
|
||||
}
|
||||
}
|
||||
""".formatted(TABLE_NAME));
|
||||
|
||||
JsonNode result = mapper.readTree(output);
|
||||
assertTrue(result.has("Item"), "Optimized getItem must return Item field");
|
||||
assertEquals("hello", result.path("Item").path("value").path("S").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(72)
|
||||
void optimized_getItem_notFound_returnsEmptyObject() throws Exception {
|
||||
String output = executeSfnOptimized("opt-get-missing", "dynamodb:getItem", """
|
||||
{
|
||||
"TableName": "%s",
|
||||
"Key": {
|
||||
"pk": {"S": "does-not-exist"},
|
||||
"sk": {"S": "nope"}
|
||||
}
|
||||
}
|
||||
""".formatted(TABLE_NAME));
|
||||
|
||||
JsonNode result = mapper.readTree(output);
|
||||
// AWS returns {} (no Item field) when item does not exist
|
||||
assertFalse(result.has("Item"), "Not-found getItem must return empty object (no Item field)");
|
||||
}
|
||||
|
||||
// ──────────────── Error Handling ────────────────
|
||||
|
||||
@Test
|
||||
|
||||
+59
@@ -227,6 +227,65 @@ class StepFunctionsJsonataIntegrationTest {
|
||||
assertTrue(output.contains("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonataPassState_withResult_rejected() {
|
||||
// AWS rejects Result in JSONata states (SCHEMA_VALIDATION_FAILED).
|
||||
// Result is a JSONPath-only field; the JSONata equivalent is Output.
|
||||
String definition = """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "SetResult",
|
||||
"States": {
|
||||
"SetResult": {
|
||||
"Type": "Pass",
|
||||
"Result": {"status": "ok", "code": 200},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body(String.format("""
|
||||
{"name":"jsonata-result-test","definition":%s,"roleArn":"%s","type":"STANDARD"}
|
||||
""", quote(definition), ROLE_ARN))
|
||||
.when().post("/")
|
||||
.then().statusCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonataPassState_withParameters_rejected() {
|
||||
// AWS rejects Parameters in JSONata states (SCHEMA_VALIDATION_FAILED).
|
||||
// Parameters is a JSONPath-only field; the JSONata equivalent is Arguments.
|
||||
String definition = """
|
||||
{
|
||||
"QueryLanguage": "JSONata",
|
||||
"StartAt": "PrepareData",
|
||||
"States": {
|
||||
"PrepareData": {
|
||||
"Type": "Pass",
|
||||
"Parameters": {
|
||||
"created_at.$": "$$.Execution.StartTime"
|
||||
},
|
||||
"Output": {"processed": true},
|
||||
"End": true
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
given()
|
||||
.header("X-Amz-Target", "AWSStepFunctions.CreateStateMachine")
|
||||
.contentType(SFN_CONTENT_TYPE)
|
||||
.body(String.format("""
|
||||
{"name":"jsonata-parameters-test","definition":%s,"roleArn":"%s","type":"STANDARD"}
|
||||
""", quote(definition), ROLE_ARN))
|
||||
.when().post("/")
|
||||
.then().statusCode(400);
|
||||
}
|
||||
|
||||
// ──────────────── Helpers ────────────────
|
||||
|
||||
private String createStateMachine(String name, String definition) {
|
||||
|
||||
Reference in new issue
Block a user