From f3875ecaebe024a705f1c0ee78ae7b3c596eae6f Mon Sep 17 00:00:00 2001 From: okxint Date: Tue, 29 Sep 2026 06:58:45 +0530 Subject: [PATCH] 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. --- binding/binding_test.go | 10 ++++++++++ binding/bson.go | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/binding/binding_test.go b/binding/binding_test.go index f90488cd..fa501dad 100644 --- a/binding/binding_test.go +++ b/binding/binding_test.go @@ -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"}`) diff --git a/binding/bson.go b/binding/bson.go index 464890f0..2fb0bbbd 100644 --- a/binding/bson.go +++ b/binding/bson.go @@ -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) }