From 4acc3a035c3e7038e9b8e51ff0fe69e69b35d23c Mon Sep 17 00:00:00 2001 From: chuanye-gao Date: Sun, 27 Sep 2026 22:57:31 +0800 Subject: [PATCH] fix(binding): don't modify map fields when form JSON binding fails setWithProperType decoded the form value straight into the destination field. Some JSON codecs write to the destination before reporting an error: jsoniter allocates a map, or replaces it with an empty one, before it reports a syntax error, while encoding/json leaves the field untouched. A failed bind therefore left the map field in a different state depending on the codec the binary was built with. Decode into a temporary value and assign it only once decoding succeeded, so a failed bind never modifies the destination. The success path is unchanged. --- binding/form_mapping.go | 12 +++++++++- binding/form_mapping_test.go | 45 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index 6982fd4f..cd43f95a 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -375,7 +375,17 @@ func setWithProperType(val string, value reflect.Value, field reflect.StructFiel } return json.API.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface()) case reflect.Map: - return json.API.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface()) + // Decode into a temporary value and only assign it once decoding + // succeeded. Some codecs write to the destination before reporting an + // error — jsoniter allocates or replaces the map even when the input is + // not valid JSON — which would leave the field modified even though the + // binding failed. + tmp := reflect.New(value.Type()) + if err := json.API.Unmarshal(bytesconv.StringToBytes(val), tmp.Interface()); err != nil { + return err + } + value.Set(tmp.Elem()) + return nil case reflect.Ptr: if !value.Elem().IsValid() { value.Set(reflect.New(value.Type().Elem())) diff --git a/binding/form_mapping_test.go b/binding/form_mapping_test.go index c78f7398..e950fb1f 100644 --- a/binding/form_mapping_test.go +++ b/binding/form_mapping_test.go @@ -1144,3 +1144,48 @@ func TestMappingEmptyValues(t *testing.T) { assert.Equal(t, []int{1, 2, 3}, s.SliceCsv) }) } + +// TestMappingFailingJSONDoesNotModifyFields covers the map fields that are +// bound from a JSON value. JSON codecs differ in how much they write to the +// destination before reporting an error, so a failed bind must not leave the +// field in a partially updated state. +// +// Several invalid inputs are checked because how jsoniter writes to the +// destination before failing depends on the input itself. +func TestMappingFailingJSONDoesNotModifyFields(t *testing.T) { + invalid := []string{"abc", "{bad", "not-json", "["} + + t.Run("map stays nil", func(t *testing.T) { + for _, val := range invalid { + var s struct { + Map map[string]any `form:"map"` + } + + err := mappingByPtr(&s, formSource{"map": {val}}, "form") + require.Error(t, err, "input %q", val) + assert.Nil(t, s.Map, "input %q", val) + } + }) + + t.Run("existing map keeps its content", func(t *testing.T) { + for _, val := range invalid { + s := struct { + Map map[string]any `form:"map"` + }{Map: map[string]any{"keep": "me"}} + + err := mappingByPtr(&s, formSource{"map": {val}}, "form") + require.Error(t, err, "input %q", val) + assert.Equal(t, map[string]any{"keep": "me"}, s.Map, "input %q", val) + } + }) + + t.Run("valid JSON is still bound", func(t *testing.T) { + var s struct { + Map map[string]any `form:"map"` + } + + err := mappingByPtr(&s, formSource{"map": {`{"a":1}`}}, "form") + require.NoError(t, err) + assert.Equal(t, map[string]any{"a": float64(1)}, s.Map) + }) +}