From 855708e6f52bc7252a71bae36102f26b8b720b94 Mon Sep 17 00:00:00 2001 From: Yufeng He <40085740+he-yufeng@users.noreply.github.com> Date: Mon, 22 Jun 2026 17:26:48 +0800 Subject: [PATCH] fix: pass original value to ParseTypeError in ArrayParameter and MapParameter After a failed two-result type assertion in Go, the LHS variable is set to the zero value for its type (nil for slices and maps). Both ArrayParameter.Parse and MapParameter.Parse were passing this zero value to ParseTypeError instead of the original `v`, causing error messages to always report the zero value rather than the actual bad input. All five other Parse methods in the same file (String, Int, Float, Bool, and the integer sub-case) correctly pass `v`. This commit aligns ArrayParameter and MapParameter with that pattern and adds regression tests that verify the original value appears in the error text. --- internal/util/parameters/parameters.go | 4 +-- internal/util/parameters/parameters_test.go | 28 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/internal/util/parameters/parameters.go b/internal/util/parameters/parameters.go index f399cca90c6e..ce2b649a5189 100644 --- a/internal/util/parameters/parameters.go +++ b/internal/util/parameters/parameters.go @@ -1349,7 +1349,7 @@ func (p *ArrayParameter) IsExcludedValues(v []any) bool { func (p *ArrayParameter) Parse(v any) (any, error) { arrVal, ok := v.([]any) if !ok { - return nil, &ParseTypeError{p.Name, p.Type, arrVal} + return nil, &ParseTypeError{p.Name, p.Type, v} } if !p.IsAllowedValues(arrVal) { return nil, fmt.Errorf("%s is not an allowed value", arrVal) @@ -1574,7 +1574,7 @@ func (p *MapParameter) IsExcludedValues(v map[string]any) bool { func (p *MapParameter) Parse(v any) (any, error) { m, ok := v.(map[string]any) if !ok { - return nil, &ParseTypeError{p.Name, p.Type, m} + return nil, &ParseTypeError{p.Name, p.Type, v} } if !p.IsAllowedValues(m) { return nil, fmt.Errorf("%s is not an allowed value", m) diff --git a/internal/util/parameters/parameters_test.go b/internal/util/parameters/parameters_test.go index a84d0f27ec13..249a049b482a 100644 --- a/internal/util/parameters/parameters_test.go +++ b/internal/util/parameters/parameters_test.go @@ -2324,3 +2324,31 @@ func TestCheckParamRequired(t *testing.T) { }) } } + +// TestParseTypeErrorIncludesValue verifies that ArrayParameter.Parse and +// MapParameter.Parse report the original input value in the error message, not +// the zero value of the assertion target (nil) that is produced when a Go +// two-result type assertion fails. +func TestParseTypeErrorIncludesValue(t *testing.T) { + t.Run("ArrayParameter", func(t *testing.T) { + p := parameters.NewArrayParameter("arr", "test array", parameters.NewStringParameter("s", "item")) + _, err := p.Parse("not-an-array") + if err == nil { + t.Fatal("expected an error for wrong type, got nil") + } + if !strings.Contains(err.Error(), "not-an-array") { + t.Errorf("error should include the original value; got: %q", err.Error()) + } + }) + + t.Run("MapParameter", func(t *testing.T) { + p := parameters.NewMapParameter("m", "test map", "string") + _, err := p.Parse("bad") + if err == nil { + t.Fatal("expected an error for wrong type, got nil") + } + if !strings.Contains(err.Error(), "bad") { + t.Errorf("error should include the original value; got: %q", err.Error()) + } + }) +}