Skip to content

Commit d8b857f

Browse files
authored
fix(net/ghttp): Fix specification routing custom parameter recognition exception (gogf#4549)
fix gogf#4442
1 parent d353bf0 commit d8b857f

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

net/ghttp/ghttp_z_unit_feature_request_param_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,49 @@ func Benchmark_ParamTagIn(b *testing.B) {
174174
client.PostContent(ctx, "/user", "id="+strconv.Itoa(i))
175175
}
176176
}
177+
178+
type UserValidReq struct {
179+
g.Meta `path:"/user" method:"get" tags:"XXX" summary:"XXX"`
180+
Query string `p:"query" dc:"查询参数"`
181+
Page int `p:"page_index" v:"min:1" dc:"页码,从1开始" d:"1"`
182+
PageSize int `p:"size" v:"between:1,50" dc:"每页大小,最大50" d:"20"`
183+
}
184+
185+
type UserValidRes struct {
186+
g.Meta `mime:"application/json"`
187+
}
188+
189+
var (
190+
UserValid = cUserValid{}
191+
)
192+
193+
type cUserValid struct{}
194+
195+
func (c *cUserValid) User(ctx context.Context, req *UserValidReq) (res *UserValidRes, err error) {
196+
g.RequestFromCtx(ctx).Response.WriteJson(req)
197+
return
198+
}
199+
200+
// Test_Params_Valid for #4442
201+
func Test_Params_Valid(t *testing.T) {
202+
s := g.Server(guid.S())
203+
s.Group("/", func(group *ghttp.RouterGroup) {
204+
group.Middleware(ghttp.MiddlewareHandlerResponse)
205+
group.Bind(UserValid)
206+
})
207+
s.SetDumpRouterMap(false)
208+
s.Start()
209+
defer s.Shutdown()
210+
211+
time.Sleep(100 * time.Millisecond)
212+
213+
gtest.C(t, func(t *gtest.T) {
214+
prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
215+
client := g.Client()
216+
client.SetPrefix(prefix)
217+
218+
t.Assert(client.GetContent(ctx, "/user"), `{"Query":"","Page":1,"PageSize":20}`)
219+
t.Assert(client.GetContent(ctx, "/user?page_index=0"), `{"code":51,"message":"The page_index value `+"`0`"+` must be equal or greater than 1","data":null}`)
220+
t.Assert(client.GetContent(ctx, "/user?size=100"), `{"code":51,"message":"The size value `+"`100`"+` must be between 1 and 50","data":null}`)
221+
})
222+
}

util/gvalid/gvalid_validator_check_struct.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (v *Validator) doCheckStruct(ctx context.Context, object any) Error {
138138
name = value
139139
} else {
140140
// It or else uses the attribute name directly.
141-
name = fieldName
141+
name = field.TagPriorityName()
142142
}
143143
} else {
144144
// It uses the alias name from validation rule.

0 commit comments

Comments
 (0)