From 4acc3a035c3e7038e9b8e51ff0fe69e69b35d23c Mon Sep 17 00:00:00 2001 From: chuanye-gao Date: Sun, 27 Sep 2026 22:57:31 +0800 Subject: [PATCH 1/2] 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) + }) +} From f0c5d1ecaf30033ff91e5c739e329cd624b84c3e Mon Sep 17 00:00:00 2001 From: chuanye-gao Date: Sun, 27 Sep 2026 22:57:37 +0800 Subject: [PATCH 2/2] ci: run the test suite with the jsoniter build tag The jsoniter build tag is documented in docs/doc.md but was never covered by the CI matrix, so codec/json/jsoniter.go was neither compiled nor tested. Add it alongside the existing sonic and go_json entries. TestContextBindRequestTooLarge now expects a generic 400 for jsoniter: like goccy/go-json, it does not propagate http.MaxBytesError, so MustBindWith cannot detect it via errors.As. --- .github/workflows/gin.yml | 1 + context_test.go | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/gin.yml b/.github/workflows/gin.yml index d0f42d09..0abd6f8e 100644 --- a/.github/workflows/gin.yml +++ b/.github/workflows/gin.yml @@ -43,6 +43,7 @@ jobs: "-tags nomsgpack", '--ldflags="-checklinkname=0" -tags sonic', "-tags go_json", + "-tags jsoniter", "-race", ] include: diff --git a/context_test.go b/context_test.go index b82e04d5..cefa5081 100644 --- a/context_test.go +++ b/context_test.go @@ -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