mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-23 11:33:37 +08:00
fix(binding): call validate() after bson.Unmarshal in BSON binding
Every other binding (JSON, YAML, XML, TOML) calls validate() after unmarshaling. BSON skipped it, so struct tags like binding:"required" were silently ignored for BSON-bound requests. Add validate() call and a regression test covering the required-field validation path.
This commit is contained in:
parent
b05d45dbb1
commit
f3875ecaeb
@ -747,6 +747,16 @@ func TestBindingBSON(t *testing.T) {
|
||||
string(data[1:]))
|
||||
}
|
||||
|
||||
func TestBindingBSONValidation(t *testing.T) {
|
||||
// Empty Foo violates binding:"required" — validate() must be called after Unmarshal.
|
||||
var obj FooStruct
|
||||
data, _ := bson.Marshal(&obj)
|
||||
req := requestWithBody(http.MethodPost, "/", string(data))
|
||||
var result FooStruct
|
||||
err := BSON.Bind(req, &result)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestValidationFails(t *testing.T) {
|
||||
var obj FooStruct
|
||||
req := requestWithBody(http.MethodPost, "/", `{"bar": "foo"}`)
|
||||
|
||||
@ -26,5 +26,8 @@ func (b bsonBinding) Bind(req *http.Request, obj any) error {
|
||||
}
|
||||
|
||||
func (bsonBinding) BindBody(body []byte, obj any) error {
|
||||
return bson.Unmarshal(body, obj)
|
||||
if err := bson.Unmarshal(body, obj); err != nil {
|
||||
return err
|
||||
}
|
||||
return validate(obj)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user