- With issues:
- Use the search tool before opening a new issue.
- Please provide source code and commit sha if you found a bug.
- Review existing issues and provide feedback or react to them.
Description
Binding query parameter to time.Time
field,when query parameter is a zero value, ShouldBind
returns error.
How to reproduce
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
g := gin.Default()
g.GET("/", func(c *gin.Context) {
var req struct {
CreatedAt time.Time `form:"created_at" time_format:"unix"`
}
if err := c.ShouldBind(&req); err != nil {
str := fmt.Sprintf("\n======err:%v\n", err)
c.String(200, str)
return
}
c.String(200, req.CreatedAt.String())
})
g.Run(":9000")
}
Expectations
$ curl "http://localhost:9000/?created_at="
1970-01-01 08:00:00 +0800 CST
Actual result
$ curl "http://localhost:9000/?created_at="
======err:strconv.ParseInt: parsing "": invalid syntax
0001-01-01 00:00:00 +0000 UTC
Environment
- go version: go version go1.23.0 darwin/arm64
- gin version (or commit ref): 1.9.1
- operating system: mac m2
I think these lines of code should move up to function start https://github.com/gin-gonic/gin/blob/e46bd521859fdfc83c508f1d42c92cb7f91e9fcb/binding/form_mapping.go#L417-L420