From 2cea55bda5674c439b3a3197b3f99a9b03a4040b Mon Sep 17 00:00:00 2001 From: Vu The Huy Date: Thu, 3 Sep 2026 18:47:28 +0700 Subject: [PATCH] fix(cloudformation): route ProvisionContext.resolveStringList through the list-aware engine (#2937) ProvisionContext.resolveStringList was a scalar-only copy of the monolith helper that CloudFormationResourceProvisioner already reroutes, and it is the one the extracted per-service provisioners call (IamRoleCfnProvisioner ManagedPolicyArns, OrganizationsCfnProvisioner ParentIds and TargetIds). Delegate to engine.resolveStringList so a list-valued intrinsic (Fn::Split / Fn::GetAZs / Fn::Cidr, including one selected by Fn::If) expands to its real values instead of collapsing to one comma-joined string, keeping "every list property" true and stopping the two helpers from drifting. The mock engines in IamRoleCfnProvisionerTest and OrganizationsCfnProvisionerTest now delegate resolveStringList to the real method (literal arrays still resolve each element through the existing resolve(...) stub). --- .../provisioners/ProvisionContext.java | 22 +++++++------ .../IamRoleCfnProvisionerTest.java | 4 +++ .../OrganizationsCfnProvisionerTest.java | 3 ++ .../provisioners/ProvisionContextTest.java | 33 +++++++++++++++++++ 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java index dd33a20961..fa617a92a5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContext.java @@ -80,18 +80,20 @@ public record ProvisionContext(CloudFormationTemplateEngine engine, String regio return engine.resolve(props.get(name)); } - /** Resolves an array property to its non-blank elements, or an empty list when absent. */ + /** + * Resolves a list property to its non-blank elements, or an empty list when absent. + * + *

Routes through {@code engine.resolveStringList} so a list-valued intrinsic + * ({@code Fn::Split} / {@code Fn::GetAZs} / {@code Fn::Cidr}, including one selected by an + * {@code Fn::If}) expands to its real values instead of collapsing to a single comma-joined + * string — the shape CDK emits for cross-stack references (issue #2937). Kept in lockstep + * with the monolith helper the per-service provisioners were split from. + */ public List resolveStringList(JsonNode props, String name) { - List values = new ArrayList<>(); - if (props != null && props.has(name) && props.get(name).isArray()) { - for (JsonNode element : props.get(name)) { - String resolved = engine.resolve(element); - if (resolved != null && !resolved.isBlank()) { - values.add(resolved); - } - } + if (props == null || !props.has(name)) { + return new ArrayList<>(); } - return values; + return new ArrayList<>(engine.resolveStringList(props.get(name))); } /** diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamRoleCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamRoleCfnProvisionerTest.java index 140057bf23..bca004c66e 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamRoleCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/IamRoleCfnProvisionerTest.java @@ -53,6 +53,10 @@ class IamRoleCfnProvisionerTest { when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0)); + // resolveStringList delegates to the real engine method; for the literal arrays these + // tests use it just walks the array and calls resolve(...) per element, stubbed above. + when(engine.resolveStringList(any())).thenCallRealMethod(); + when(engine.resolveJsonAttribute(any())).thenAnswer(inv -> { JsonNode node = inv.getArgument(0); return node != null && node.isTextual() ? node.asText() : node.toString(); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/OrganizationsCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/OrganizationsCfnProvisionerTest.java index fd7912cb49..26d8e7f6a1 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/OrganizationsCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/OrganizationsCfnProvisionerTest.java @@ -62,6 +62,9 @@ class OrganizationsCfnProvisionerTest { return node == null ? null : node.asText(); }); when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0)); + // resolveStringList delegates to the real engine method; for the literal arrays these + // tests use it just walks the array and calls resolve(...) per element, stubbed above. + when(engine.resolveStringList(any())).thenCallRealMethod(); when(engine.resolveJsonAttribute(any())).thenAnswer(inv -> { JsonNode node = inv.getArgument(0); return node.isTextual() ? node.asText() : node.toString(); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContextTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContextTest.java index f3d0aa306b..1859a3f710 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContextTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/ProvisionContextTest.java @@ -15,6 +15,8 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; /** The shared helpers on {@link ProvisionContext} that extracted provisioners build on. */ @@ -177,4 +179,35 @@ class ProvisionContextTest { "a replacing update derives a different name and must still create"); } + + /** + * The whole property node is handed to {@code engine.resolveStringList}, so a list-valued + * intrinsic ({@code Fn::Split} over a cross-stack {@code Fn::ImportValue}) expands to its real + * values instead of collapsing to one comma-joined string (issue #2937). Resolving each element + * in isolation, as the old copy did, would have yielded a single unusable entry. + */ + @Test + void resolveStringListRoutesListValuedIntrinsicsThroughTheEngine() { + JsonNode props = props(""" + { + "SubnetIds": { + "Fn::Split": [",", {"Fn::ImportValue": "vpc-stack:PrivateSubnetIds"}] + } + } + """); + when(engine.resolveStringList(props.get("SubnetIds"))) + .thenReturn(List.of("subnet-a", "subnet-b", "subnet-c")); + + assertEquals(List.of("subnet-a", "subnet-b", "subnet-c"), + context(null).resolveStringList(props, "SubnetIds")); + } + + @Test + void resolveStringListReturnsAMutableEmptyListForAnAbsentPropertyWithoutCallingTheEngine() { + List values = context(null).resolveStringList(props("{}"), "ManagedPolicyArns"); + + assertEquals(List.of(), values); + values.add("still-mutable"); + verify(engine, never()).resolveStringList(any()); + } }