swagger2版本文档解析1.0版本 #2

Merged
zhangdeman merged 14 commits from feature/upgrade_parse_swag into master 2024-12-25 15:00:17 +08:00
4 changed files with 64 additions and 33 deletions
Showing only changes of commit dd46f5ad80 - Show all commits

View File

@ -33,7 +33,7 @@ func GetUriPathParamList(uriPath string) []*define.ParamConfig {
Type: consts.DataTypeString.String(), Type: consts.DataTypeString.String(),
Title: param, Title: param,
Description: param, Description: param,
IsRequired: true, Required: true,
}) })
} }
return result return result
@ -59,7 +59,7 @@ func GetParamType(docParamType string, formatType string) consts.DataType {
case "string": case "string":
return consts.DataTypeString return consts.DataTypeString
default: default:
return consts.DataTypeAny return consts.DataTypeMapStrAny
} }
} }

View File

@ -49,7 +49,7 @@ type ParamConfig struct {
Type string `json:"type"` // 参数类型 Type string `json:"type"` // 参数类型
Title string `json:"title"` // 参数标题 Title string `json:"title"` // 参数标题
Description string `json:"description"` // 参数描述 Description string `json:"description"` // 参数描述
IsRequired bool `json:"is_required"` // 是否必传 Required bool `json:"required"` // 是否必传
} }
// ResultConfig 返回值配置 // ResultConfig 返回值配置

View File

@ -29,13 +29,18 @@ type SwaggerPathConfig struct {
// //
// Date : 16:53 2024/4/19 // Date : 16:53 2024/4/19
type SwaggerPathConfigParameter struct { type SwaggerPathConfigParameter struct {
Type string `json:"type,omitempty"` // 类型 Type string `json:"type,omitempty"` // 类型
Description string `json:"description"` // 描述 Format string `json:"format"` // 格式化类型
Name string `json:"name"` // 参数名称 Description string `json:"description"` // 描述
In string `json:"in"` // 参数位置 Name string `json:"name"` // 参数名称
Required bool `json:"required"` // 是否必传 In string `json:"in"` // 参数位置
EnumList []interface{} `json:"enum_list,omitempty"` // 枚举值列表 Required bool `json:"required"` // 是否必传
Schema map[string]string `json:"schema"` // 参数schema EnumList []any `json:"enum_list,omitempty"` // 枚举值列表
Schema *SwaggerPathConfigParameterScheme `json:"schema"` // 参数schema
}
type SwaggerPathConfigParameterScheme struct {
Ref string `json:"$ref"` // 引用的数据结构定义
} }
// SwaggerPathConfigResponse ... // SwaggerPathConfigResponse ...

View File

@ -47,27 +47,7 @@ func buildUriList(swaggerDoc *define.Swagger) ([]*define.UriBaseConfig, error) {
uriList := make([]*define.UriBaseConfig, 0) uriList := make([]*define.UriBaseConfig, 0)
for itemUri, itemUriMethodConfig := range swaggerDoc.Paths { for itemUri, itemUriMethodConfig := range swaggerDoc.Paths {
for requestMethod, methodConfig := range itemUriMethodConfig { for requestMethod, methodConfig := range itemUriMethodConfig {
if len(methodConfig.Consumes) == 0 { initSwagger2UriMethodConfig(requestMethod, methodConfig)
// 未配置请求方法, 写请求 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,
}
}
uriResult := &define.UriBaseConfig{ uriResult := &define.UriBaseConfig{
Uri: swaggerDoc.BasePath + "/" + strings.TrimLeft(itemUri, "/"), Uri: swaggerDoc.BasePath + "/" + strings.TrimLeft(itemUri, "/"),
Method: strings.ToUpper(requestMethod), Method: strings.ToUpper(requestMethod),
@ -76,12 +56,58 @@ func buildUriList(swaggerDoc *define.Swagger) ([]*define.UriBaseConfig, error) {
TagList: methodConfig.Tags, TagList: methodConfig.Tags,
Summary: methodConfig.Summary, Summary: methodConfig.Summary,
Description: methodConfig.Description, Description: methodConfig.Description,
ParamList: make([]*define.ParamConfig, 0), ParamList: buildSwagger2ParamConfig(swaggerDoc, methodConfig.Parameters),
ResultList: nil, ResultList: nil,
} }
// 解析query/body/header参数
uriList = append(uriList, uriResult) uriList = append(uriList, uriResult)
} }
} }
return uriList, nil 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
}