Skip to content

Fix cdktf synth --hcl fails to render false(bool), 0(int), ""(empty string) #3907

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 24 additions & 9 deletions packages/cdktf/lib/hcl/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,15 +600,24 @@ export function renderAttributes(attributes: any): string {
//
// We might have some attributes that don't have type information
// just try to guess them

// Handle primitive values and falsy values explicitly
if (v === undefined) {
return undefined;
}
if (v === null) {
return `${name} = null`;
}
if (typeof v === "boolean") {
return `${name} = ${v}`;
}
if (typeof v === "string" || typeof v === "number") {
return `${name} = ${renderFuzzyJsonExpression(v)}`;
} else if (typeof v === "boolean") {
return `${name} = ${v}`;
} else if (Array.isArray(v)) {
}
if (Array.isArray(v)) {
return `${name} = ${renderFuzzyJsonExpression(v)}`;
} else if (v === null) {
return `${name} = null`;
} else if (
}
if (
typeof v === "object" &&
// eslint-disable-next-line no-prototype-builtins
!v.hasOwnProperty("value") &&
Expand All @@ -621,9 +630,7 @@ ${renderSimpleAttributes(v)}
}`;
}
return `${name} = ${renderFuzzyJsonExpression(v)}`;
} else if (v === undefined) {
return undefined;
}
}

// Referencing both isBlock and is_block, because sometimes we pass through a snake case filter
// within attributes.
Expand Down Expand Up @@ -677,6 +684,14 @@ ${renderAttributes(value)}
// In either case, we should try to not output [object Object] here
// and try a best approximation. Though, it would not work for
// blocks
if (
typeof value === "string" ||
typeof value === "number" ||
typeof value === "boolean" ||
value === null
) {
return `${name} = ${renderFuzzyJsonExpression(value)}`;
}
if (typeof value === "object") {
return `
# Warning: The following attribute is of an unexpected type. Either there's a problem with the provider
Expand Down
136 changes: 136 additions & 0 deletions packages/cdktf/test/json-to-hcl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,23 @@ describe("output", () => {
`);
});

test("sensitive output false", async () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");

new TerraformOutput(stack, "test-output", {
value: 1,
sensitive: false,
});
expect(Testing.synthHcl(stack)).toMatchInlineSnapshot(`
"

output "test-output" {
value = 1
sensitive = false
}"
`);
});
test("map keys with invalid identifier chars", async () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");
Expand Down Expand Up @@ -1099,6 +1116,125 @@ test("string type", () => {
`);
});

test("string variable default", () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");

new TerraformVariable(stack, "test-variable", {
type: "string",
default: false
});
const hcl = Testing.synthHcl(stack);
expect(hcl).toContain('default = false');
expect(Testing.synthHcl(stack)).toMatchInlineSnapshot(`
"

variable "test-variable" {
type = string
default = false
}"
`);
});

test("bool variable default", () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");

new TerraformVariable(stack, "test-variable", {
type: "bool",
default: false
});
const hcl = Testing.synthHcl(stack);
expect(hcl).toContain("default = false"); // assume CDKTF standard output formatting has space around the equals sign.
expect(Testing.synthHcl(stack)).toMatchInlineSnapshot(`
"

variable "test-variable" {
type = bool
default = false
}"
`);
});

test("string variable sensitive", () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");

new TerraformVariable(stack, "test-variable", {
type: "string",
sensitive: false
});
const hcl = Testing.synthHcl(stack);
expect(hcl).toContain("sensitive = false");
expect(Testing.synthHcl(stack)).toMatchInlineSnapshot(`
"

variable "test-variable" {
type = string
sensitive = false
}"
`);
});

test("number variable default zero", () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");

const hcl = Testing.synthHcl(stack);
expect(hcl).toContain("default = 0");
new TerraformVariable(stack, "test-variable", {
type: "number",
default: 0
});
expect(Testing.synthHcl(stack)).toMatchInlineSnapshot(`
"

variable "test-variable" {
type = number
default = 0
}"
`);
});

test("string variable default empty", () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");

new TerraformVariable(stack, "test-variable", {
type: "string",
default: ""
});
const hcl = Testing.synthHcl(stack);
expect(hcl).toContain('default = ""');
expect(Testing.synthHcl(stack)).toMatchInlineSnapshot(`
"

variable "test-variable" {
type = string
default = ""
}"
`);
});

test("variable default null", () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");

new TerraformVariable(stack, "test-variable", {
type: "string",
default: null,
});
const hcl = Testing.synthHcl(stack);
expect(hcl).toContain('default = null');
expect(Testing.synthHcl(stack)).toMatchInlineSnapshot(`
"
variable "test-variable" {
type = string
default = null
}"
`);
});

test("number type", () => {
const app = Testing.app();
const stack = new TerraformStack(app, "test");
Expand Down