From 4ef53ff26d0d7030631e6fa7f02b8f82cefe07ca Mon Sep 17 00:00:00 2001 From: Maximilian Pfeffer Date: Tue, 28 Jul 2026 03:01:28 +0200 Subject: [PATCH] bugfix #1019 - fixed testcases for different json libs. --- body_limit_test.go | 27 +++++++++++++++++++++------ context_test.go | 14 +++++--------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/body_limit_test.go b/body_limit_test.go index 6eb92c97..24d46588 100644 --- a/body_limit_test.go +++ b/body_limit_test.go @@ -18,14 +18,26 @@ import ( "go.mongodb.org/mongo-driver/v2/bson" ) +// maxBytesErrorPropagates reports whether the active JSON backend propagates +// *http.MaxBytesError through Decode()/Unmarshal(). goccy/go-json normalizes +// a MaxBytesReader read failure into a generic EOF, discarding the original +// error (see the caveat in context.go's MustBindWith and +// https://github.com/goccy/go-json/issues/485). bytedance/sonic was reported +// to have the same issue (https://github.com/bytedance/sonic/issues/800), but +// that no longer reproduces with the sonic version currently pinned in +// go.mod — verified locally via `go test -tags sonic`. +func maxBytesErrorPropagates() bool { + return json.Package != "github.com/goccy/go-json" +} + // expectedTooLargeStatus mirrors the go-json caveat documented in // TestContextBindRequestTooLarge: go-json does not propagate // *http.MaxBytesError, so the response falls back to a generic 400. func expectedTooLargeStatus() int { - if json.Package == "github.com/goccy/go-json" { - return http.StatusBadRequest + if maxBytesErrorPropagates() { + return http.StatusRequestEntityTooLarge } - return http.StatusRequestEntityTooLarge + return http.StatusBadRequest } // --- unit-level: limitRequestBody branch coverage --- @@ -112,7 +124,7 @@ func TestBindExceedsBodyLimit(t *testing.T) { require.Error(t, err) c.Writer.WriteHeaderNow() - if json.Package != "github.com/goccy/go-json" { + if maxBytesErrorPropagates() { var maxBytesErr *http.MaxBytesError require.ErrorAs(t, err, &maxBytesErr) } @@ -200,8 +212,11 @@ func TestBindBodyLimitAcrossFormats(t *testing.T) { err := c.ShouldBindWith(tt.target(), tt.b) require.Error(t, err) - var maxBytesErr *http.MaxBytesError - require.ErrorAs(t, err, &maxBytesErr) + + if tt.name != "json" || maxBytesErrorPropagates() { + var maxBytesErr *http.MaxBytesError + require.ErrorAs(t, err, &maxBytesErr) + } }) } } diff --git a/context_test.go b/context_test.go index e8d305e4..3fee0153 100644 --- a/context_test.go +++ b/context_test.go @@ -29,7 +29,6 @@ import ( "github.com/gin-contrib/sse" "github.com/gin-gonic/gin/binding" - "github.com/gin-gonic/gin/codec/json" testdata "github.com/gin-gonic/gin/testdata/protoexample" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -2221,15 +2220,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 - // https://github.com/goccy/go-json/issues/485 - var expectedCode int - switch json.Package { - case "github.com/goccy/go-json": + // When using go-json as JSON encoder, it does not propagate the + // http.MaxBytesError error, so the response falls back to a generic 400 + // instead of 413. See maxBytesErrorPropagates in body_limit_test.go. + expectedCode := http.StatusRequestEntityTooLarge + if !maxBytesErrorPropagates() { expectedCode = http.StatusBadRequest - default: - expectedCode = http.StatusRequestEntityTooLarge } w := httptest.NewRecorder()