Skip to content

Commit

Permalink
fix(lib): Correctly render string tokens that contain plain objects (#…
Browse files Browse the repository at this point in the history
…3545)

Fixes #3540
  • Loading branch information
ansgarm authored Mar 11, 2024
1 parent 639b61a commit cd31325
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/cdktf/lib/tfExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ class TFExpression extends Intrinsic implements IResolvable {
const resolvedArg = context.resolve(arg);

if (typeof arg === "string") {
return this.resolveString(arg, resolvedArg);
const str = this.resolveString(arg, resolvedArg);
// When Token.asString() is used on an object, str will be the object and needs to be resolved differently
// This happens for example in MapTerraformIterator#_getForEachExpression and can cause issues like #3540
if (typeof str !== "string") {
return this.resolveExpressionPart(context, str);
}
return str;
}

return this.resolveExpressionPart(context, arg);
Expand Down
7 changes: 7 additions & 0 deletions packages/cdktf/test/tfExpression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,10 @@ test("nesting can undo the wrapping", () => {
`"\${x(docker_container.foo.bar, "this is the ref: \${y("my ref: \${docker_container.foo.bar}", docker_container.foo.bar)}")}"`
);
});

test("expressions correctly resolve string wrapped objects", () => {
const expr = call("keys", [Token.asString({ a: "b" })]);
expect(resolveExpression(expr)).toMatchInlineSnapshot(
`"\${keys({"a" = "b"})}"`
);
});

0 comments on commit cd31325

Please sign in to comment.