Skip to content

Commit 6b78c57

Browse files
lib: render empty string as quotes
1 parent 91ec8b0 commit 6b78c57

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

packages/cdktf/lib/hcl/render.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ function wrapIdentifierInQuotesIfNeeded(key: string): string {
2626
*
2727
*/
2828
function renderString(str: string): string {
29+
if (str === "") {
30+
return `""`;
31+
}
32+
2933
if (!str) {
3034
return str;
3135
}
@@ -526,6 +530,10 @@ function renderFuzzyJsonExpression(jsonExpression: any): string {
526530
}
527531

528532
if (typeof jsonExpression === "string") {
533+
if (jsonExpression === "") {
534+
return `""`;
535+
}
536+
529537
if (jsonExpression.includes("${")) {
530538
return `"${jsonExpression}"`;
531539
}
@@ -609,7 +617,7 @@ export function renderAttributes(attributes: any): string {
609617
!v.hasOwnProperty("dynamic")
610618
) {
611619
if (metaBlocks.includes(name)) {
612-
return `${name} {
620+
return `${name} {
613621
${renderSimpleAttributes(v)}
614622
}`;
615623
}
@@ -643,8 +651,8 @@ ${renderSimpleAttributes(v)}
643651
}
644652

645653
if (block && type !== "list" && type !== "set") {
646-
return `${name} {
647-
${renderAttributes(value)}
654+
return `${name} {
655+
${renderAttributes(value)}
648656
}`;
649657
}
650658
if (type === "list" || type === "set") {

packages/cdktf/test/json-to-hcl.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,33 @@ test("string local with quoted name", async () => {
7474
`);
7575
});
7676

77+
test("empty string", async () => {
78+
const app = Testing.app();
79+
const stack = new TerraformStack(app, "test");
80+
81+
new TerraformLocal(stack, "greeting", {
82+
a: "",
83+
});
84+
85+
new TestResource(stack, "test", {
86+
name: "",
87+
});
88+
89+
const hcl = Testing.synthHcl(stack);
90+
expect(hcl).toMatchInlineSnapshot(`
91+
"
92+
93+
locals {
94+
greeting = {
95+
a = ""
96+
}
97+
}
98+
resource "test_resource" "test" {
99+
name = ""
100+
}"
101+
`);
102+
});
103+
77104
test("with provider alias", async () => {
78105
const app = Testing.app();
79106
const stack = new TerraformStack(app, "test");

0 commit comments

Comments
 (0)