From 3726e28902f65dc671f31bdc6552ae705136835f Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 7 Aug 2025 13:01:33 +0200 Subject: [PATCH 1/3] fix: return empty array if no option multi-selected --- cli/cliui/select.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/cliui/select.go b/cli/cliui/select.go index b3222cbbf3a71..80a5b663abfb1 100644 --- a/cli/cliui/select.go +++ b/cli/cliui/select.go @@ -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 marshalled + // into JSON. Otherwise, `var results []string` would be marshalled to "null". + // See: https://github.com/golang/go/issues/27589 + results := []string{} for _, sel := range selected { custom := true for i, option := range richOptions.Options { From 7943dc1b23c2ba87fcd4177a8d502682019aa3fd Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 7 Aug 2025 13:21:43 +0200 Subject: [PATCH 2/3] add unit test --- cli/cliui/select.go | 4 ++-- cli/cliui/select_test.go | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/cli/cliui/select.go b/cli/cliui/select.go index 80a5b663abfb1..f609ca81c3e26 100644 --- a/cli/cliui/select.go +++ b/cli/cliui/select.go @@ -350,8 +350,8 @@ func RichMultiSelect(inv *serpent.Invocation, richOptions RichMultiSelectOptions // Check selected option, convert descriptions (line) to values // - // The function must return an initialized empty array, since it is later marshalled - // into JSON. Otherwise, `var results []string` would be marshalled to "null". + // 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 { diff --git a/cli/cliui/select_test.go b/cli/cliui/select_test.go index 21fc4cb03c398..51f85f86d0cd0 100644 --- a/cli/cliui/select_test.go +++ b/cli/cliui/select_test.go @@ -111,6 +111,17 @@ func TestRichMultiSelect(t *testing.T) { allowCustom: true, want: []string{"aaa", "bbb"}, }, + { + name: "Empty", + 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 { From 8f49e09566b3509ca6f0d0e5f2bc7b61f1369f5a Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Thu, 7 Aug 2025 13:22:47 +0200 Subject: [PATCH 3/3] fix --- cli/cliui/select_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/cliui/select_test.go b/cli/cliui/select_test.go index 51f85f86d0cd0..55ab81f50f01b 100644 --- a/cli/cliui/select_test.go +++ b/cli/cliui/select_test.go @@ -112,7 +112,7 @@ func TestRichMultiSelect(t *testing.T) { want: []string{"aaa", "bbb"}, }, { - name: "Empty", + name: "NoOptionSelected", options: []codersdk.TemplateVersionParameterOption{ {Name: "AAA", Description: "This is AAA", Value: "aaa"}, {Name: "BBB", Description: "This is BBB", Value: "bbb"},