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 101 additions and 11 deletions
Showing only changes of commit ff2fa198ee - Show all commits

View File

@ -33,7 +33,55 @@ func GetUriPathParamList(uriPath string) []*define.ParamConfig {
Type: consts.DataTypeString.String(), Type: consts.DataTypeString.String(),
Title: param, Title: param,
Description: param, Description: param,
IsRequired: true,
}) })
} }
return result return result
} }
// GetParamType 将文档配置的数据类型转换为归一化处理后的数据类型
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:52 2024/12/24
func GetParamType(docParamType string, formatType string) consts.DataType {
docParamType = strings.ToLower(docParamType)
formatType = strings.ToLower(formatType)
if len(formatType) == 0 {
formatType = docParamType
}
switch docParamType {
case "integer":
if formatType == "int64" {
return consts.DataTypeInt
}
return consts.DataTypeUint
case "string":
return consts.DataTypeString
default:
return consts.DataTypeAny
}
}
// GetParamLocation 获取参数位置
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:02 2024/12/24
func GetParamLocation(docParamLocation string) consts.RequestDataLocation {
docParamLocation = strings.ToLower(docParamLocation)
switch docParamLocation {
case "query":
return consts.RequestDataLocationQuery
case "header":
return consts.RequestDataLocationHeader
case "cookie":
return consts.RequestDataLocationCookie
case "body":
return consts.RequestDataLocationBody
case "path":
return consts.RequestDataLocationUriPath
default:
return consts.RequestDataLocationQuery
}
}

View File

@ -30,6 +30,7 @@ type UriBaseConfig struct {
Uri string `json:"uri"` // 接口路由 Uri string `json:"uri"` // 接口路由
Method string `json:"method"` // 接口请求方法 Method string `json:"method"` // 接口请求方法
ContentType string `json:"content_type"` // 接口请求类型 ContentType string `json:"content_type"` // 接口请求类型
OutputContentType string `json:"output_content_type"` // 输出数据类型
TagList []string `json:"tag_list"` // 接口标签列表 TagList []string `json:"tag_list"` // 接口标签列表
Summary string `json:"summary"` // 接口摘要描述 Summary string `json:"summary"` // 接口摘要描述
Description string `json:"description"` // 接口详细描述 Description string `json:"description"` // 接口详细描述
@ -48,6 +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"` // 是否必传
} }
// ResultConfig 返回值配置 // ResultConfig 返回值配置

View File

@ -15,6 +15,8 @@ package define
type SwaggerPathConfig struct { type SwaggerPathConfig struct {
Description string `json:"description"` // 接口描述 Description string `json:"description"` // 接口描述
Consumes []string `json:"consumes"` // 请求方式, application/json等 Consumes []string `json:"consumes"` // 请求方式, application/json等
Produces []string `json:"produces"` // 请求方式, application/json等
OperationID string `json:"operationId"` // 操作ID
Tags []string `json:"tags"` // 接口标签 Tags []string `json:"tags"` // 接口标签
Summary string `json:"summary"` // 接口摘要 Summary string `json:"summary"` // 接口摘要
Parameters []*SwaggerPathConfigParameter `json:"parameters"` // 参数列表 Parameters []*SwaggerPathConfigParameter `json:"parameters"` // 参数列表
@ -76,6 +78,8 @@ type SwaggerDefinitionProperty struct {
Type string `json:"type"` // 类型 Type string `json:"type"` // 类型
Items map[string]string `json:"items,omitempty"` // 引用类型中的引用(数组) Items map[string]string `json:"items,omitempty"` // 引用类型中的引用(数组)
AllOf []map[string]string `json:"allOf,omitempty"` // 引用类型中的引用(对象) AllOf []map[string]string `json:"allOf,omitempty"` // 引用类型中的引用(对象)
XGoName string `json:"x-go-name"` // go字段名称
XGoPackage string `json:"x-go-package"` // go 包路径
} }
// Swagger 文档整体结构定义 // Swagger 文档整体结构定义

View File

@ -9,9 +9,11 @@ package api_doc
import ( import (
"errors" "errors"
"fmt"
"git.zhangdeman.cn/gateway/api-doc/define" "git.zhangdeman.cn/gateway/api-doc/define"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/serialize" "git.zhangdeman.cn/zhangdeman/serialize"
"net/http"
"strings"
) )
// ParseSwagger2 解析swagger2.0版本文档 // ParseSwagger2 解析swagger2.0版本文档
@ -44,8 +46,42 @@ func ParseSwagger2(docContent []byte) (*define.DocParseResult, error) {
func buildUriList(swaggerDoc *define.Swagger) ([]*define.UriBaseConfig, error) { 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 {
GetUriPathParamList(itemUri) for requestMethod, methodConfig := range itemUriMethodConfig {
fmt.Println(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 return uriList, nil
} }