请求、响应type增加默认值

This commit is contained in:
白茶清欢 2025-02-14 21:08:19 +08:00
parent c9604091f2
commit f2cae8542a

View File

@ -54,6 +54,9 @@ func NewOpenapiDoc(info *define.Info, servers []*define.ServerItem) *Generate {
servers = []*define.ServerItem{} servers = []*define.ServerItem{}
} }
return &Generate{ return &Generate{
readMethodList: []string{
http.MethodGet, http.MethodHead, http.MethodConnect, http.MethodOptions, http.MethodTrace,
},
docData: &define.OpenapiDoc{ docData: &define.OpenapiDoc{
Openapi: consts.SwaggerDocVersion3, Openapi: consts.SwaggerDocVersion3,
Info: info, Info: info,
@ -71,7 +74,8 @@ func NewOpenapiDoc(info *define.Info, servers []*define.ServerItem) *Generate {
// //
// Date : 15:57 2024/7/22 // Date : 15:57 2024/7/22
type Generate struct { type Generate struct {
docData *define.OpenapiDoc docData *define.OpenapiDoc
readMethodList []string
} }
// SetLicense 设置文档协议 // SetLicense 设置文档协议
@ -186,10 +190,8 @@ func (g *Generate) AddApiFromInAndOut(paramType reflect.Type, resultType reflect
} }
// 接口文档初始化 // 接口文档初始化
cfg := g.getApiDocBaseCfg(baseCfg, paramType) cfg := g.getApiDocBaseCfg(baseCfg, paramType)
paramMethod := []string{
http.MethodGet, http.MethodHead, http.MethodConnect, http.MethodOptions, http.MethodTrace, if wrapper.ArrayType(g.readMethodList).Has(baseCfg.Method) >= 0 {
}
if wrapper.ArrayType(paramMethod).Has(baseCfg.Method) >= 0 {
cfg.RequestBody = nil // get类请求没有request body cfg.RequestBody = nil // get类请求没有request body
// 参数解析 // 参数解析
g.ParseReadConfigParam(baseCfg, cfg, paramType) g.ParseReadConfigParam(baseCfg, cfg, paramType)
@ -642,7 +644,19 @@ func (g *Generate) parseBaseUriConfig(paramType reflect.Type) (*define.UriBaseCo
deprecated := metaField.Tag.Get(define.TagDeprecated) deprecated := metaField.Tag.Get(define.TagDeprecated)
res.Deprecated = deprecated == "1" || deprecated == "true" res.Deprecated = deprecated == "1" || deprecated == "true"
res.ContentType = strings.Split(metaField.Tag.Get(define.TagContentType), ",") res.ContentType = strings.Split(metaField.Tag.Get(define.TagContentType), ",")
if len(res.ContentType) == 0 {
if wrapper.ArrayType(g.readMethodList).Has(res.Method) >= 0 {
// get类请求
res.ContentType = []string{consts.MimeTypeXWWWFormUrlencoded}
} else {
res.ContentType = []string{consts.MimeTypeJson}
}
}
res.OutputContentType = strings.Split(metaField.Tag.Get(define.TagOutputContentType), ",") res.OutputContentType = strings.Split(metaField.Tag.Get(define.TagOutputContentType), ",")
if len(res.OutputContentType) == 0 {
// 未设置响应类型默认JSON数据
res.OutputContentType = []string{consts.MimeTypeJson}
}
res.Summary = ParseStructField.Summary(metaField) res.Summary = ParseStructField.Summary(metaField)
if res.Method == "" { if res.Method == "" {
return nil, errors.New("baseCfg.Method is empty") return nil, errors.New("baseCfg.Method is empty")