Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lib): render empty string as quotes #3683

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .mise.toml
DanielMSchmidt marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[tools]
node = '18'
python = '3.11'
go = '1.18'
java = "prefix:corretto-20"
14 changes: 11 additions & 3 deletions packages/cdktf/lib/hcl/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ function wrapIdentifierInQuotesIfNeeded(key: string): string {
*
*/
function renderString(str: string): string {
if (str === "") {
return `""`;
}

if (!str) {
return str;
}
Expand Down Expand Up @@ -526,6 +530,10 @@ function renderFuzzyJsonExpression(jsonExpression: any): string {
}

if (typeof jsonExpression === "string") {
if (jsonExpression === "") {
return `""`;
}

if (jsonExpression.includes("${")) {
return `"${jsonExpression}"`;
}
Expand Down Expand Up @@ -609,7 +617,7 @@ export function renderAttributes(attributes: any): string {
!v.hasOwnProperty("dynamic")
) {
if (metaBlocks.includes(name)) {
return `${name} {
return `${name} {
${renderSimpleAttributes(v)}
}`;
}
Expand Down Expand Up @@ -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") {
Expand Down
27 changes: 27 additions & 0 deletions packages/cdktf/test/json-to-hcl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading