fix(net/ghttp): wrong in-tag param parse for query param (#4227) (#4228)

Fixed issue #4227 and add unit test

---------

Co-authored-by: hailaz <739476267@qq.com>
This commit is contained in:
UncleChair
2025-08-29 09:46:48 +08:00
committed by GitHub
parent ee24da4e72
commit f9ec3b19f7
2 changed files with 64 additions and 4 deletions

View File

@ -222,10 +222,8 @@ func (r *Request) mergeInTagStructValue(data map[string]any) error {
fields := r.serveHandler.Handler.Info.ReqStructFields
if len(fields) > 0 {
var (
foundKey string
foundValue any
headerMap = make(map[string]any)
cookieMap = make(map[string]any)
headerMap = make(map[string]any)
cookieMap = make(map[string]any)
)
for k, v := range r.Header {
@ -239,6 +237,10 @@ func (r *Request) mergeInTagStructValue(data map[string]any) error {
}
for _, field := range fields {
var (
foundKey string
foundValue any
)
if tagValue := field.TagIn(); tagValue != "" {
findKey := field.TagPriorityName()
switch tagValue {

View File

@ -726,3 +726,61 @@ func Test_Issue4093(t *testing.T) {
t.Assert(client.PostContent(ctx, "/test"), `{"page":1,"pageSize":10,"pagination":true,"name":"john","number":1}`)
})
}
// Issue4227Req
type Issue4227Req struct {
g.Meta `path:"/hello/:path_param" method:"post"`
HeaderParam string `json:"Authorization" in:"header" default:"Bearer token123"`
QueryParam bool `json:"query_param" in:"query" default:"false"`
PathParam int `json:"path_param" in:"path" default:"123" v:"required"`
CookieParam bool `json:"cookie_param" in:"cookie" default:"false"`
BodyParam bool `json:"body_param" default:"false"`
}
type Issue4227Res struct {
g.Meta `mime:"application/json"`
}
var (
Issue4227 = cIssue4227{}
)
type cIssue4227 struct{}
func (c *cIssue4227) Feature(ctx context.Context, req *Issue4227Req) (res *Issue4227Res, err error) {
g.RequestFromCtx(ctx).Response.WriteJson(req)
return
}
// https://github.com/gogf/gf/issues/4227
func Test_Issue4227(t *testing.T) {
s := g.Server(guid.S())
s.Group("/", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.Bind(Issue4227)
})
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
client := g.Client().ContentJson()
client.SetPrefix(prefix)
resp1 := client.PostContent(ctx, "/hello/123", `{}`)
t.Assert(resp1, `{"Authorization":"Bearer token123","query_param":false,"path_param":123,"cookie_param":false,"body_param":false}`)
client.SetHeader("Authorization", "Bearer token123")
resp2 := client.PostContent(ctx, "/hello/123", `{"body_param":"true"}`)
t.Assert(resp2, `{"Authorization":"Bearer token123","query_param":false,"path_param":123,"cookie_param":false,"body_param":true}`)
client.SetCookie("cookie_param", "true")
resp3 := client.PostContent(ctx, "/hello/123", `{}`)
t.Assert(resp3, `{"Authorization":"Bearer token123","query_param":false,"path_param":123,"cookie_param":true,"body_param":false}`)
})
}