From 6ba78a42eb7513a794f360b5da2ef01fc4b086f3 Mon Sep 17 00:00:00 2001 From: Leul Tewodros Agonafer <107800362+LeulTew@users.noreply.github.com> Date: Fri, 18 Sep 2026 08:31:31 +0300 Subject: [PATCH] fix(binding): initialize nil map destinations --- binding/binding_test.go | 69 ++++++++++++++++++++++++++++++++++++ binding/form_mapping.go | 8 +++++ binding/form_mapping_test.go | 45 +++++++++++++++++++++++ 3 files changed, 122 insertions(+) diff --git a/binding/binding_test.go b/binding/binding_test.go index f90488cd..bd622d9a 100644 --- a/binding/binding_test.go +++ b/binding/binding_test.go @@ -11,6 +11,7 @@ import ( "io" "mime/multipart" "net/http" + "net/url" "os" "reflect" "strconv" @@ -451,6 +452,74 @@ func TestBindingQueryStringMap(t *testing.T) { assert.Equal(t, "world", obj["hello"]) } +func TestBindingMapPointer(t *testing.T) { + const data = "foo=first&foo=last&empty=" + for _, b := range []struct { + binding Binding + path string + body string + }{ + {Query, "/?" + data, ""}, + {Form, "/", data}, + {FormPost, "/", data}, + } { + t.Run(b.binding.Name(), func(t *testing.T) { + tests := []struct { + name string + obj any + want any + }{ + { + "nil string map", + new(map[string]string), + &map[string]string{"foo": "last", "empty": ""}, + }, + { + "initialized string map", + &map[string]string{"foo": "stale", "keep": "value"}, + &map[string]string{"foo": "last", "empty": "", "keep": "value"}, + }, + { + "nil string slice map", + new(map[string][]string), + &map[string][]string{"foo": {"first", "last"}, "empty": {""}}, + }, + { + "initialized string slice map", + &map[string][]string{"foo": {"stale"}, "keep": {"value"}}, + &map[string][]string{"foo": {"first", "last"}, "empty": {""}, "keep": {"value"}}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := requestWithBody(http.MethodPost, b.path, b.body) + req.Header.Set("Content-Type", MIMEPOSTForm) + require.NoError(t, req.ParseForm()) + source := req.Form + var err error + require.NotPanics(t, func() { + err = b.binding.Bind(req, tt.obj) + }) + require.NoError(t, err) + assert.Equal(t, tt.want, tt.obj) + assert.Equal(t, url.Values{"foo": {"first", "last"}, "empty": {""}}, source) + }) + } + }) + } +} + +func TestBindingUriMapPointer(t *testing.T) { + form := map[string][]string{"foo": {"first", "last"}, "empty": {""}} + var stringMap map[string]string + require.NoError(t, Uri.BindUri(form, &stringMap)) + assert.Equal(t, map[string]string{"foo": "last", "empty": ""}, stringMap) + + var sliceMap map[string][]string + require.NoError(t, Uri.BindUri(form, &sliceMap)) + assert.Equal(t, form, sliceMap) +} + func TestBindingXML(t *testing.T) { testBodyBinding(t, XML, "xml", diff --git a/binding/form_mapping.go b/binding/form_mapping.go index 6982fd4f..162347fe 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -53,6 +53,14 @@ func mapFormByTag(ptr any, form map[string][]string, tag string) error { } if ptrVal.Kind() == reflect.Map && ptrVal.Type().Key().Kind() == reflect.String { + if ptrVal.IsNil() && ptrVal.CanSet() && len(form) > 0 { + m := reflect.MakeMap(ptrVal.Type()) + if err := setFormMap(m.Interface(), form); err != nil { + return err + } + ptrVal.Set(m) + return nil + } if pointed != nil { ptr = pointed } diff --git a/binding/form_mapping_test.go b/binding/form_mapping_test.go index c78f7398..5f545c83 100644 --- a/binding/form_mapping_test.go +++ b/binding/form_mapping_test.go @@ -182,6 +182,51 @@ func TestMapFormWithTag(t *testing.T) { assert.Equal(t, 6, s.F) } +func TestMapFormWithTagMapPointer(t *testing.T) { + type namedMap map[string]string + type namedSliceMap map[string][]string + form := map[string][]string{"foo": {"first", "last"}, "empty": {""}} + tests := []struct { + name string + obj any + form map[string][]string + want any + err error + }{ + {"absent string map", new(map[string]string), nil, new(map[string]string), nil}, + {"absent string slice map", new(map[string][]string), nil, new(map[string][]string), nil}, + {"empty string map", new(map[string]string), map[string][]string{}, new(map[string]string), nil}, + {"empty string slice map", new(map[string][]string), map[string][]string{}, new(map[string][]string), nil}, + {"nil string map", new(map[string]string), form, &map[string]string{"foo": "last", "empty": ""}, nil}, + {"nil string slice map", new(map[string][]string), form, + &map[string][]string{"foo": {"first", "last"}, "empty": {""}}, nil}, + {"unsupported value", new(map[string]int), form, new(map[string]int), ErrConvertToMapString}, + {"unsupported slice value", new(map[string][]int), form, new(map[string][]int), ErrConvertMapStringSlice}, + {"unsupported named map", new(namedMap), form, new(namedMap), ErrConvertToMapString}, + {"unsupported named slice map", new(namedSliceMap), form, new(namedSliceMap), ErrConvertMapStringSlice}, + {"string map by value", map[string]string{"foo": "stale", "keep": "value"}, form, + map[string]string{"foo": "last", "empty": "", "keep": "value"}, nil}, + {"string slice map by value", map[string][]string{"foo": {"stale"}, "keep": {"value"}}, form, + map[string][]string{"foo": {"first", "last"}, "empty": {""}, "keep": {"value"}}, nil}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var err error + require.NotPanics(t, func() { + err = MapFormWithTag(tt.obj, tt.form, "externalTag") + }) + if tt.err != nil { + require.ErrorIs(t, err, tt.err) + assert.Same(t, tt.err, err) + } else { + require.NoError(t, err) + } + assert.Equal(t, tt.want, tt.obj) + assert.Equal(t, map[string][]string{"foo": {"first", "last"}, "empty": {""}}, form) + }) + } +} + func TestMappingTime(t *testing.T) { var s struct { Time time.Time