Compare commits

..

3 Commits

5 changed files with 7 additions and 65 deletions

View File

@ -1,6 +1,6 @@
The MIT License (MIT)
Copyright (c) 2014-present Manuel Martínez-Almeida
Copyright (c) 2014 Manuel Martínez-Almeida
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -4,7 +4,7 @@ GO_VERSION=$(shell $(GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f2)
PACKAGES ?= $(shell $(GO) list ./...)
VETPACKAGES ?= $(shell $(GO) list ./... | grep -v /examples/)
GOFILES := $(shell find . -name "*.go")
TESTFOLDER := $(shell $(GO) list ./... | grep -E 'gin$$|ginS$$|binding$$|render$$' | grep -v examples)
TESTFOLDER := $(shell $(GO) list ./... | grep -E 'gin$$|binding$$|render$$' | grep -v examples)
TESTTAGS ?= ""
.PHONY: test
@ -12,11 +12,7 @@ TESTTAGS ?= ""
test:
echo "mode: count" > coverage.out
for d in $(TESTFOLDER); do \
if [ -n "$(TESTTAGS)" ]; then \
$(GO) test $(TESTTAGS) -v -covermode=count -coverprofile=profile.out $$d > tmp.out; \
else \
$(GO) test -v -covermode=count -coverprofile=profile.out $$d > tmp.out; \
fi; \
$(GO) test $(TESTTAGS) -v -covermode=count -coverprofile=profile.out $$d > tmp.out; \
cat tmp.out; \
if grep -q "^--- FAIL" tmp.out; then \
rm tmp.out; \

View File

@ -1,7 +1,3 @@
// Copyright 2026 Gin Core Team. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package binding
import (

View File

@ -813,19 +813,8 @@ func (c *Context) BindUri(obj any) error {
// - Caller must provide Content-Type header to select the correct body binding (eg "application/json" for JSON binding)
// - Binding validation tags are verified after all request parts have been bound
func (c *Context) BindAll(obj any) error {
err := c.ShouldBindAll(obj)
if err != nil {
var maxBytesErr *http.MaxBytesError
// Note: When using sonic or go-json as JSON encoder, they do not propagate the http.MaxBytesError error
// https://github.com/goccy/go-json/issues/485
// https://github.com/bytedance/sonic/issues/800
switch {
case errors.As(err, &maxBytesErr):
c.AbortWithError(http.StatusRequestEntityTooLarge, err).SetType(ErrorTypeBind) //nolint: errcheck
default:
c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind) //nolint: errcheck
}
if err := c.ShouldBindAll(obj); err != nil {
c.AbortWithError(http.StatusBadRequest, err).SetType(ErrorTypeBind) //nolint: errcheck
return err
}
return nil

View File

@ -2318,7 +2318,7 @@ func TestContextBindAll(t *testing.T) {
c.Request, _ = http.NewRequest(http.MethodPost, "/1234?foo=true", strings.NewReader(`{"bar":"spam"}`))
c.Params = []Param{{Key: "id", Value: "1234"}}
c.Request.Header.Add("Content-Type", MIMEJSON)
c.Request.Header.Add("Content-Type", MIMEJSON) // set fake content-type
c.Request.Header.Add("Limit", "100")
var obj struct {
@ -2340,7 +2340,7 @@ func TestContextBindAll_400OnError(t *testing.T) {
c, _ := CreateTestContext(w)
c.Request, _ = http.NewRequest(http.MethodPost, "/1234?foo=true", strings.NewReader(`{"bar":"spam"}`))
c.Request.Header.Add("Content-Type", MIMEJSON)
c.Request.Header.Add("Content-Type", MIMEJSON) // set fake content-type
c.Request.Header.Add("Limit", "100")
var obj struct {
@ -2353,45 +2353,6 @@ func TestContextBindAll_400OnError(t *testing.T) {
require.ErrorContains(t, err, "Field validation for 'ID' failed")
}
func TestContextBindAll_413MaxBytes(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":
expectedCode = http.StatusBadRequest
default:
expectedCode = http.StatusRequestEntityTooLarge
}
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
c.Request, _ = http.NewRequest(http.MethodPost, "/1234?foo=true", strings.NewReader(`{"bar":"spam"}`))
c.Params = []Param{{Key: "id", Value: "1234"}}
c.Request.Header.Add("Content-Type", MIMEJSON)
c.Request.Header.Add("Limit", "100")
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 10)
var obj struct {
Limit int `header:"limit" binding:"required"`
ID string `uri:"id" binding:"required,numeric"`
Foo bool `form:"foo" binding:"required"`
Bar string `form:"bar" xml:"bar" binding:"required"`
}
err := c.BindAll(&obj)
require.Error(t, err)
if json.Package != "github.com/goccy/go-json" {
var maxBytesErr *http.MaxBytesError
require.ErrorAs(t, err, &maxBytesErr)
}
c.Writer.WriteHeaderNow()
assert.Equal(t, expectedCode, w.Code)
assert.True(t, c.IsAborted())
}
func TestContextBadAutoBind(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)