支持配置文档选项 #24
@@ -296,6 +296,12 @@ func (g *Generate) initApiConfig(docFlag string, apiMeta define.UriConfig) (*ope
|
|||||||
}
|
}
|
||||||
newOperate := openapi3.NewOperation()
|
newOperate := openapi3.NewOperation()
|
||||||
newOperate.Parameters = make(openapi3.Parameters, 0)
|
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.Responses = openapi3.NewResponses()
|
||||||
newOperate.Summary = apiMeta.Desc
|
newOperate.Summary = apiMeta.Desc
|
||||||
newOperate.Description = apiMeta.Desc
|
newOperate.Description = apiMeta.Desc
|
||||||
|
|||||||
@@ -53,6 +53,21 @@ func TestGenerate_AddApiDoc(t *testing.T) {
|
|||||||
In: strings.ToLower(consts.RequestDataLocationHeader.String()),
|
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{
|
DocManager.AddApiDoc(docFlag, define.UriConfig{
|
||||||
Path: "/a/b/c",
|
Path: "/a/b/c",
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user