From 96224746a3f77ee8bcc5d8cbd87df5fcba695941 Mon Sep 17 00:00:00 2001 From: Saloni Date: Tue, 9 Jul 2024 20:06:59 +0900 Subject: [PATCH 1/2] bind: Maintain backwards compatibility for map[string]interface{} binding --- bind.go | 6 ++++++ bind_test.go | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/bind.go b/bind.go index 507def3ea..8cb352b35 100644 --- a/bind.go +++ b/bind.go @@ -159,6 +159,12 @@ 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 { + if len(v) == 1 { + val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0])) + } else { + val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v)) + } } else { val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v)) } diff --git a/bind_test.go b/bind_test.go index db7693cb9..5051262f8 100644 --- a/bind_test.go +++ b/bind_test.go @@ -498,7 +498,7 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) { assert.Equal(t, map[string]interface{}{ "multiple": []string{"1", "2"}, - "single": []string{"3"}, + "single": "3", }, dest, ) @@ -510,7 +510,7 @@ func TestDefaultBinder_bindDataToMap(t *testing.T) { assert.Equal(t, map[string]interface{}{ "multiple": []string{"1", "2"}, - "single": []string{"3"}, + "single": "3", }, dest, ) From 62fe8cdeb7f6eadb50d470e88aea966a5653f590 Mon Sep 17 00:00:00 2001 From: Saloni Date: Fri, 19 Jul 2024 18:39:21 +0900 Subject: [PATCH 2/2] bind to single string for map[string]interface{}{} --- bind.go | 8 +++----- bind_test.go | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/bind.go b/bind.go index 8cb352b35..877c0f5c4 100644 --- a/bind.go +++ b/bind.go @@ -160,11 +160,9 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri if isElemString { val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0])) } else if isElemInterface { - if len(v) == 1 { - val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v[0])) - } else { - val.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v)) - } + // 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 5051262f8..fc0c00598 100644 --- a/bind_test.go +++ b/bind_test.go @@ -497,7 +497,7 @@ 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"}, + "multiple": "1", "single": "3", }, dest, @@ -509,7 +509,7 @@ 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"}, + "multiple": "1", "single": "3", }, dest,