-
-
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
About the problem of standardizing routing request parameters #2904
Labels
Comments
Issues-translate-bot
changed the title
关于规范路由请求参数的问题
About the problem of standardizing routing request parameters
Aug 26, 2023
建议贴下代码,和报错信息 |
It is recommended to paste the code and error message |
type TestReq struct {
Age int
Name string
}
type TestRes struct {
Id int
Age int
Name string
}
s := g.Server(guid.S())
s.Use(ghttp.MiddlewareHandlerResponse)
// 注意这里的req参数是值类型,注册的时候没有报错,调用时候会报错
s.BindHandler("/test", func(ctx context.Context, req TestReq) (res *TestRes, err error) {
return &TestRes{
Id: 1,
Age: req.Age,
Name: req.Name,
}, nil
})
s.BindHandler("/test/error", func(ctx context.Context, req *TestReq) (res *TestRes, err error) {
return &TestRes{
Id: 1,
Age: req.Age,
Name: req.Name,
}, gerror.New("error")
})
s.SetDumpRouterMap(false)
s.Start()
// gf\net\ghttp\ghttp_request_middleware.go:(*middleware).callHandlerFunc
// 原因是这里
if funcInfo.Type.In(1).Kind() == reflect.Ptr {
inputObject = reflect.New(funcInfo.Type.In(1).Elem())
m.request.error = m.request.Parse(inputObject.Interface())
} else {
// 不是指针类型但还是使用elem方法导致报错,非指针类型调用elem会导致panic
inputObject = reflect.New(funcInfo.Type.In(1).Elem()).Elem()
m.request.error = m.request.Parse(inputObject.Addr().Interface())
} |
This was referenced Sep 1, 2023
gqcn
added
enhancement
and removed
bug
It is confirmed a bug, but don't worry, we'll handle it.
labels
Sep 5, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
1. What version of
Go
and system type/arch are you using?go 1.19.3, windows/amd64
2. What version of
GoFrame
are you using?GoFrame CLI Tool v2.5.1, https://goframe.org
3. Can this issue be re-produced with the latest release?
可以
4. What did you do?
当规范路由req参数不是指针的时候,注册的时候不会报错,但是运行时会报错
exception recovered: reflect: Elem of invalid type ghttp_test.TestForHandlerWithObjectAndMeta1Req
5. What did you expect to see?
是否可以在注册的时候提示必须为指针?
6. What did you see instead?
当规范路由req参数不是指针的时候,注册的时候不会报错,但是运行时会报错
exception recovered: reflect: Elem of invalid type ghttp_test.TestForHandlerWithObjectAndMeta1Req
The text was updated successfully, but these errors were encountered: