Return error when the template's single interpolation results in null value (#36658)

sams/inverse-target^2
Samsondeen 1 year ago committed by GitHub
parent 9ac4b3a62b
commit 08fa94c2ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
kind: BUG FIXES
body: Return error when the templatestring function contains only a single interpolation that evaluates to a null value
time: 2025-03-10T09:31:53.479704+01:00
custom:
Issue: "36652"

@ -380,6 +380,14 @@ func makeRenderTemplateFunc(funcsCb func() (funcs map[string]function.Function,
if diags.HasErrors() {
return cty.DynamicVal, diags
}
if val.IsNull() {
return cty.DynamicVal, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Template result is null",
Detail: "The result of the template is null, which is not a valid result for a templatestring call.",
Subject: expr.Range().Ptr(),
}
}
return val, nil
}
}

@ -273,6 +273,28 @@ func TestTemplateString(t *testing.T) {
want cty.Value
wantErr string
}{
{ // a single string interpolation that evaluates to null should fail
`template`,
map[string]cty.Value{
"template": cty.StringVal(`${test}`),
},
cty.ObjectVal(map[string]cty.Value{
"test": cty.NullVal(cty.String),
}),
cty.NilVal,
`<templatestring argument>:1,1-8: Template result is null; The result of the template is null, which is not a valid result for a templatestring call.`,
},
{ // a single string interpolation that evaluates to unknown should not fail
`template`,
map[string]cty.Value{
"template": cty.StringVal(`${test}`),
},
cty.ObjectVal(map[string]cty.Value{
"test": cty.UnknownVal(cty.String),
}),
cty.UnknownVal(cty.String).RefineNotNull(),
``,
},
{
`template`,
map[string]cty.Value{

Loading…
Cancel
Save