Skip to content

Commit f13e264

Browse files
authored
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{}{}
1 parent f7d9f51 commit f13e264

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

bind.go

+4
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
159159
for k, v := range data {
160160
if isElemString {
161161
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
162+
} else if isElemInterface {
163+
// To maintain backward compatibility, we always bind to the first string value
164+
// and not the slice of strings when dealing with map[string]interface{}{}
165+
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0]))
162166
} else {
163167
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
164168
}

bind_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,8 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
497497
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
498498
assert.Equal(t,
499499
map[string]interface{}{
500-
"multiple": []string{"1", "2"},
501-
"single": []string{"3"},
500+
"multiple": "1",
501+
"single": "3",
502502
},
503503
dest,
504504
)
@@ -509,8 +509,8 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
509509
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
510510
assert.Equal(t,
511511
map[string]interface{}{
512-
"multiple": []string{"1", "2"},
513-
"single": []string{"3"},
512+
"multiple": "1",
513+
"single": "3",
514514
},
515515
dest,
516516
)

0 commit comments

Comments
 (0)