Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About bind parse #4058

Closed
Ephemeraler opened this issue Sep 18, 2024 · 2 comments
Closed

About bind parse #4058

Ephemeraler opened this issue Sep 18, 2024 · 2 comments

Comments

@Ephemeraler
Copy link

  • 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
@JimChenWYU
Copy link

JimChenWYU commented Sep 18, 2024

Maybe you can look for help from here

#4032

@Ephemeraler
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants