Skip to content

Commit

Permalink
bind: Maintain backwards compatibility for map[string]interface{} bin…
Browse files Browse the repository at this point in the history
…ding (#2656)

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

* bind to single string for map[string]interface{}{}
  • Loading branch information
thesaltree authored Jul 22, 2024
1 parent f7d9f51 commit f13e264
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
for k, v := range data {
if isElemString {
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 {
val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v))
}
Expand Down
8 changes: 4 additions & 4 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,8 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t,
map[string]interface{}{
"multiple": []string{"1", "2"},
"single": []string{"3"},
"multiple": "1",
"single": "3",
},
dest,
)
Expand All @@ -509,8 +509,8 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) {
assert.NoError(t, new(DefaultBinder).bindData(&dest, exampleData, "param"))
assert.Equal(t,
map[string]interface{}{
"multiple": []string{"1", "2"},
"single": []string{"3"},
"multiple": "1",
"single": "3",
},
dest,
)
Expand Down

0 comments on commit f13e264

Please sign in to comment.