From f2cae8542abe206ea1bd6f798b8eed8e993df803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 14 Feb 2025 21:08:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=B1=82=E3=80=81=E5=93=8D=E5=BA=94ty?= =?UTF-8?q?pe=E5=A2=9E=E5=8A=A0=E9=BB=98=E8=AE=A4=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generate.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) 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")