Skip to content

Commit 77c41dd

Browse files
Allow nil interfaces in template context
It was discovered during testing that an empty interface (which could be encoded via 'null' in YAML) would cause a panic. This should be an allowed type, basically like a primitive. Refs: - https://issues.redhat.com/browse/ACM-20863 Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
1 parent 73dd6f9 commit 77c41dd

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

pkg/templates/templates.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ func isPrimitive(kind reflect.Kind) bool {
414414

415415
func getValidContextHelper(value any) error {
416416
f := reflect.TypeOf(value)
417+
if f == nil { // nil interface value.
418+
return nil
419+
}
417420

418421
// Allow primitive types (excludes complex numbers)
419422
if isPrimitive(f.Kind()) {

pkg/templates/templates_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,16 @@ func TestResolveTemplateWithContext(t *testing.T) {
962962
},
963963
expectedResult: "value: world spacename",
964964
},
965+
"nested_empty_interface": {
966+
inputTmpl: `value: '{{ .Foo.bar }} {{ .Foo.other }}'`,
967+
ctx: struct{ Foo map[string]interface{} }{
968+
Foo: map[string]interface{}{
969+
"bar": "hello",
970+
"other": nil, // this can occur when YAML has 'null' fields
971+
},
972+
},
973+
expectedResult: "value: hello <no value>",
974+
},
965975
}
966976

967977
for testName, test := range testcases {

0 commit comments

Comments
 (0)