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:
okxint 2026-09-29 06:58:45 +05:30
parent b05d45dbb1
commit f3875ecaeb
2 changed files with 14 additions and 1 deletions

View File

@ -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"}`)

View File

@ -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)
}