mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-23 11:33:37 +08:00
Merge f0c5d1ecaf30033ff91e5c739e329cd624b84c3e into 43fe48e8a0f44af783116cdb010725e6bb50255f
This commit is contained in:
commit
357698ac06
1
.github/workflows/gin.yml
vendored
1
.github/workflows/gin.yml
vendored
@ -43,6 +43,7 @@ jobs:
|
||||
"-tags nomsgpack",
|
||||
'--ldflags="-checklinkname=0" -tags sonic',
|
||||
"-tags go_json",
|
||||
"-tags jsoniter",
|
||||
"-race",
|
||||
]
|
||||
include:
|
||||
|
||||
@ -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()))
|
||||
|
||||
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user