Merge e83cdcc9e463ec5d582123e34ecb602a3b46c6c5 into dcaa4296d111981ffb31ac3eba90bb63e1eb5ab9

This commit is contained in:
Name 2026-08-23 00:24:06 -07:00 committed by GitHub
commit 1e170cfda0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 0 deletions

View File

@ -990,6 +990,11 @@ func (c *Context) ShouldBindBodyWithPlain(obj any) error {
return c.ShouldBindBodyWith(obj, binding.Plain)
}
// ShouldBindBodyWithProtoBuf is a shortcut for c.ShouldBindBodyWith(obj, binding.ProtoBuf).
func (c *Context) ShouldBindBodyWithProtoBuf(obj any) error {
return c.ShouldBindBodyWith(obj, binding.ProtoBuf)
}
// ClientIP implements one best effort algorithm to return the real client IP.
// It calls c.RemoteIP() under the hood, to check if the remote IP is a trusted proxy or not.
// If it is it will then try to parse the headers defined in Engine.RemoteIPHeaders (defaulting to [X-Forwarded-For, X-Real-IP]).

View File

@ -3004,6 +3004,21 @@ func TestContextShouldBindBodyWithPlain(t *testing.T) {
}
}
func TestContextShouldBindBodyWithProtoBuf(t *testing.T) {
label := "FOO"
protoBody, err := proto.Marshal(&testdata.Test{Label: &label})
require.NoError(t, err)
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
c.Request = httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(protoBody))
obj := testdata.Test{}
require.NoError(t, c.ShouldBindBodyWithProtoBuf(&obj))
require.NotNil(t, obj.Label)
assert.Equal(t, "FOO", *obj.Label)
}
func TestContextGolangContext(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest(http.MethodPost, "/", strings.NewReader(`{"foo":"bar", "bar":"foo"}`))

View File

@ -1576,6 +1576,7 @@ For this, you can use `c.ShouldBindBodyWith` or shortcuts.
- `c.ShouldBindBodyWithXML` is a shortcut for c.ShouldBindBodyWith(obj, binding.XML).
- `c.ShouldBindBodyWithYAML` is a shortcut for c.ShouldBindBodyWith(obj, binding.YAML).
- `c.ShouldBindBodyWithTOML` is a shortcut for c.ShouldBindBodyWith(obj, binding.TOML).
- `c.ShouldBindBodyWithProtoBuf` is a shortcut for c.ShouldBindBodyWith(obj, binding.ProtoBuf).
```go
func SomeHandler(c *gin.Context) {