diff --git a/bind.go b/bind.go index 0e5ddd469..aa669777f 100644 --- a/bind.go +++ b/bind.go @@ -235,7 +235,7 @@ func bindData(destination any, data map[string][]string, tag string, dataFiles m isElemString := k == reflect.String isElemSliceOfStrings := k == reflect.Slice && typ.Elem().Elem().Kind() == reflect.String if !isElemSliceOfStrings && !isElemString && !isElemInterface { - return nil + return errors.New("binding element must be a map with string, []string or any values") } if val.IsNil() { val.Set(reflect.MakeMap(typ)) diff --git a/bind_map_test.go b/bind_map_test.go new file mode 100644 index 000000000..1b1a03eb1 --- /dev/null +++ b/bind_map_test.go @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: MIT +// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors + +package echo + +import ( + "net/http" + "net/http/httptest" + "reflect" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestBindQueryParamsSupportedMapValueTypes(t *testing.T) { + e := New() + req := httptest.NewRequest(http.MethodGet, "/?a=1&a=2&b=3", nil) + + t.Run("map[string]string", func(t *testing.T) { + c := e.NewContext(req, httptest.NewRecorder()) + dest := map[string]string{} + require.NoError(t, BindQueryParams(c, &dest)) + assert.Equal(t, map[string]string{"a": "1", "b": "3"}, dest) + }) + + t.Run("map[string][]string", func(t *testing.T) { + c := e.NewContext(req, httptest.NewRecorder()) + dest := map[string][]string{} + require.NoError(t, BindQueryParams(c, &dest)) + assert.Equal(t, map[string][]string{"a": {"1", "2"}, "b": {"3"}}, dest) + }) + + t.Run("map[string]any", func(t *testing.T) { + c := e.NewContext(req, httptest.NewRecorder()) + dest := map[string]any{} + require.NoError(t, BindQueryParams(c, &dest)) + assert.Equal(t, map[string]any{"a": "1", "b": "3"}, dest) + }) +} + +func TestBindQueryParamsUnsupportedMapValueTypes(t *testing.T) { + e := New() + req := httptest.NewRequest(http.MethodGet, "/?a=1&a=2", nil) + + tests := []struct { + name string + target any + expected any + }{ + { + name: "map[string]int", + target: &map[string]int{}, + expected: map[string]int{}, + }, + { + name: "map[string][]int", + target: &map[string][]int{}, + expected: map[string][]int{}, + }, + { + name: "map[string]bool", + target: &map[string]bool{}, + expected: map[string]bool{}, + }, + { + name: "nil map[string]int", + target: func() *map[string]int { + var dest map[string]int + return &dest + }(), + expected: map[string]int(nil), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := e.NewContext(req, httptest.NewRecorder()) + + err := BindQueryParams(c, tt.target) + require.Error(t, err) + assert.Equal(t, http.StatusBadRequest, StatusCode(err)) + assert.Equal(t, tt.expected, reflect.ValueOf(tt.target).Elem().Interface()) + }) + } +} + +func TestBindHeadersUnsupportedMapValueTypes(t *testing.T) { + e := New() + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.Header.Set("A", "1") + c := e.NewContext(req, httptest.NewRecorder()) + + var dest map[string]int + err := BindHeaders(c, &dest) + require.Error(t, err) + assert.Equal(t, http.StatusBadRequest, StatusCode(err)) + assert.Nil(t, dest) +} diff --git a/bind_test.go b/bind_test.go index c1ef54435..2fee64239 100644 --- a/bind_test.go +++ b/bind_test.go @@ -517,27 +517,31 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) { ) }) - t.Run("ok, bind to map[string]int skips", func(t *testing.T) { + t.Run("nok, bind to map[string]int", func(t *testing.T) { dest := map[string]int{} - assert.NoError(t, bindData(&dest, exampleData, "param", nil)) + err := bindData(&dest, exampleData, "param", nil) + assert.EqualError(t, err, "binding element must be a map with string, []string or any values") assert.Equal(t, map[string]int{}, dest) }) - t.Run("ok, bind to map[string]int skips with nil map", func(t *testing.T) { + t.Run("nok, bind to map[string]int with nil map", func(t *testing.T) { var dest map[string]int - assert.NoError(t, bindData(&dest, exampleData, "param", nil)) + err := bindData(&dest, exampleData, "param", nil) + assert.EqualError(t, err, "binding element must be a map with string, []string or any values") assert.Equal(t, map[string]int(nil), dest) }) - t.Run("ok, bind to map[string][]int skips", func(t *testing.T) { + t.Run("nok, bind to map[string][]int", func(t *testing.T) { dest := map[string][]int{} - assert.NoError(t, bindData(&dest, exampleData, "param", nil)) + err := bindData(&dest, exampleData, "param", nil) + assert.EqualError(t, err, "binding element must be a map with string, []string or any values") assert.Equal(t, map[string][]int{}, dest) }) - t.Run("ok, bind to map[string][]int skips with nil map", func(t *testing.T) { + t.Run("nok, bind to map[string][]int with nil map", func(t *testing.T) { var dest map[string][]int - assert.NoError(t, bindData(&dest, exampleData, "param", nil)) + err := bindData(&dest, exampleData, "param", nil) + assert.EqualError(t, err, "binding element must be a map with string, []string or any values") assert.Equal(t, map[string][]int(nil), dest) }) }