1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-01 22:09:21 +02:00

bind: Maintain backwards compatibility for map[string]interface{} binding (#2656)

* bind: Maintain backwards compatibility for map[string]interface{} binding

* bind to single string for map[string]interface{}{}
This commit is contained in:
Saloni Agarwal 2024-07-22 13:33:01 +09:00 committed by GitHub
parent f7d9f5142e
commit f13e2640f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 4 deletions

View File

@ -159,6 +159,10 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
for k, v := range data { for k, v := range data {
if isElemString { if isElemString {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0])) val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
} else if isElemInterface {
// To maintain backward compatibility, we always bind to the first string value
// and not the slice of strings when dealing with map[string]interface{}{}
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
} else { } else {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v)) val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
} }

View File

@ -497,8 +497,8 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param")) assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t, assert.Equal(t,
map[string]interface{}{ map[string]interface{}{
"multiple": []string{"1", "2"}, "multiple": "1",
"single": []string{"3"}, "single": "3",
}, },
dest, dest,
) )
@ -509,8 +509,8 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param")) assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t, assert.Equal(t,
map[string]interface{}{ map[string]interface{}{
"multiple": []string{"1", "2"}, "multiple": "1",
"single": []string{"3"}, "single": "3",
}, },
dest, dest,
) )