From dd46f5ad807cc28c30febbd6688aad82022ae269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 24 Dec 2024 12:33:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9F=BA=E7=A1=80=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=8F=82=E6=95=B0=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.go | 4 +-- define/generate.go | 2 +- define/swagger.go | 19 +++++++----- swagger.go | 72 +++++++++++++++++++++++++++++++--------------- 4 files changed, 64 insertions(+), 33 deletions(-) diff --git a/common.go b/common.go index 4580533..497c02d 100644 --- a/common.go +++ b/common.go @@ -33,7 +33,7 @@ func GetUriPathParamList(uriPath string) []*define.ParamConfig { Type: consts.DataTypeString.String(), Title: param, Description: param, - IsRequired: true, + Required: true, }) } return result @@ -59,7 +59,7 @@ func GetParamType(docParamType string, formatType string) consts.DataType { case "string": return consts.DataTypeString default: - return consts.DataTypeAny + return consts.DataTypeMapStrAny } } diff --git a/define/generate.go b/define/generate.go index 08584ce..7f66fcf 100644 --- a/define/generate.go +++ b/define/generate.go @@ -49,7 +49,7 @@ type ParamConfig struct { Type string `json:"type"` // 参数类型 Title string `json:"title"` // 参数标题 Description string `json:"description"` // 参数描述 - IsRequired bool `json:"is_required"` // 是否必传 + Required bool `json:"required"` // 是否必传 } // ResultConfig 返回值配置 diff --git a/define/swagger.go b/define/swagger.go index f6ee591..b7bdcea 100644 --- a/define/swagger.go +++ b/define/swagger.go @@ -29,13 +29,18 @@ type SwaggerPathConfig struct { // // Date : 16:53 2024/4/19 type SwaggerPathConfigParameter struct { - Type string `json:"type,omitempty"` // 类型 - Description string `json:"description"` // 描述 - Name string `json:"name"` // 参数名称 - In string `json:"in"` // 参数位置 - Required bool `json:"required"` // 是否必传 - EnumList []interface{} `json:"enum_list,omitempty"` // 枚举值列表 - Schema map[string]string `json:"schema"` // 参数schema + Type string `json:"type,omitempty"` // 类型 + Format string `json:"format"` // 格式化类型 + Description string `json:"description"` // 描述 + Name string `json:"name"` // 参数名称 + In string `json:"in"` // 参数位置 + Required bool `json:"required"` // 是否必传 + EnumList []any `json:"enum_list,omitempty"` // 枚举值列表 + Schema *SwaggerPathConfigParameterScheme `json:"schema"` // 参数schema +} + +type SwaggerPathConfigParameterScheme struct { + Ref string `json:"$ref"` // 引用的数据结构定义 } // SwaggerPathConfigResponse ... diff --git a/swagger.go b/swagger.go index 11602ff..325b499 100644 --- a/swagger.go +++ b/swagger.go @@ -47,27 +47,7 @@ func buildUriList(swaggerDoc *define.Swagger) ([]*define.UriBaseConfig, error) { uriList := make([]*define.UriBaseConfig, 0) for itemUri, itemUriMethodConfig := range swaggerDoc.Paths { for requestMethod, methodConfig := range itemUriMethodConfig { - if len(methodConfig.Consumes) == 0 { - // 未配置请求方法, 写请求 json , 读请求 form - if strings.ToUpper(requestMethod) == http.MethodPost || - strings.ToUpper(requestMethod) == http.MethodPut || - strings.ToUpper(requestMethod) == http.MethodPatch || - strings.ToUpper(requestMethod) == http.MethodDelete { - methodConfig.Consumes = []string{ - consts.MimeTypeJson, - } - } else { - methodConfig.Consumes = []string{ - consts.MimeTypeXWWWFormUrlencoded, - } - } - } - if len(methodConfig.Produces) == 0 { - // 未配置输出数据类型, 默认 json - methodConfig.Produces = []string{ - consts.MimeTypeJson, - } - } + initSwagger2UriMethodConfig(requestMethod, methodConfig) uriResult := &define.UriBaseConfig{ Uri: swaggerDoc.BasePath + "/" + strings.TrimLeft(itemUri, "/"), Method: strings.ToUpper(requestMethod), @@ -76,12 +56,58 @@ func buildUriList(swaggerDoc *define.Swagger) ([]*define.UriBaseConfig, error) { TagList: methodConfig.Tags, Summary: methodConfig.Summary, Description: methodConfig.Description, - ParamList: make([]*define.ParamConfig, 0), + ParamList: buildSwagger2ParamConfig(swaggerDoc, methodConfig.Parameters), ResultList: nil, } - // 解析query/body/header参数 uriList = append(uriList, uriResult) } } return uriList, nil } + +// 初始化配置请求方法 返回类型 +func initSwagger2UriMethodConfig(requestMethod string, methodConfig *define.SwaggerPathConfig) { + if len(methodConfig.Consumes) == 0 { + // 未配置请求方法, 写请求 json , 读请求 form + if strings.ToUpper(requestMethod) == http.MethodPost || + strings.ToUpper(requestMethod) == http.MethodPut || + strings.ToUpper(requestMethod) == http.MethodPatch || + strings.ToUpper(requestMethod) == http.MethodDelete { + methodConfig.Consumes = []string{ + consts.MimeTypeJson, + } + } else { + methodConfig.Consumes = []string{ + consts.MimeTypeXWWWFormUrlencoded, + } + } + } + if len(methodConfig.Produces) == 0 { + // 未配置输出数据类型, 默认 json + methodConfig.Produces = []string{ + consts.MimeTypeJson, + } + } +} + +// buildSwagger2ParamConfig 构建请求参数配置 +func buildSwagger2ParamConfig(swaggerDoc *define.Swagger, paramConfigList []*define.SwaggerPathConfigParameter) []*define.ParamConfig { + res := make([]*define.ParamConfig, 0) + // 解析参数 + for _, paramConfig := range paramConfigList { + paramConfigBuildConfig := &define.ParamConfig{ + Location: GetParamLocation(paramConfig.In).String(), + Path: paramConfig.Name, + Type: GetParamType(paramConfig.Type, paramConfig.Format).String(), + Title: paramConfig.Name, + Description: paramConfig.Description, + Required: paramConfig.Required, + } + res = append(res, paramConfigBuildConfig) + if nil != paramConfig.Schema && len(paramConfig.Schema.Ref) > 0 { + // TODO : 可以继续展开 + } + } + + return res +}