feat: 支持配置Security + 移除不必要的components
This commit is contained in:
@@ -29,7 +29,7 @@ var (
|
||||
func NewOpenApiDoc(optionFunc ...OptionFunc) *openapi3.T {
|
||||
t := &openapi3.T{
|
||||
Extensions: map[string]any{},
|
||||
OpenAPI: "3.1.0",
|
||||
OpenAPI: "3.0.3",
|
||||
Components: &openapi3.Components{
|
||||
Extensions: map[string]any{},
|
||||
Origin: &openapi3.Origin{
|
||||
@@ -167,7 +167,7 @@ func (g *Generate) AddApiDoc(docFlag string, apiMeta define.UriConfig, request a
|
||||
|
||||
schemaData := GenerateOpenAPISchema(requestType)
|
||||
apiOperate, isRead := g.initApiConfig(docFlag, apiMeta)
|
||||
requestTypeStr := requestType.String()
|
||||
//requestTypeStr := requestType.String()
|
||||
if isRead {
|
||||
for paramName, paramConfig := range schemaData.Value.Properties {
|
||||
apiOperate.Parameters = append(apiOperate.Parameters, &openapi3.ParameterRef{
|
||||
@@ -217,14 +217,13 @@ func (g *Generate) AddApiDoc(docFlag string, apiMeta define.UriConfig, request a
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化接口配置
|
||||
if _, exist := g.docTable[docFlag].Components.Schemas[requestTypeStr]; !exist {
|
||||
/*if _, exist := g.docTable[docFlag].Components.Schemas[requestTypeStr]; !exist {
|
||||
g.docTable[docFlag].Components.Schemas[requestTypeStr] = schemaData
|
||||
}
|
||||
responseTypeStr := responseType.String()
|
||||
}*/
|
||||
/*responseTypeStr := responseType.String()
|
||||
if _, exist := g.docTable[docFlag].Components.Schemas[responseTypeStr]; !exist {
|
||||
g.docTable[docFlag].Components.Schemas[responseTypeStr] = GenerateOpenAPISchema(responseType)
|
||||
}
|
||||
}*/
|
||||
desc := "请求成功"
|
||||
apiOperate.Responses.Set(fmt.Sprintf("%v", http.StatusOK), &openapi3.ResponseRef{
|
||||
Extensions: nil,
|
||||
@@ -239,7 +238,7 @@ func (g *Generate) AddApiDoc(docFlag string, apiMeta define.UriConfig, request a
|
||||
consts.MimeTypeJson: {
|
||||
Extensions: nil,
|
||||
Origin: nil,
|
||||
Schema: g.docTable[docFlag].Components.Schemas[responseTypeStr],
|
||||
Schema: GenerateOpenAPISchema(responseType),
|
||||
Example: nil,
|
||||
Examples: nil,
|
||||
Encoding: nil,
|
||||
|
||||
@@ -11,10 +11,13 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/api-doc/define"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"github.com/getkin/kin-openapi/openapi3"
|
||||
)
|
||||
|
||||
func TestGenerate_AddApiDoc(t *testing.T) {
|
||||
@@ -35,7 +38,21 @@ func TestGenerate_AddApiDoc(t *testing.T) {
|
||||
Category *Category `json:"category,omitempty" description:"分类"`
|
||||
}
|
||||
docFlag := "demo"
|
||||
DocManager.NewOpenApiDoc(docFlag)
|
||||
DocManager.NewOpenApiDoc(docFlag, WithSecurity(&openapi3.SecuritySchemes{
|
||||
"Token-Auth": {
|
||||
Extensions: nil,
|
||||
Origin: nil,
|
||||
Ref: "",
|
||||
Value: &openapi3.SecurityScheme{
|
||||
Extensions: nil,
|
||||
Origin: nil,
|
||||
Type: "apiKey",
|
||||
Description: "Token 身份认证",
|
||||
Name: "token",
|
||||
In: strings.ToLower(consts.RequestDataLocationHeader.String()),
|
||||
},
|
||||
},
|
||||
}))
|
||||
DocManager.AddApiDoc(docFlag, define.UriConfig{
|
||||
Path: "/a/b/c",
|
||||
RequestMethod: http.MethodGet,
|
||||
|
||||
@@ -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