Merge db36b3b3a96e6cdce86c927e70dc537b1430e51f into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646

This commit is contained in:
Chuanye Gao 2026-10-10 14:48:15 +00:00 committed by GitHub
commit 6dba350f08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 60 additions and 4 deletions

View File

@ -43,6 +43,7 @@ jobs:
"-tags nomsgpack",
'--ldflags="-checklinkname=0" -tags sonic',
"-tags go_json",
"-tags jsoniter",
"-race",
]
include:

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

View File

@ -2321,12 +2321,12 @@ func TestContextContentType(t *testing.T) {
}
func TestContextBindRequestTooLarge(t *testing.T) {
// When using go-json as JSON encoder, they do not propagate the http.MaxBytesError error
// The response will fail with a generic 400 instead of 413
// When using go-json or jsoniter as JSON encoder, they do not propagate the
// http.MaxBytesError error. The response will fail with a generic 400 instead of 413.
// https://github.com/goccy/go-json/issues/485
var expectedCode int
switch json.Package {
case "github.com/goccy/go-json":
case "github.com/goccy/go-json", "github.com/json-iterator/go":
expectedCode = http.StatusBadRequest
default:
expectedCode = http.StatusRequestEntityTooLarge