mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-23 11:33:37 +08:00
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.
34 lines
635 B
Go
34 lines
635 B
Go
// Copyright 2025 Gin Core Team. All rights reserved.
|
|
// Use of this source code is governed by a MIT style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package binding
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
type bsonBinding struct{}
|
|
|
|
func (bsonBinding) Name() string {
|
|
return "bson"
|
|
}
|
|
|
|
func (b bsonBinding) Bind(req *http.Request, obj any) error {
|
|
buf, err := io.ReadAll(req.Body)
|
|
if err == nil {
|
|
err = b.BindBody(buf, obj)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (bsonBinding) BindBody(body []byte, obj any) error {
|
|
if err := bson.Unmarshal(body, obj); err != nil {
|
|
return err
|
|
}
|
|
return validate(obj)
|
|
}
|