gf icon indicating copy to clipboard operation
gf copied to clipboard

规范路由注册时,参数设置为 in:"header",生成api.json正常(有header参数),但是调用接口时框架无法解析这个参数

Open sanrentai opened this issue 3 years ago • 7 comments

1. What version of Go and system type/arch are you using?

go version go1.17.1 windows/amd64

2. What version of GoFrame are you using?

v2.1.1

3. Can this issue be re-produced with the latest release?

yes

4. What did you do?

// 在header中传 AppKey      参数
type QueryReq struct {
	g.Meta `path:"/query" tags:"查询" method:"post" summary:"查询"`
	AppKey      string `in:"header" v:"required#业务系统唯一码不能为空"`
}

5. What did you expect to see?

正常解析参数

6. What did you see instead?

业务系统唯一码不能为空

sanrentai avatar Aug 10 '22 03:08 sanrentai

能否提供最小代码

mingzaily avatar Aug 11 '22 12:08 mingzaily

package main

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
)

type QueryReq struct {
	g.Meta `path:"/query" tags:"查询" method:"post" summary:"查询"`
	AppKey string `in:"header" v:"required#业务系统唯一码不能为空"`
}

type QueryRes struct {
}

type Controller struct{}

var c Controller

func (a *Controller) Index(ctx context.Context, req *QueryReq) (res *QueryRes, err error) {
	g.Log().Info(ctx, req)
	return
}

func main() {
	s := g.Server()
	s.Group("/", func(group *ghttp.RouterGroup) {
		group.Middleware(ghttp.MiddlewareHandlerResponse)
		group.Bind(c)
	})
	s.SetPort(8199)
	s.Run()
}

image @mingzaily

sanrentai avatar Aug 12 '22 02:08 sanrentai

跟 #2036 的问题一样

sanrentai avatar Aug 12 '22 02:08 sanrentai

和社区小伙伴讨论了下,文档中的in只是给openapi的展示,解析参数并未支持,后续会添加功能

mingzaily avatar Aug 12 '22 08:08 mingzaily

假如现在需要从header中取数据,可以现在先这样

r := g.RequestFromCtx(ctx)
r.GetHeader("AppKey")

或使用中间件处理

mingzaily avatar Aug 12 '22 08:08 mingzaily

目前还没进到 Index 方法就 error 了

sanrentai avatar Aug 13 '22 08:08 sanrentai

可以使用中间件或先取消header参数的必传验证

mingzaily avatar Aug 15 '22 03:08 mingzaily

计划分几步操作

  • [x] 1. 先实现一版直接反射的,但效率不高
  • [x] 2. 配套单元测试
  • [ ] 3. 在注册路由时进行反射解析缓存起来,提高请求时的处理效率
    • [ ] 默认值
    • [ ] 数据校验
    • [ ] 规范路由
    • [ ] 其它tag

hailaz avatar Feb 16 '23 02:02 hailaz

@sanrentai 目前已经支持了

hailaz avatar Mar 14 '23 08:03 hailaz

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


@sanrentai is currently supported

Issues-translate-bot avatar Mar 14 '23 08:03 Issues-translate-bot