feat(stepfunctions): resolve payload-template .$ inside arrays and $$ context in intrinsic args (#1691)

* feat(stepfunctions): resolve payload-template .$ inside arrays and $$ context in intrinsic args

Two AWS States Language parity fixes in the payload-template / intrinsic path:

- resolveParameters now recurses into arrays, not just objects. A Parameters / ItemSelector /
  ResultSelector template resolves .$ references at any nesting depth, including inside arrays
  (e.g. an ECS Overrides.ContainerOverrides[].Environment[].Value.$). Previously array elements
  passed through verbatim, so those references were never substituted.
- Intrinsic-function arguments can now reference the Context Object ($$). resolvePath /
  evaluateIntrinsic / resolveIntrinsicArg thread the Context Object through, so an argument like
  States.Format('{}', $$.Map.Item.Value.x) or a bare $$.Execution.Id resolves against the
  context. Ordinary $. / input-only resolution is unchanged (context is null there).

Adds AslExecutorArrayParamsTest and AslExecutorIntrinsicContextTest.

* docs: regenerate action tables
This commit is contained in:
Alex Banna authored and GitHub committed 2026-07-09 00:17:24 -05:00
1 parent 4a16266f48
commit 54b378ed6c
3 files changed
+226 -21

No files matched your search

@@ -1716,9 +1716,12 @@ public class AslExecutor {
} else if ("$$".equals(path)) {
resolved.set(realKey, context);
} else {
resolved.set(realKey, resolvePath(path, input));
// Pass the Context Object through so a $$. reference nested inside an
// intrinsic (e.g. States.Format(..., $$.Map.Item.Value.x)) can resolve it;
// for a plain $. or States.* input reference, context is simply ignored.
resolved.set(realKey, resolvePath(path, input, context));
}
} else if (val.isObject()) {
} else if (val.isObject() || val.isArray()) {
resolved.set(key, resolveParameters(val, input, context));
} else {
resolved.set(key, val);
@@ -1726,27 +1729,53 @@ public class AslExecutor {
}
return resolved;
}
if (parameters.isArray()) {
// Payload templates resolve .$ references at any depth, including inside arrays
// (e.g. an ECS Overrides.ContainerOverrides[].Environment[].Value.$).
ArrayNode resolvedArray = objectMapper.createArrayNode();
for (JsonNode element : parameters) {
resolvedArray.add(resolveParameters(element, input, context));
}
return resolvedArray;
}
return parameters;
}
JsonNode resolvePath(String path, JsonNode root) {
JsonNode node = resolvePathNode(path, root);
// Most callers do not distinguish an absent path from an explicit null; collapse both to null.
return resolvePath(path, root, null);
}
/**
* Resolve a JSONPath reference or {@code States.*} intrinsic. When {@code context} is
* non-null it is the Context Object ({@code $$}), letting intrinsic arguments reference it
* (e.g. a {@code $$.Map.Item.Value.x} argument nested inside {@code States.Format(...)}).
* It is null for ordinary input-only resolution, which preserves existing behavior.
*
* <p>Most callers do not distinguish an absent path from an explicit null, so both collapse
* to null; callers that care about presence (e.g. {@code IsPresent}) use {@link #resolvePathNode}.
*/
JsonNode resolvePath(String path, JsonNode root, JsonNode context) {
JsonNode node = resolvePathNode(path, root, context);
return node.isMissingNode() ? NullNode.getInstance() : node;
}
JsonNode resolvePathNode(String path, JsonNode root) {
return resolvePathNode(path, root, null);
}
/**
* Resolves a reference path while preserving the distinction between an explicit null value
* (returns a {@link NullNode}) and a missing/absent path (returns a {@link MissingNode}).
* {@link #resolvePath} collapses both to null; only callers that care about presence
* (e.g. {@code IsPresent}) should use this variant.
* (e.g. {@code IsPresent}) should use this variant. When {@code context} is non-null it is the
* Context Object ({@code $$}) available to {@code States.*} intrinsic arguments.
*/
JsonNode resolvePathNode(String path, JsonNode root) {
JsonNode resolvePathNode(String path, JsonNode root, JsonNode context) {
if (path == null || "$".equals(path)) {
return root;
}
if (path.startsWith("States.")) {
return evaluateIntrinsic(path, root);
return evaluateIntrinsic(path, root, context);
}
// Support dotted ($.a.b) and root-bracket ($[*], $[0]) forms; anything else is unsupported.
if (!path.startsWith("$.") && !path.startsWith("$[")) {
@@ -1842,7 +1871,7 @@ public class AslExecutor {
* States.Array, States.ArrayLength, States.ArrayContains, States.MathAdd, States.UUID.
* Throws FailStateException("States.Runtime") for unrecognized functions.
*/
private JsonNode evaluateIntrinsic(String expr, JsonNode root) {
private JsonNode evaluateIntrinsic(String expr, JsonNode root, JsonNode context) {
int parenOpen = expr.indexOf('(');
int parenClose = expr.lastIndexOf(')');
if (parenOpen < 0 || parenClose < 0) {
@@ -1853,7 +1882,7 @@ public class AslExecutor {
return switch (fnName) {
case "States.StringToJson" -> {
JsonNode arg = resolveIntrinsicArg(argsStr, root);
JsonNode arg = resolveIntrinsicArg(argsStr, root, context);
try {
yield objectMapper.readTree(arg.asText());
} catch (Exception e) {
@@ -1862,7 +1891,7 @@ public class AslExecutor {
}
}
case "States.JsonToString" -> {
JsonNode arg = resolveIntrinsicArg(argsStr, root);
JsonNode arg = resolveIntrinsicArg(argsStr, root, context);
try {
yield objectMapper.getNodeFactory().textNode(objectMapper.writeValueAsString(arg));
} catch (Exception e) {
@@ -1882,7 +1911,7 @@ public class AslExecutor {
if (argIdx >= parts.size()) {
throw new FailStateException("States.Runtime", "States.Format: not enough arguments");
}
JsonNode argVal = resolveIntrinsicArg(parts.get(argIdx++).trim(), root);
JsonNode argVal = resolveIntrinsicArg(parts.get(argIdx++).trim(), root, context);
sb.append(argVal.isTextual() ? argVal.asText() : argVal.toString());
i++; // skip '}'
} else {
@@ -1895,12 +1924,12 @@ public class AslExecutor {
List<String> parts = splitIntrinsicArgs(argsStr);
ArrayNode arr = objectMapper.createArrayNode();
for (String part : parts) {
arr.add(resolveIntrinsicArg(part.trim(), root));
arr.add(resolveIntrinsicArg(part.trim(), root, context));
}
yield arr;
}
case "States.ArrayLength" -> {
JsonNode arg = resolveIntrinsicArg(argsStr, root);
JsonNode arg = resolveIntrinsicArg(argsStr, root, context);
if (!arg.isArray()) {
throw new FailStateException("States.Runtime", "States.ArrayLength requires an array");
}
@@ -1911,8 +1940,8 @@ public class AslExecutor {
if (parts.size() != 2) {
throw new FailStateException("States.Runtime", "States.MathAdd requires exactly 2 arguments");
}
JsonNode a = resolveIntrinsicArg(parts.get(0).trim(), root);
JsonNode b = resolveIntrinsicArg(parts.get(1).trim(), root);
JsonNode a = resolveIntrinsicArg(parts.get(0).trim(), root, context);
JsonNode b = resolveIntrinsicArg(parts.get(1).trim(), root, context);
yield objectMapper.getNodeFactory().numberNode(a.asLong() + b.asLong());
}
case "States.ArrayContains" -> {
@@ -1921,8 +1950,8 @@ public class AslExecutor {
throw new FailStateException("States.Runtime",
"States.ArrayContains requires exactly 2 arguments");
}
JsonNode array = resolveIntrinsicArg(parts.get(0).trim(), root);
JsonNode value = resolveIntrinsicArg(parts.get(1).trim(), root);
JsonNode array = resolveIntrinsicArg(parts.get(0).trim(), root, context);
JsonNode value = resolveIntrinsicArg(parts.get(1).trim(), root, context);
if (!array.isArray()) {
// AWS throws rather than silently returning false, matching States.ArrayLength.
throw new FailStateException("States.Runtime",
@@ -1949,10 +1978,23 @@ public class AslExecutor {
* Resolve a single intrinsic argument: either a $.path reference, a quoted string literal,
* or a numeric literal.
*/
private JsonNode resolveIntrinsicArg(String arg, JsonNode root) {
private JsonNode resolveIntrinsicArg(String arg, JsonNode root, JsonNode context) {
arg = arg.trim();
// A $$.-prefixed argument references the Context Object; resolve it against context
// (as a $. path) so intrinsics can read e.g. $$.Map.Item.Value.x or $$.Execution.Id.
// When context is null these fall through to the bare-path branch and a $$. arg formats
// as the literal string "null". This is Floci-internal transitional behavior, not AWS
// semantics: on real AWS the Context Object always exists. Every payload-template call
// site already threads context; this fallback (and the noContext_* test pinning it) only
// exists until context is also threaded into the remaining resolvePath callers.
if (context != null && arg.startsWith("$$.")) {
return resolvePath("$." + arg.substring(3), context);
}
if (context != null && "$$".equals(arg)) {
return context;
}
if (arg.startsWith("$.") || "$".equals(arg)) {
return resolvePath(arg, root);
return resolvePath(arg, root, context);
}
if (arg.startsWith("'") && arg.endsWith("'")) {
return objectMapper.getNodeFactory().textNode(arg.substring(1, arg.length() - 1));
@@ -1966,8 +2008,8 @@ public class AslExecutor {
try {
return objectMapper.getNodeFactory().numberNode(Double.parseDouble(arg));
} catch (NumberFormatException e2) {
// fall through: treat as bare path
return resolvePath(arg, root);
// fall through: treat as bare path (may itself be a nested States.* intrinsic)
return resolvePath(arg, root, context);
}
}
}