Skip to content

Commit 2851d9f

Browse files
authored
fix: return empty array if no option multi-selected (#19224)
Related: #19145
1 parent 91780db commit 2851d9f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

cli/cliui/select.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,11 @@ func RichMultiSelect(inv *serpent.Invocation, richOptions RichMultiSelectOptions
349349
}
350350

351351
// Check selected option, convert descriptions (line) to values
352-
var results []string
352+
//
353+
// The function must return an initialized empty array, since it is later marshaled
354+
// into JSON. Otherwise, `var results []string` would be marshaled to "null".
355+
// See: https://github.com/golang/go/issues/27589
356+
results := []string{}
353357
for _, sel := range selected {
354358
custom := true
355359
for i, option := range richOptions.Options {

cli/cliui/select_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ func TestRichMultiSelect(t *testing.T) {
111111
allowCustom: true,
112112
want: []string{"aaa", "bbb"},
113113
},
114+
{
115+
name: "NoOptionSelected",
116+
options: []codersdk.TemplateVersionParameterOption{
117+
{Name: "AAA", Description: "This is AAA", Value: "aaa"},
118+
{Name: "BBB", Description: "This is BBB", Value: "bbb"},
119+
{Name: "CCC", Description: "This is CCC", Value: "ccc"},
120+
},
121+
defaults: []string{},
122+
allowCustom: false,
123+
want: []string{},
124+
},
114125
}
115126

116127
for _, tt := range tests {

0 commit comments

Comments
 (0)