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
2 changes: 1 addition & 1 deletion bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
99 changes: 99 additions & 0 deletions bind_map_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
20 changes: 12 additions & 8 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down