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

About the problem of standardizing routing request parameters #2904

Closed
wln32 opened this issue Aug 26, 2023 · 3 comments
Closed

About the problem of standardizing routing request parameters #2904

wln32 opened this issue Aug 26, 2023 · 3 comments
Labels
done This issue is done, which may be release in next version. enhancement inactive

Comments

@wln32
Copy link
Member

wln32 commented Aug 26, 2023

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

@Issues-translate-bot Issues-translate-bot changed the title 关于规范路由请求参数的问题 About the problem of standardizing routing request parameters Aug 26, 2023
@hailaz
Copy link
Member

hailaz commented Aug 29, 2023

建议贴下代码,和报错信息

@Issues-translate-bot
Copy link

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


It is recommended to paste the code and error message

@wln32
Copy link
Member Author

wln32 commented Aug 29, 2023

建议贴下代码,和报错信息

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())
}

@hailaz hailaz added the bug It is confirmed a bug, but don't worry, we'll handle it. label Sep 1, 2023
hailaz added a commit that referenced this issue Sep 1, 2023
@gqcn gqcn added enhancement and removed bug It is confirmed a bug, but don't worry, we'll handle it. labels Sep 5, 2023
gqcn added a commit that referenced this issue Oct 9, 2023
@gqcn gqcn added the done This issue is done, which may be release in next version. label Oct 9, 2023
@gqcn gqcn closed this as completed Oct 9, 2023
gqcn added a commit that referenced this issue Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
done This issue is done, which may be release in next version. enhancement inactive
Projects
None yet
Development

No branches or pull requests

4 participants