- 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
when I declare a struct
type Test struct {
Start time.Time
form:"start"}
I can send a request /test?start=2023-09-02T15:00:00Z. But, When I define a custom time type, like this:
`
type Time struct {
time.Time
}
type Test struct {
Start Time form:"start"
}
`
When I send a request, I get error "invalid character '-' after top-level value".
How to reproduce
package main
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
type Time struct {
time.Time
}
type Test struct {
Start Time `form:"start"`
}
func main() {
engine := gin.Default()
engine.GET("/test", func(c *gin.Context) {
var test Test
/*******/
query := c.Request.URL.Query()
fmt.Println(query)
if err := c.ShouldBindQuery(&test); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
fmt.Println(test)
})
engine.Run(":8080")
}
Expectations
$ curl http://localhost:8080/test?start=2023-09-02T15:00:00Z
Actual result
$ curl -i http://localhost:8080/test?start=2023-09-02T15:00:00Z
{
"error": "invalid character '-' after top-level value"
}
Environment
- go version: go 1.23.0
- gin version (or commit ref): 1.10.0
- operating system: linux
Comment From: JimChenWYU
Maybe you can look for help from here
https://github.com/gin-gonic/gin/issues/4032
Comment From: Ephemeraler
Maybe you can look for help for this
4032
Thanks, but I don't want to add "" in request. because time.Time can do it without "". In source code, I found error in setWithProperType case reflect.Struct, when value is time.Time, the function will call setTimeField, but other type will call json.Unmarshal. I am finding a way to solve it, maybe there a way that entering case rteflect.Ptr can solve it(I guess)