From f18334691da9a1829bfe09f641a5a2831057d9a9 Mon Sep 17 00:00:00 2001 From: Chuang Wang Date: Mon, 18 Jul 2022 13:53:27 -0700 Subject: [PATCH] Fix the issue with empty array replacement Prior to this change, if there is only one element in an array that is a reference to an empty array, the original array becomes nil after replacement, but it should be an empty array instead of nil. Fixes https://github.com/tektoncd/pipeline/issues/5149 --- pkg/apis/pipeline/v1beta1/param_types.go | 2 +- pkg/apis/pipeline/v1beta1/param_types_test.go | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/apis/pipeline/v1beta1/param_types.go b/pkg/apis/pipeline/v1beta1/param_types.go index 8489c95b295..23e71a52ef4 100644 --- a/pkg/apis/pipeline/v1beta1/param_types.go +++ b/pkg/apis/pipeline/v1beta1/param_types.go @@ -198,7 +198,7 @@ func (arrayOrString ArrayOrString) MarshalJSON() ([]byte, error) { func (arrayOrString *ArrayOrString) ApplyReplacements(stringReplacements map[string]string, arrayReplacements map[string][]string, objectReplacements map[string]map[string]string) { switch arrayOrString.Type { case ParamTypeArray: - var newArrayVal []string + newArrayVal := []string{} for _, v := range arrayOrString.ArrayVal { newArrayVal = append(newArrayVal, substitution.ApplyArrayReplacements(v, stringReplacements, arrayReplacements)...) } diff --git a/pkg/apis/pipeline/v1beta1/param_types_test.go b/pkg/apis/pipeline/v1beta1/param_types_test.go index 2a8f0d0b2d7..fe437e42cad 100644 --- a/pkg/apis/pipeline/v1beta1/param_types_test.go +++ b/pkg/apis/pipeline/v1beta1/param_types_test.go @@ -209,7 +209,14 @@ func TestArrayOrString_ApplyReplacements(t *testing.T) { }, expectedOutput: v1beta1.NewArrayOrString("firstvalue", "array", "value", "lastvalue", "asdf", "sdfsd"), }, { - name: "empty array replacement", + name: "empty array replacement without extra elements", + args: args{ + input: v1beta1.NewArrayOrString("$(arraykey)"), + arrayReplacements: map[string][]string{"arraykey": {}}, + }, + expectedOutput: &v1beta1.ArrayOrString{Type: v1beta1.ParamTypeArray, ArrayVal: []string{}}, + }, { + name: "empty array replacement with extra elements", args: args{ input: v1beta1.NewArrayOrString("firstvalue", "$(arraykey)", "lastvalue"), stringReplacements: map[string]string{"some": "value", "anotherkey": "value"},