The Go standard library includes a method `http.MaxBytesReader` that allows limiting the request body. For example, users can create a middleware like:
```go
func MiddlewareMaxBodySize(c *gin.Context) {
// Limit request body to 100 bytes
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 100)
c.Next()
}
```
When the body exceeds the limit, reading from the request body returns an error of type `http.MaxBytesError`.
This PR makes sure that when the error is of kind `http.MaxBytesError`, Gin returns the correct status code 413 (Request Entity Too Large) instead of a generic 400 (Bad Request).
* chore: update Go versions and dependencies for improved compatibility
- Update Go versions in workflow file to `1.23` and `1.24`
- Enhance test tags in workflow with specific linker flags
- Remove the conditional formatting step for Go `1.22.x` in workflow
- Remove `goimports` settings from `.golangci.yml`
- Update `go.mod` to use Go `1.23.0`
- Upgrade `github.com/bytedance/sonic` from `v1.11.6` to `v1.13.1`
- Update indirect dependencies `sonic/loader` to `v0.2.4` and `base64x` to `v0.1.5` in `go.mod`
Signed-off-by: appleboy <appleboy.tw@gmail.com>
* chore: update project for Go 1.23 compatibility and documentation fixes
- Update Go version requirement from 1.22 to 1.23 in README.md
- Remove superfluous `$` from example command in README.md
- Update warning message to reflect new Go version requirement in debug.go
- Update test assertion to reflect new Go version requirement in debug_test.go
Signed-off-by: appleboy <appleboy.tw@gmail.com>
---------
Signed-off-by: appleboy <appleboy.tw@gmail.com>
- Update Go versions in GitHub Actions workflow to `1.22` and `1.23`
- Update README to require Go version `1.22` or above
- Adjust table formatting in README for better alignment
- Update warning message in `debug.go` to reflect Go version `1.22`
- Update test in `debug_test.go` to reflect Go version `1.22`
- Update `go.mod` to require Go version `1.22`
- Update dependencies in `go.mod` to newer versions
Signed-off-by: appleboy <appleboy.tw@gmail.com>
- Update GoReleaser action to version 6 in GitHub workflow
- Use `http.MethodPost` constant in test requests instead of hardcoded string
Signed-off-by: appleboy <appleboy.tw@gmail.com>
* enhance code imported by #3413
if it needs to check if the handler is nil, tie c.index shall
always ++
* test: refactor test context initialization and handler logic
- Remove an empty line in `TestContextInitQueryCache`
- Add `TestContextNext` function with tests for `Next` method behavior with no handlers, one handler, and multiple handlers
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
---------
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Co-authored-by: zjj <zhong2plus@gmail.com>
This PR introduces a generic function, getTyped[T any], to simplify value retrieval in the Context struct. It replaces repetitive type assertions in the GetString GetBool etc. methods.
Co-authored-by: Maksim Konovalov <maksim.konovalov@vk.team>
Use assert.InDelta for float comparison with tolerance in TestContextGetFloat32
Remove unnecessary blank line in TestContextInitQueryCache
Replace anonymous struct with named contextKey type in TestContextWithFallbackValueFromRequestContext
Update context key handling in TestContextWithFallbackValueFromRequestContext to use contextKey type
- Use specified default value in struct tags when binding a request input to struct for validation, even if sent empty, not only when not sent at all.
- Add string field to `TestMappingDefault` test case.
- Add test case for not sent form field to default to the value specified via code.
- Add test case for form field sent empty to default to the value specified via code.
Fixes: How to apply default value if empty value provided by client during model binding? #4042, #13042df, #a41721a
* feat(form): add array collection format in form binding
* feat(form): add array collection format in form binding
* test(form): fix test code for array collection format in form binding