diff --git a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java index 82d2c10b8a..0ba721a7aa 100644 --- a/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java +++ b/src/main/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutor.java @@ -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. + * + *

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 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); } } } diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorArrayParamsTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorArrayParamsTest.java new file mode 100644 index 0000000000..ab5f2d940f --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorArrayParamsTest.java @@ -0,0 +1,56 @@ +package io.github.hectorvent.floci.services.stepfunctions; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * A Parameters / ItemSelector payload template must resolve {@code .$} references at ANY nesting + * depth, including inside arrays — e.g. the {@code Environment} array of an ECS + * {@code Overrides.ContainerOverrides[]}. Without array recursion those {@code Value.$} entries pass + * through verbatim and the launched container receives empty environment variables. + */ +class AslExecutorArrayParamsTest { + + private final ObjectMapper mapper = new ObjectMapper(); + + private AslExecutor newExecutor() { + return new AslExecutor(null, null, null, null, null, null, null, null, + null, null, mapper, null, null); + } + + @Test + void resolvesDotDollarReferencesInsideArrays() throws Exception { + JsonNode parameters = mapper.readTree(""" + { + "Overrides": { + "ContainerOverrides": [ + { + "Name": "runner", + "Environment": [ + {"Name": "ACTION_TYPE", "Value.$": "$.solution.operation"}, + {"Name": "PROVISION_TYPE", "Value.$": "$.solution.primaryProvisionType"}, + {"Name": "STATIC", "Value": "CREATE_UPDATE"} + ] + } + ] + } + } + """); + JsonNode input = mapper.readTree(""" + { "solution": { "operation": "create", "primaryProvisionType": "awscdk" } } + """); + + JsonNode resolved = newExecutor().resolveParameters(parameters, input, mapper.createObjectNode()); + + JsonNode env = resolved.path("Overrides").path("ContainerOverrides").path(0).path("Environment"); + assertEquals("create", env.path(0).path("Value").asText()); + assertEquals("awscdk", env.path(1).path("Value").asText()); + assertEquals("CREATE_UPDATE", env.path(2).path("Value").asText()); + // The unresolved ".$" key must be gone. + assertTrue(env.path(0).path("Value.$").isMissingNode(), "Value.$ should have been resolved away"); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorIntrinsicContextTest.java b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorIntrinsicContextTest.java new file mode 100644 index 0000000000..d1579fb8da --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/stepfunctions/AslExecutorIntrinsicContextTest.java @@ -0,0 +1,107 @@ +package io.github.hectorvent.floci.services.stepfunctions; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Unit tests for Context Object ($$) resolution inside JSONPath-mode intrinsic functions. + * + *

A whole-value {@code "x.$": "$$.Map.Item.Value"} Parameters field already resolved against + * the Context Object, but a {@code $$.} reference nested inside an intrinsic argument — e.g. + * {@code States.Format('.../{}', $$.Map.Item.Value.solutionId)} — used to fall through to an + * input-only lookup and resolve to null, because the context was never threaded from + * {@code resolveParameters} into {@code evaluateIntrinsic}/{@code resolveIntrinsicArg}. These + * tests pin the fix and guard the no-context (input-only) path against regression. + */ +class AslExecutorIntrinsicContextTest { + + private final ObjectMapper mapper = new ObjectMapper(); + + /** Only ObjectMapper is exercised by the path/intrinsic resolution methods under test. */ + private AslExecutor newExecutor() { + return new AslExecutor(null, null, null, null, null, null, null, null, + null, null, mapper, null, null); + } + + @Test + void statesFormat_resolvesContextArgInsideParameters() throws Exception { + AslExecutor executor = newExecutor(); + // Shaped like a Map ItemSelector: $. args come from the Map state's effective input, + // $$.Map.Item.Value.* comes from the per-iteration Context Object. + JsonNode parameters = mapper.readTree(""" + { + "provisionerFolder.$": "States.Format('/mnt/efs/{}/{}/{}/provisioner', $.systemId, $.systemConfigVersion, $$.Map.Item.Value.solutionId)" + } + """); + JsonNode input = mapper.readTree(""" + { "systemId": "SYS1", "systemConfigVersion": "v3" } + """); + JsonNode context = mapper.readTree(""" + { "Map": { "Item": { "Index": 0, "Value": { "solutionId": "SOLUTION_A" } } } } + """); + + JsonNode resolved = executor.resolveParameters(parameters, input, context); + + assertEquals("/mnt/efs/SYS1/v3/SOLUTION_A/provisioner", + resolved.path("provisionerFolder").asText()); + } + + @Test + void statesFormat_resolvesContextArgViaResolvePath() { + AslExecutor executor = newExecutor(); + JsonNode input = mapper.createObjectNode(); + JsonNode context = mapper.createObjectNode(); + ((com.fasterxml.jackson.databind.node.ObjectNode) context).putObject("Map") + .putObject("Item").putObject("Value").put("solutionId", "SOLUTION_B"); + + JsonNode result = executor.resolvePath( + "States.Format('id={}', $$.Map.Item.Value.solutionId)", input, context); + + assertEquals("id=SOLUTION_B", result.asText()); + } + + @Test + void wholeContext_resolvesInsideIntrinsic() { + AslExecutor executor = newExecutor(); + JsonNode input = mapper.createObjectNode(); + JsonNode context = mapper.createObjectNode(); + ((com.fasterxml.jackson.databind.node.ObjectNode) context).put("token", "abc"); + + // States.JsonToString($$) serializes the whole Context Object. + JsonNode result = executor.resolvePath("States.JsonToString($$)", input, context); + + assertEquals("{\"token\":\"abc\"}", result.asText()); + } + + @Test + void inputArgsStillResolveWithContextPresent() { + AslExecutor executor = newExecutor(); + JsonNode input = mapper.createObjectNode(); + ((com.fasterxml.jackson.databind.node.ObjectNode) input).put("a", "X").put("b", "Y"); + JsonNode context = mapper.createObjectNode(); + + JsonNode result = executor.resolvePath("States.Format('{}-{}', $.a, $.b)", input, context); + + assertEquals("X-Y", result.asText()); + } + + @Test + void noContext_intrinsicResolutionUnchanged() { + AslExecutor executor = newExecutor(); + JsonNode input = mapper.createObjectNode(); + ((com.fasterxml.jackson.databind.node.ObjectNode) input).put("name", "widget"); + + // The 2-arg form (context == null) must behave exactly as before: $. args resolve, + // and a stray $$. arg — which has no context to resolve against — yields "null". + // The "null" here pins Floci-internal transitional behavior, NOT AWS semantics (on real + // AWS the Context Object always exists); see resolveIntrinsicArg. It only holds until + // context is threaded into the remaining resolvePath callers, at which point this + // assertion should be updated rather than treated as a contract. + assertEquals("widget", executor.resolvePath("States.Format('{}', $.name)", input).asText()); + assertEquals("null", executor.resolvePath( + "States.Format('{}', $$.Map.Item.Value.solutionId)", input).asText()); + } +}