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

请求参数如果是bool型,false会被解析为true,只有0才会解析为false #4093

Open
Q32611600 opened this issue Jan 2, 2025 · 14 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@Q32611600
Copy link

Go version

arm64

GoFrame version

ex 2.7

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

{
"state": "",
"pagination": false
} 请求参数中的false会被解析为true,只有0才会false

What did you see happen?

{
"state": "",
"pagination": false
} 请求参数中的false会被解析为true,只有0才会false

What did you expect to see?

{
"state": "",
"pagination": false
} 请求参数中的false会被解析为true,只有0才会false

@Q32611600 Q32611600 added the bug It is confirmed a bug, but don't worry, we'll handle it. label Jan 2, 2025
@Q32611600 Q32611600 changed the title os/gtime: issue title 请求参数如果是bool型,false会被解析为true,只有0才会解析为false Jan 2, 2025
@wln32
Copy link
Member

wln32 commented Jan 5, 2025

请给出完整示例代码

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Please give complete sample code

@Q32611600
Copy link
Author

Q32611600 commented Jan 5, 2025

// 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:"是否需要进行分页"`
}

比如这里的是否分页
控制器里面用 if req.Pagination {
提交的参数是否false 也会走分页,只有传入0才会不走分页
}

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


// PageReq paging request
typePageReq struct {
Page int json:"page" example:"10" d:"1" v:"min:1#The minimum value of the page number cannot be less than 1" dc:"Current page number"
PerPage int json:"pageSize" example:"1" d:"10" v:"min:1|max:200#The minimum value of the number per page cannot be less than 1 | the maximum value cannot be greater than 200" dc:"Per page Quantity"
Pagination bool json:"pagination" d:"true" dc:"Whether pagination is required"
}
For example, here is whether to paginate
Use if req.Pagination { in the controller
Whether the submitted parameters are false will also cause paging. Only when 0 is passed in will pagination not occur.
}

@wln32
Copy link
Member

wln32 commented Jan 5, 2025

@Q32611600 我在2.7.0版本中尝试了没有问题,false会被正确解析为false

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@Q32611600 I tried it in version 2.7.0 with no problem, false will be correctly parsed as false

@Q32611600
Copy link
Author

v2.8.0
Welcome to GoFrame!
Env Detail:
Go Version: go1.23.1 windows/amd64
GF Version(go.mod):
github.com/gogf/gf/contrib/drivers/mssql/[email protected]
github.com/gogf/gf/contrib/nosql/redis/[email protected]
github.com/gogf/gf/[email protected]
CLI Detail:
Installed At: C:\Go\bin\gf.exe
Built Go Version: go1.22.5
Built GF Version: v2.8.0
Git Commit: 2024-11-17 10:57:17 ec6c537
Built Time: 2024-11-17 11:03:05
这是我当前用的版本

@wln32
Copy link
Member

wln32 commented Jan 5, 2025

@Q32611600 看起来是字段的d tag的问题,我加了d tag问题就复现了

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@Q32611600 It seems to be a problem with the d tag of the field. I added the d tag and the problem reappeared.

@Q32611600
Copy link
Author

Q32611600 commented Jan 5, 2025 via email

@Q32611600
Copy link
Author

Q32611600 commented Jan 5, 2025 via email

@wln32
Copy link
Member

wln32 commented Jan 5, 2025

@gqcn 目前mergeDefaultStructValue这个函数看起来有逻辑错误,当用户传值时,如果传的是类型的默认值,并且如果给字段设置了默认值的话(当和类型的默认值不同时),会给这个字段设置默认值
image

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@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
image

@wln32
Copy link
Member

wln32 commented Jan 5, 2025

@gqcn 我尝试了数字类型和字符串类型添加默认值时,数字类型不会被设置默认值,因为它被解析为json.Number,传0时,会被认为非空的
image

使用以下参数传递

{
"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"`
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

No branches or pull requests

3 participants