fix(hcl): coerce non-string object keys to strings instead of panicking (#2795)

HCL object keys may be non-string literals (e.g. a number like 1). The
decoder called cty.Value.AsString on the evaluated key, which panics with
'not a string' for any non-string key type. Mirror OpenTofu/Terragrunt by
silently converting non-string keys to their string representation via
cty/convert. Add decode scenarios covering numeric and boolean keys.
This commit is contained in:
Pablo Garcia
2026-08-07 14:53:18 +02:00
parent 7862131c9c
commit 55030eaa86
2 changed files with 44 additions and 1 deletions
+14
View File
@@ -472,6 +472,20 @@ var hclFormatScenarios = []formatScenario{
expected: "service {\n optional_field = null\n}\n",
scenarioType: "roundtrip",
},
{
description: "Non-string object keys are coerced to strings",
skipDoc: true,
input: `intdict = { 1 = {} }`,
expected: "intdict: {\"1\": {}}\n",
scenarioType: "decode",
},
{
description: "Mixed non-string object keys are coerced to strings",
skipDoc: true,
input: `d = { 1 = "a", 2 = "b", true = "c" }`,
expected: "d: {\"1\": \"a\", \"2\": \"b\", \"true\": \"c\"}\n",
scenarioType: "decode",
},
}
func testHclScenario(t *testing.T, s formatScenario) {