-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
请求参数如果是bool型,false会被解析为true,只有0才会解析为false #4093
Comments
请给出完整示例代码 |
Please give complete sample code |
// PageReq 分页请求
type PageReq struct {
Page int `json:"page" example:"10" d:"1" v:"min:1#页码最小值不能低于1" dc:"当前页码"`
PerPage int `json:"pageSize" example:"1" d:"10" v:"min:1|max:200#每页数量最小值不能低于1|最大值不能大于200" dc:"每页数量"`
Pagination bool `json:"pagination" d:"true" dc:"是否需要进行分页"`
} 比如这里的是否分页 |
// PageReq paging request |
@Q32611600 我在2.7.0版本中尝试了没有问题,false会被正确解析为false |
@Q32611600 I tried it in version 2.7.0 with no problem, false will be correctly parsed as false |
v2.8.0 |
@Q32611600 看起来是字段的d tag的问题,我加了d tag问题就复现了 |
@Q32611600 It seems to be a problem with the d tag of the field. I added the d tag and the problem reappeared. |
强仔
***@***.***
…------------------ 原始邮件 ------------------
发件人: "gogf/gf" ***@***.***>;
发送时间: 2025年1月5日(星期天) 晚上6:10
***@***.***>;
***@***.******@***.***>;
主题: Re: [gogf/gf] 请求参数如果是bool型,false会被解析为true,只有0才会解析为false (Issue #4093)
@Q32611600 我在2.7.0版本中尝试了没有问题,false会被正确解析为false
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
的确如此,我把缺省值去掉后测试。正如您所说是正确的
强仔
***@***.***
…------------------ 原始邮件 ------------------
发件人: "gogf/gf" ***@***.***>;
发送时间: 2025年1月5日(星期天) 晚上6:14
***@***.***>;
***@***.******@***.***>;
主题: Re: [gogf/gf] 请求参数如果是bool型,false会被解析为true,只有0才会解析为false (Issue #4093)
@Q32611600 看起来是字段的d tag的问题,我加了d tag问题就复现了
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
@gqcn 目前mergeDefaultStructValue这个函数看起来有逻辑错误,当用户传值时,如果传的是类型的默认值,并且如果给字段设置了默认值的话(当和类型的默认值不同时),会给这个字段设置默认值 |
@gqcn At present, the mergeDefaultStructValue function seems to have a logical error. When the user passes a value, if the default value of the type is passed, and if a default value is set for the field (when it is different from the default value of the type), this will be given Field settings default value |
@gqcn 我尝试了数字类型和字符串类型添加默认值时,数字类型不会被设置默认值,因为它被解析为json.Number,传0时,会被认为非空的 使用以下参数传递 {
"state": "",
"pagination": false,
"k1":0
} // 对应的结构体
type HelloReq struct {
g.Meta `path:"/hello" tags:"Hello" method:"get" summary:"You first hello api"`
State string `json:"state" d:"default_state"`
Pagination bool `json:"pagination" d:"true"`
K1 int `json:"k1" d:"198"`
} |
请问这个Bool默认值问题有进展吗? |
Is there any progress on this Bool default value issue? |
补充一下复现必须条件
关键点,传参字段的类型被识别为bool且值为 需要讨论如何处理,并同步修复多处相同逻辑的代码。 代码定位如下 gf/net/ghttp/ghttp_request_param_request.go Line 207 in 63cb328
|
补充资料 需要区分出来哪些是有效参数,哪些是无效参数 例如k1=v1&k2这样的,k2算不算有效的 |
Supplementary information It is necessary to distinguish which are valid parameters and which are invalid parameters For example, if k1=v1&k2 is like this, is k2 considered valid? |
@hailaz http对于参数的解析,依赖gstr.Parse函数,我测试了一下,结果如下 m1, _ := gstr.Parse("k1=v1&k2")
// output: map[k1:v1]
m2, _ := gstr.Parse("k1=v1&k2=")
// output: map[k1:v1 k2:] 当key有=号时,它被解析为有效的参数,没有=号时,视为无效的,文档可以增加一下说明 |
@hailaz http For parameter parsing, I rely on the gstr.Parse function. I tested it and the result is as follows m1, _ := gstr.Parse("k1=v1&k2")
// output: map[k1:v1]
m2, _ := gstr.Parse("k1=v1&k2=")
// output: map[k1:v1 k2:] When the key has an = sign, it is parsed as a valid parameter. If there is no = sign, it is considered invalid. The document can add a description. |
@hailaz 目前ghttp解析json body时,使用的是标准库的json.Unmarshal,当它解析到map时,和go的类型对应关系如下 根据原有的逻辑来看,当前端传的key的类型为string或者bool的默认值时,应该都会被认为没有传值才对, type HelloReq struct {
g.Meta `path:"/hello" tags:"Hello" method:"post" summary:"You first hello api"`
Page int `json:"page" example:"10" d:"1" v:"min:1#页码最小值不能低于1" dc:"当前页码"`
PerPage int `json:"pageSize" example:"1" d:"10" v:"min:1|max:200#每页数量最小值不能低于1|最大值不能大于200" dc:"每页数量"`
Pagination bool `json:"pagination" d:"true" dc:"是否需要进行分页"`
K1 string `json:"k1" d:"k1_default"`
}
func (c *ControllerV1) Hello(ctx context.Context, req *v1.HelloReq) (res *v1.HelloRes, err error) {
g.Log().Info(ctx, req)
g.RequestFromCtx(ctx).Response.WriteJson(req)
return
} 请求的json数据 {
"state": "",
"pagination": false,
"k1":""
} 控制台输出结果
可以看到bool类型和string似乎都被设置默认值了 |
Go version
arm64
GoFrame version
ex 2.7
Can this bug be reproduced with the latest release?
Option Yes
What did you do?
请求参数中的false会被解析为true,只有0才会false
What did you see happen?
请求参数中的false会被解析为true,只有0才会false
What did you expect to see?
请求参数中的false会被解析为true,只有0才会false
The text was updated successfully, but these errors were encountered: