feat: 支持设置公共的请求参数

This commit is contained in:
2026-01-07 12:35:43 +08:00
parent 55238e0de8
commit 7de0c874c6
3 changed files with 43 additions and 0 deletions

View File

@@ -296,6 +296,12 @@ func (g *Generate) initApiConfig(docFlag string, apiMeta define.UriConfig) (*ope
}
newOperate := openapi3.NewOperation()
newOperate.Parameters = make(openapi3.Parameters, 0)
// 合入公共的 请求参数
if nil != g.docTable[docFlag].Components.Parameters {
for _, v := range g.docTable[docFlag].Components.Parameters {
newOperate.Parameters = append(newOperate.Parameters, v)
}
}
newOperate.Responses = openapi3.NewResponses()
newOperate.Summary = apiMeta.Desc
newOperate.Description = apiMeta.Desc

View File

@@ -53,6 +53,21 @@ func TestGenerate_AddApiDoc(t *testing.T) {
In: strings.ToLower(consts.RequestDataLocationHeader.String()),
},
},
}), WithCommonParameter(&openapi3.ParametersMap{
"Token": {
Value: &openapi3.Parameter{
Name: "Token",
In: strings.ToLower(consts.RequestDataLocationHeader.String()),
Description: "用户登录 Token",
},
},
"User-Agent": {
Value: &openapi3.Parameter{
Name: "User-Agent",
In: strings.ToLower(consts.RequestDataLocationHeader.String()),
Description: "用户访问 UA",
},
},
}))
DocManager.AddApiDoc(docFlag, define.UriConfig{
Path: "/a/b/c",

View File

@@ -63,3 +63,25 @@ func WithSecurity(securityTable *openapi3.SecuritySchemes) OptionFunc {
}
}
}
// WithCommonParameter 设置公共请求蚕食
func WithCommonParameter(commonParameterTable *openapi3.ParametersMap) OptionFunc {
return func(t *openapi3.T) {
if nil == commonParameterTable {
return
}
if nil == t.Components {
t.Components = &openapi3.Components{}
}
if nil == t.Components.Parameters {
t.Components.Parameters = make(map[string]*openapi3.ParameterRef)
}
// 不要直接复制, 逐个设置, 可以重复调用, 后面的会覆盖前面的
for k, v := range *commonParameterTable {
if nil == v {
continue
}
t.Components.Parameters[k] = v
}
}
}