Skip to content

Commit

Permalink
stamper: preserve empty array when stamping (#993)
Browse files Browse the repository at this point in the history
when performing the recursive evaluation of the objects we stamp when
using `spec.template: {...}`, we used to always have a nil var that
would be instantiated when appending to it, but given that with 0-sized
slices we wouldn't get into the loop and thus never initialize, we'd
effectively always be transformating `[]` into `nil`.

`[]` -> `nil` is problematic specially when using `Runnable` with Tekton
as parameters that should be of type `array` but are initialized with
`[]` from the Runnable side would end up being transformed into `nil`
which is badly handled by tekton (it transforms `nil` -> `""`, which
goes against the validation of a task that expects an array)

Signed-off-by: Ciro S. Costa <[email protected]>
  • Loading branch information
Ciro S. Costa authored Aug 24, 2022
1 parent 2ee79e6 commit 67ceb63
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/templates/stamper.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ func (s *Stamper) recursivelyEvaluateTemplates(jsonValue interface{}, loopDetect
}
return stampedMap, nil
case []interface{}:
if len(typedJSONValue) == 0 {
return typedJSONValue, nil
}

var stampedSlice []interface{}
for _, sliceElement := range typedJSONValue {
stampedElement, err := s.recursivelyEvaluateTemplates(sliceElement, loopDetector)
Expand Down
3 changes: 3 additions & 0 deletions pkg/templates/stamper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ var _ = Describe("Stamper", func() {
Entry(`Single tag, array value and type preserved, nested tags evaluated`,
`$(params.sub)$`, `["foo", "$(params['extra-for-nested'])$"]`, []interface{}{"foo", "nested"}, ""),

Entry(`Single tag, empty array value preserved`,
`$(params.sub)$`, `["foo", []]`, []interface{}{"foo", []interface{}{}}, ""),

Entry(`Multiple tags, result becomes a string`,
`$(params.sub)$$(params.sub)$`, `5`, "55", ""),

Expand Down

0 comments on commit 67ceb63

Please sign in to comment.