Description
Issue Description
Empty string turns into false
bool during binding in requests with content types multipart/form-data
and application/x-www-form-urlencoded
.
Checklist
- Dependencies installed
- No typos
- Searched existing issues and docs
Expected behaviour
When we bind requests parameters into a struct with a bool field, we expect binding to fail with http.StatusBadRequest
if the required bool parameter contains an empty string.
We have this similar behaviour during json.Unmarshal
: it cannot unmarshal empty string into a bool variable. It throws an explicit error.
Actual behaviour
If the required bool parameter is an empty string in a request with content types multipart/form-data
and application/x-www-form-urlencoded
, binding into a struct makes this parameter a bool variable with false
value. It doesn't throw any errors.
Steps to reproduce
This unexpected behaviour is due to the function in bind.go file:
func setBoolField(value string, field reflect.Value) error {
if value == "" {
value = "false"
}
boolVal, err := strconv.ParseBool(value)
if err == nil {
field.SetBool(boolVal)
}
return err
}
Working code to debug
type request struct {
Activate bool `form:"activate" `
}
var req UploadRequest
if err := c.Bind(&req); err != nil {
return err
}
Version/commit
v4.1.15