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

0.7.0 BindAndValidate 无法绑定 time.Time #964

Closed
liuzhaowei55 opened this issue Sep 27, 2023 · 10 comments
Closed

0.7.0 BindAndValidate 无法绑定 time.Time #964

liuzhaowei55 opened this issue Sep 27, 2023 · 10 comments
Assignees
Labels
bug Something isn't working

Comments

@liuzhaowei55
Copy link

Describe the bug

0.7.0 BindAndValidate 无法绑定 time.Time

To Reproduce

request body

{
  "startAt": "2006-01-02T15:04:05+07:00"
}

struct

type CreateReq struct {
	StartAt *time.Time `json:"startAt"`
}

bind

var req CreateReq
err := c.BindAndValidate(&req)

结果 StartAt 是空值,0.6.8 正常

Expected behavior

A clear and concise description of what you expected to happen.

Screenshots

image

Hertz version:

0.7.0

Environment:

The output of go env.

Additional context

Add any other context about the problem here.

@FGYFFFF
Copy link
Contributor

FGYFFFF commented Sep 27, 2023

@liuzhaowei55 感谢你的反馈。v0.7.0 做了绑定的重构,time.Time 我们将其做出了一个默认的自定义类型绑定,导致之前 json.unmarshal() 的值被覆盖掉。我来修复下

@FGYFFFF
Copy link
Contributor

FGYFFFF commented Sep 27, 2023

@liuzhaowei55 目前可以直接使用 c.BindJSON() 来规避下

@liuzhaowei55
Copy link
Author

另外就是 v0.6.8 可以通过定义 *time.Time 来实现 nil 值,但 v0.7.0 是会默认生成空值,这个能兼容下吗

@FGYFFFF
Copy link
Contributor

FGYFFFF commented Sep 27, 2023

另外就是 v0.6.8 可以通过定义 *time.Time 来实现 nil 值,但 v0.7.0 是会默认生成空值,这个能兼容下吗

可以的,这块是对 time.Time 做了特殊处理,导致出现 diff,马上修复。先用用 c.BindJSON()

@FGYFFFF
Copy link
Contributor

FGYFFFF commented Sep 27, 2023

@liuzhaowei55 已经修复,#965
我再来同步一下问题出现的原因吧:

  • 因为请求的 body 是 json,所以先做了一次 json.unmarshal(),你传递的值会直接被绑定成功
  • time.Time 是我们默认提供的一种自定义绑定函数,在执行的时候以空值的形式把 json 绑定的值覆盖了

之前绑定的行为是 "只有 tag 匹配才执行自定义的行为",重构后我们想要做的是 "所有的自定义的行为都一定会执行",所以默认的 time.Time 被执行了。(在上述修复中,我们将这种行为改回来了,下个小版本发布)

这块我建议你直接使用 c.BindJSON() 接口就好,这样的话可以省去许多其他的绑定逻辑,性能更好。

@li-jin-gou li-jin-gou added the bug Something isn't working label Oct 8, 2023
@FGYFFFF
Copy link
Contributor

FGYFFFF commented Oct 10, 2023

已修复 v0.7.1

@FGYFFFF FGYFFFF closed this as completed Oct 10, 2023
@wujianbo00
Copy link

已修复 v0.7.1
这个时间绑定还没完全解决,例如请求头Content-Type: application/x-www-form-urlencoded任解析空值。
curl -X 'POST' 'http://...' -H 'accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' -d 'birth=2021-01-01&a=...'

@FGYFFFF
Copy link
Contributor

FGYFFFF commented May 15, 2024

已修复 v0.7.1
这个时间绑定还没完全解决,例如请求头Content-Type: application/x-www-form-urlencoded任解析空值。
curl -X 'POST' 'http://...' -H 'accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' -d 'birth=2021-01-01&a=...'

可以给个更具体的 struct 和 curl 吗

@wujianbo00
Copy link

上面已经给出curl了啊,
curl -X 'POST' 'http://localhost:8080/test' -H 'accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' -d 'birth=2024-01-01%2012%3A00%3A00'

type MyTime time.Time
type ReqTime struct {
Birthday MyTime json:"birth" form:"birth" //MyTime已经重写UnmarshalJSON解析字符串
}
func Test(ctx context.Context, c *app.RequestContext) {
var req ReqTime
c.BindAndValidate(&req) // v0.9.0 Birthday是空的。换成v0.6.2就正常了
c.BindForm(&req) // v0.9.0 Birthday也是空的
}

@FGYFFFF
Copy link
Contributor

FGYFFFF commented May 24, 2024

上面已经给出curl了啊, curl -X 'POST' 'http://localhost:8080/test' -H 'accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' -d 'birth=2024-01-01%2012%3A00%3A00'

type MyTime time.Time type ReqTime struct { Birthday MyTime json:"birth" form:"birth" //MyTime已经重写UnmarshalJSON解析字符串 } func Test(ctx context.Context, c *app.RequestContext) { var req ReqTime c.BindAndValidate(&req) // v0.9.0 Birthday是空的。换成v0.6.2就正常了 c.BindForm(&req) // v0.9.0 Birthday也是空的 }

我复现了问题,这块是因为解析字段的时候,没有继续去识别该字段是一个 type 别名类型。和这个 issue 不是同一个问题,可以再提个 issue 吗? 我来修复下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

4 participants