From f13e2640f0eacdbe1bffc076c25126368d6445d2 Mon Sep 17 00:00:00 2001 From: Saloni Agarwal <46742197+thesaltree@users.noreply.github.com> Date: Mon, 22 Jul 2024 13:33:01 +0900 Subject: [PATCH] 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{}{} --- bind.go | 4 ++++ bind_test.go | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/bind.go b/bind.go index 507def3e..877c0f5c 100644 --- a/bind.go +++ b/bind.go @@ -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)) } diff --git a/bind_test.go b/bind_test.go index db7693cb..fc0c0059 100644 --- a/bind_test.go +++ b/bind_test.go @@ -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, ) @@ -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, )