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