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.
This commit is contained in:
chuanye-gao 2026-09-27 22:57:31 +08:00
parent 1bd0ecf2d3
commit 4acc3a035c
2 changed files with 56 additions and 1 deletions

View File

@ -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()))

View File

@ -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)
})
}