优化结构体定义
This commit is contained in:
42
swagger.go
42
swagger.go
@ -9,9 +9,11 @@ package api_doc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/gateway/api-doc/define"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ParseSwagger2 解析swagger2.0版本文档
|
||||
@ -44,8 +46,42 @@ func ParseSwagger2(docContent []byte) (*define.DocParseResult, error) {
|
||||
func buildUriList(swaggerDoc *define.Swagger) ([]*define.UriBaseConfig, error) {
|
||||
uriList := make([]*define.UriBaseConfig, 0)
|
||||
for itemUri, itemUriMethodConfig := range swaggerDoc.Paths {
|
||||
GetUriPathParamList(itemUri)
|
||||
fmt.Println(itemUriMethodConfig)
|
||||
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,
|
||||
}
|
||||
}
|
||||
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: make([]*define.ParamConfig, 0),
|
||||
ResultList: nil,
|
||||
}
|
||||
// 解析query/body/header参数
|
||||
uriList = append(uriList, uriResult)
|
||||
}
|
||||
}
|
||||
return uriList, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user