Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/util/parameters/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions internal/util/parameters/parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
})
}