diff --git a/packages/cdktf/lib/hcl/render.ts b/packages/cdktf/lib/hcl/render.ts index 7ae0d72bf6..0aea2957d6 100644 --- a/packages/cdktf/lib/hcl/render.ts +++ b/packages/cdktf/lib/hcl/render.ts @@ -26,6 +26,10 @@ function wrapIdentifierInQuotesIfNeeded(key: string): string { * */ function renderString(str: string): string { + if (str === "") { + return `""`; + } + if (!str) { return str; } @@ -526,6 +530,10 @@ function renderFuzzyJsonExpression(jsonExpression: any): string { } if (typeof jsonExpression === "string") { + if (jsonExpression === "") { + return `""`; + } + if (jsonExpression.includes("${")) { return `"${jsonExpression}"`; } @@ -609,7 +617,7 @@ export function renderAttributes(attributes: any): string { !v.hasOwnProperty("dynamic") ) { if (metaBlocks.includes(name)) { - return `${name} { + return `${name} { ${renderSimpleAttributes(v)} }`; } @@ -643,8 +651,8 @@ ${renderSimpleAttributes(v)} } if (block && type !== "list" && type !== "set") { - return `${name} { -${renderAttributes(value)} + return `${name} { +${renderAttributes(value)} }`; } if (type === "list" || type === "set") { diff --git a/packages/cdktf/test/json-to-hcl.test.ts b/packages/cdktf/test/json-to-hcl.test.ts index 886937eaf4..67c369b600 100644 --- a/packages/cdktf/test/json-to-hcl.test.ts +++ b/packages/cdktf/test/json-to-hcl.test.ts @@ -74,6 +74,33 @@ test("string local with quoted name", async () => { `); }); +test("empty string", async () => { + const app = Testing.app(); + const stack = new TerraformStack(app, "test"); + + new TerraformLocal(stack, "greeting", { + a: "", + }); + + new TestResource(stack, "test", { + name: "", + }); + + const hcl = Testing.synthHcl(stack); + expect(hcl).toMatchInlineSnapshot(` + " + + locals { + greeting = { + a = "" + } + } + resource "test_resource" "test" { + name = "" + }" + `); +}); + test("with provider alias", async () => { const app = Testing.app(); const stack = new TerraformStack(app, "test");