feat: 支持配置Security + 移除不必要的components
This commit is contained in:
@@ -7,7 +7,11 @@
|
||||
// Date : 2026-01-06 22:48
|
||||
package openapi
|
||||
|
||||
import "github.com/getkin/kin-openapi/openapi3"
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/getkin/kin-openapi/openapi3"
|
||||
)
|
||||
|
||||
// OptionFunc 设置文档选项
|
||||
type OptionFunc func(t *openapi3.T)
|
||||
@@ -31,3 +35,31 @@ func WithInfo(info *openapi3.Info) OptionFunc {
|
||||
t.Info = info
|
||||
}
|
||||
}
|
||||
|
||||
// WithSecurity 设置安全策略
|
||||
func WithSecurity(securityTable *openapi3.SecuritySchemes) OptionFunc {
|
||||
return func(t *openapi3.T) {
|
||||
if nil == securityTable {
|
||||
return
|
||||
}
|
||||
if nil == t.Components {
|
||||
t.Components = &openapi3.Components{}
|
||||
}
|
||||
if nil == t.Components.SecuritySchemes {
|
||||
t.Components.SecuritySchemes = make(map[string]*openapi3.SecuritySchemeRef)
|
||||
}
|
||||
if nil == t.Security {
|
||||
t.Security = make([]openapi3.SecurityRequirement, 0)
|
||||
}
|
||||
keyList := make([]string, 0)
|
||||
for k, v := range *securityTable {
|
||||
keyList = append(keyList, k)
|
||||
t.Components.SecuritySchemes[k] = v
|
||||
}
|
||||
// 保证生成结果有序
|
||||
sort.Strings(keyList)
|
||||
for _, k := range keyList {
|
||||
t.Security = append(t.Security, map[string][]string{k: {}})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user