fix: return empty array if no option multi-selected (#19224)

Related: https://github.com/coder/coder/issues/19145
This commit is contained in:
Marcin Tojek
2025-08-07 13:37:58 +02:00
committed by GitHub
parent 91780db1fe
commit 2851d9f3ea
2 changed files with 16 additions and 1 deletions
+5 -1
View File
@@ -349,7 +349,11 @@ func RichMultiSelect(inv *serpent.Invocation, richOptions RichMultiSelectOptions
}
// Check selected option, convert descriptions (line) to values
var results []string
//
// The function must return an initialized empty array, since it is later marshaled
// into JSON. Otherwise, `var results []string` would be marshaled to "null".
// See: https://github.com/golang/go/issues/27589
results := []string{}
for _, sel := range selected {
custom := true
for i, option := range richOptions.Options {
+11
View File
@@ -111,6 +111,17 @@ func TestRichMultiSelect(t *testing.T) {
allowCustom: true,
want: []string{"aaa", "bbb"},
},
{
name: "NoOptionSelected",
options: []codersdk.TemplateVersionParameterOption{
{Name: "AAA", Description: "This is AAA", Value: "aaa"},
{Name: "BBB", Description: "This is BBB", Value: "bbb"},
{Name: "CCC", Description: "This is CCC", Value: "ccc"},
},
defaults: []string{},
allowCustom: false,
want: []string{},
},
}
for _, tt := range tests {