完成基础请求参数构建

This commit is contained in:
白茶清欢 2024-12-24 12:33:50 +08:00
parent ff2fa198ee
commit dd46f5ad80
4 changed files with 64 additions and 33 deletions

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

@ -30,12 +30,17 @@ 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"` // 类型
Format string `json:"format"` // 格式化类型
Description string `json:"description"` // 描述 Description string `json:"description"` // 描述
Name string `json:"name"` // 参数名称 Name string `json:"name"` // 参数名称
In string `json:"in"` // 参数位置 In string `json:"in"` // 参数位置
Required bool `json:"required"` // 是否必传 Required bool `json:"required"` // 是否必传
EnumList []interface{} `json:"enum_list,omitempty"` // 枚举值列表 EnumList []any `json:"enum_list,omitempty"` // 枚举值列表
Schema map[string]string `json:"schema"` // 参数schema Schema *SwaggerPathConfigParameterScheme `json:"schema"` // 参数schema
}
type SwaggerPathConfigParameterScheme struct {
Ref string `json:"$ref"` // 引用的数据结构定义
} }
// SwaggerPathConfigResponse ... // SwaggerPathConfigResponse ...

View File

@ -47,6 +47,26 @@ 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 {
initSwagger2UriMethodConfig(requestMethod, methodConfig)
uriResult := &define.UriBaseConfig{
Uri: swaggerDoc.BasePath + "/" + strings.TrimLeft(itemUri, "/"),
Method: strings.ToUpper(requestMethod),
ContentType: methodConfig.Consumes[0],
OutputContentType: methodConfig.Produces[0],
TagList: methodConfig.Tags,
Summary: methodConfig.Summary,
Description: methodConfig.Description,
ParamList: buildSwagger2ParamConfig(swaggerDoc, methodConfig.Parameters),
ResultList: nil,
}
uriList = append(uriList, uriResult)
}
}
return uriList, nil
}
// 初始化配置请求方法 返回类型
func initSwagger2UriMethodConfig(requestMethod string, methodConfig *define.SwaggerPathConfig) {
if len(methodConfig.Consumes) == 0 { if len(methodConfig.Consumes) == 0 {
// 未配置请求方法, 写请求 json , 读请求 form // 未配置请求方法, 写请求 json , 读请求 form
if strings.ToUpper(requestMethod) == http.MethodPost || if strings.ToUpper(requestMethod) == http.MethodPost ||
@ -68,20 +88,26 @@ func buildUriList(swaggerDoc *define.Swagger) ([]*define.UriBaseConfig, error) {
consts.MimeTypeJson, consts.MimeTypeJson,
} }
} }
uriResult := &define.UriBaseConfig{ }
Uri: swaggerDoc.BasePath + "/" + strings.TrimLeft(itemUri, "/"),
Method: strings.ToUpper(requestMethod), // buildSwagger2ParamConfig 构建请求参数配置
ContentType: methodConfig.Consumes[0], func buildSwagger2ParamConfig(swaggerDoc *define.Swagger, paramConfigList []*define.SwaggerPathConfigParameter) []*define.ParamConfig {
OutputContentType: methodConfig.Produces[0], res := make([]*define.ParamConfig, 0)
TagList: methodConfig.Tags, // 解析参数
Summary: methodConfig.Summary, for _, paramConfig := range paramConfigList {
Description: methodConfig.Description, paramConfigBuildConfig := &define.ParamConfig{
ParamList: make([]*define.ParamConfig, 0), Location: GetParamLocation(paramConfig.In).String(),
ResultList: nil, Path: paramConfig.Name,
} Type: GetParamType(paramConfig.Type, paramConfig.Format).String(),
// 解析query/body/header参数 Title: paramConfig.Name,
uriList = append(uriList, uriResult) Description: paramConfig.Description,
} Required: paramConfig.Required,
} }
return uriList, nil res = append(res, paramConfigBuildConfig)
if nil != paramConfig.Schema && len(paramConfig.Schema.Ref) > 0 {
// TODO : 可以继续展开
}
}
return res
} }