v1基础请求参数构建
This commit is contained in:
parent
114b5db641
commit
9569f7dfd0
@ -66,6 +66,7 @@ type SwaggerPathConfigParameter struct {
|
|||||||
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"` // 枚举值列表
|
||||||
Schema map[string]string `json:"schema"` // 参数schema
|
Schema map[string]string `json:"schema"` // 参数schema
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,7 +88,7 @@ type SwaggerPathConfigResponse struct {
|
|||||||
type SwaggerDefinition struct {
|
type SwaggerDefinition struct {
|
||||||
Type string `json:"type"` // 类型
|
Type string `json:"type"` // 类型
|
||||||
Required []string `json:"required"` // 必传参数列表
|
Required []string `json:"required"` // 必传参数列表
|
||||||
Properties map[string]SwaggerDefinitionProperty `json:"properties"` // 参数名 => 参数配置
|
Properties map[string]*SwaggerDefinitionProperty `json:"properties"` // 参数名 => 参数配置
|
||||||
}
|
}
|
||||||
|
|
||||||
// SwaggerDefinitionProperty ...
|
// SwaggerDefinitionProperty ...
|
||||||
@ -111,8 +112,8 @@ type Swagger struct {
|
|||||||
Host string `json:"host"` // 服务域名
|
Host string `json:"host"` // 服务域名
|
||||||
BasePath string `json:"basePath"` // 基础path
|
BasePath string `json:"basePath"` // 基础path
|
||||||
Info Info `json:"info"` // 文档描述信息
|
Info Info `json:"info"` // 文档描述信息
|
||||||
Paths map[string]map[string]SwaggerPathConfig `json:"paths"` // 接口列表 : 接口 => 请求方法 => 请求配置
|
Paths map[string]map[string]*SwaggerPathConfig `json:"paths"` // 接口列表 : 接口 => 请求方法 => 请求配置
|
||||||
Definitions map[string]SwaggerDefinition `json:"definitions"` // 数据定义
|
Definitions map[string]*SwaggerDefinition `json:"definitions"` // 数据定义
|
||||||
}
|
}
|
||||||
|
|
||||||
// SwaggerInput ...
|
// SwaggerInput ...
|
||||||
@ -156,6 +157,7 @@ type SwaggerParameterInput struct {
|
|||||||
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"` // 枚举值列表
|
||||||
}
|
}
|
||||||
|
|
||||||
// SwaggerResponseInput 响应数据
|
// SwaggerResponseInput 响应数据
|
||||||
|
125
swagger/run.go
125
swagger/run.go
@ -29,9 +29,10 @@ func Generate(docConfig *define.SwaggerInput) (*define.Swagger, error) {
|
|||||||
Host: docConfig.Host,
|
Host: docConfig.Host,
|
||||||
BasePath: docConfig.BasePath,
|
BasePath: docConfig.BasePath,
|
||||||
Info: docConfig.Info,
|
Info: docConfig.Info,
|
||||||
Paths: map[string]map[string]define.SwaggerPathConfig{},
|
Paths: map[string]map[string]*define.SwaggerPathConfig{},
|
||||||
Definitions: map[string]define.SwaggerDefinition{},
|
Definitions: map[string]*define.SwaggerDefinition{},
|
||||||
}
|
}
|
||||||
|
generatePathConfig(swaggerInfo, docConfig)
|
||||||
return swaggerInfo, nil
|
return swaggerInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,9 +42,13 @@ func Generate(docConfig *define.SwaggerInput) (*define.Swagger, error) {
|
|||||||
//
|
//
|
||||||
// Date : 10:54 2024/4/22
|
// Date : 10:54 2024/4/22
|
||||||
func formatDocConfig(docConfig *define.SwaggerInput) {
|
func formatDocConfig(docConfig *define.SwaggerInput) {
|
||||||
if docConfig.Schemes == nil {
|
if len(docConfig.Schemes) == 0 {
|
||||||
docConfig.Schemes = make([]string, 0)
|
docConfig.Schemes = []string{consts.SchemeHTTP}
|
||||||
}
|
}
|
||||||
|
docConfig.Host = wrapper.String(docConfig.Host).ReplaceChar(map[string]string{
|
||||||
|
"http://": "",
|
||||||
|
"https://": "",
|
||||||
|
}).Value()
|
||||||
for _, itemPath := range docConfig.PathConfigList {
|
for _, itemPath := range docConfig.PathConfigList {
|
||||||
// 默认请求类型 application/json
|
// 默认请求类型 application/json
|
||||||
itemPath.ContentType = strings.TrimSpace(itemPath.ContentType)
|
itemPath.ContentType = strings.TrimSpace(itemPath.ContentType)
|
||||||
@ -59,7 +64,7 @@ func formatDocConfig(docConfig *define.SwaggerInput) {
|
|||||||
for _, itemParam := range itemPath.ParameterList {
|
for _, itemParam := range itemPath.ParameterList {
|
||||||
// 填充默认参数位置
|
// 填充默认参数位置
|
||||||
itemParam.In = strings.TrimSpace(itemParam.In)
|
itemParam.In = strings.TrimSpace(itemParam.In)
|
||||||
itemPath.Summary = wrapper.TernaryOperator.String(len(itemParam.In) == 0, wrapper.String(util.GetParameterDefaultLocation(itemPath.Method)), wrapper.String(itemParam.In)).Value()
|
itemParam.In = wrapper.TernaryOperator.String(len(itemParam.In) == 0, wrapper.String(util.GetParameterDefaultLocation(itemPath.Method)), wrapper.String(itemParam.In)).Value()
|
||||||
|
|
||||||
// 参数类型没填, 按照字符串处理
|
// 参数类型没填, 按照字符串处理
|
||||||
itemParam.Type = strings.TrimSpace(itemParam.Type)
|
itemParam.Type = strings.TrimSpace(itemParam.Type)
|
||||||
@ -80,3 +85,113 @@ func formatDocConfig(docConfig *define.SwaggerInput) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generatePathConfig 生成接口配置
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 12:00 2024/4/22
|
||||||
|
func generatePathConfig(swaggerInfo *define.Swagger, docConfig *define.SwaggerInput) {
|
||||||
|
for _, itemPath := range docConfig.PathConfigList {
|
||||||
|
itemPath.Uri = getUri(itemPath.Uri)
|
||||||
|
if _, exist := swaggerInfo.Paths[itemPath.Uri]; !exist {
|
||||||
|
swaggerInfo.Paths[itemPath.Uri] = map[string]*define.SwaggerPathConfig{}
|
||||||
|
}
|
||||||
|
// 处理请求类型的配置
|
||||||
|
swaggerInfo.Paths[itemPath.Uri][itemPath.Method] = &define.SwaggerPathConfig{
|
||||||
|
Description: itemPath.Description,
|
||||||
|
Consumes: []string{itemPath.ContentType},
|
||||||
|
Tags: itemPath.TagList,
|
||||||
|
Summary: itemPath.Summary,
|
||||||
|
Parameters: make([]*define.SwaggerPathConfigParameter, 0),
|
||||||
|
Response: make(map[string]*define.SwaggerPathConfigResponse),
|
||||||
|
}
|
||||||
|
// 生成参数配置
|
||||||
|
generatePathParameterConfig(swaggerInfo, itemPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// generatePathParameterConfig 生成接口的参数配置
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 12:09 2024/4/22
|
||||||
|
func generatePathParameterConfig(swaggerInfo *define.Swagger, pathConfig *define.SwaggerPathInput) {
|
||||||
|
swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Parameters = make([]*define.SwaggerPathConfigParameter, 0)
|
||||||
|
hasDealTable := map[string]bool{}
|
||||||
|
|
||||||
|
for _, itemParamInput := range pathConfig.ParameterList {
|
||||||
|
if len(itemParamInput.Name) == 0 {
|
||||||
|
// 未指定参数名, 忽略
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// name 可能是 x.x.x 递归数组, 或者 x.x.[].x
|
||||||
|
generateParameterDefinitions(swaggerInfo, pathConfig.Uri, "", itemParamInput.Name, itemParamInput)
|
||||||
|
namePath := strings.Split(itemParamInput.Name, ".")
|
||||||
|
if _, exist := hasDealTable[namePath[0]]; !exist {
|
||||||
|
hasDealTable[namePath[0]] = true
|
||||||
|
swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Parameters = append(swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Parameters, &define.SwaggerPathConfigParameter{
|
||||||
|
Type: itemParamInput.Type,
|
||||||
|
Description: itemParamInput.Description,
|
||||||
|
Name: namePath[0],
|
||||||
|
In: itemParamInput.In,
|
||||||
|
Required: itemParamInput.Required,
|
||||||
|
Schema: map[string]string{
|
||||||
|
// "$ref": "#/definitions/" + pathConfig.Uri + ".input",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// generateParameterDefinitions 生成请求参数的描述
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 14:06 2024/4/22
|
||||||
|
func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, parentPath string, subPath string, paramConfig *define.SwaggerParameterInput) {
|
||||||
|
if nil == swaggerInfo.Definitions {
|
||||||
|
swaggerInfo.Definitions = map[string]*define.SwaggerDefinition{}
|
||||||
|
}
|
||||||
|
subPathArr := strings.Split(subPath, ".")
|
||||||
|
if _, exist := swaggerInfo.Definitions[parentPath]; !exist && len(parentPath) > 0 {
|
||||||
|
swaggerInfo.Definitions[parentPath] = &define.SwaggerDefinition{
|
||||||
|
Type: "object",
|
||||||
|
Required: make([]string, 0),
|
||||||
|
Properties: make(map[string]*define.SwaggerDefinitionProperty),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(subPathArr) == 1 {
|
||||||
|
if len(parentPath) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
swaggerInfo.Definitions[parentPath].Type = paramConfig.Type
|
||||||
|
if paramConfig.Required {
|
||||||
|
swaggerInfo.Definitions[parentPath].Required = append(swaggerInfo.Definitions[parentPath].Required, paramConfig.Name)
|
||||||
|
}
|
||||||
|
/*swaggerInfo.Definitions[parentPath].Properties[paramConfig.Name] = &define.SwaggerDefinitionProperty{
|
||||||
|
Description: paramConfig.Description,
|
||||||
|
Type: paramConfig.Type,
|
||||||
|
}*/
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(parentPath) == 0 {
|
||||||
|
parentPath = uri + ".input"
|
||||||
|
}
|
||||||
|
if len(subPathArr) == 2 {
|
||||||
|
if subPathArr[1] == "[]" {
|
||||||
|
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{
|
||||||
|
Description: paramConfig.Description,
|
||||||
|
Type: "Array",
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
generateParameterDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), paramConfig)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
generateParameterDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), paramConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUri(uri string) string {
|
||||||
|
return "/" + strings.TrimLeft(uri, "/")
|
||||||
|
}
|
||||||
|
63
swagger/run_test.go
Normal file
63
swagger/run_test.go
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
// Package swagger ...
|
||||||
|
//
|
||||||
|
// Description : swagger ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2024-04-22 16:02
|
||||||
|
package swagger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"git.zhangdeman.cn/gateway/api-doc/define"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGenerate(t *testing.T) {
|
||||||
|
res, _ := Generate(&define.SwaggerInput{
|
||||||
|
Schemes: []string{},
|
||||||
|
Swagger: define.SwaggerVersion2,
|
||||||
|
Host: "http://www.baidu.com",
|
||||||
|
BasePath: "/",
|
||||||
|
Info: define.Info{
|
||||||
|
Description: "测试",
|
||||||
|
Title: "测试",
|
||||||
|
Contact: define.Contact{
|
||||||
|
Name: "白茶",
|
||||||
|
Url: "http://www.baidu.com",
|
||||||
|
Email: "go@email.com",
|
||||||
|
},
|
||||||
|
License: define.License{
|
||||||
|
Name: consts.LicenseApache20,
|
||||||
|
Url: consts.LicenseUrlTable[consts.LicenseApache20],
|
||||||
|
},
|
||||||
|
Version: "",
|
||||||
|
},
|
||||||
|
PathConfigList: []*define.SwaggerPathInput{
|
||||||
|
&define.SwaggerPathInput{
|
||||||
|
Uri: "/test",
|
||||||
|
Method: http.MethodPost,
|
||||||
|
ContentType: consts.MimeTypeJson,
|
||||||
|
Summary: "测试接口",
|
||||||
|
Description: "测试接口",
|
||||||
|
TagList: []string{"test"},
|
||||||
|
ParameterList: []*define.SwaggerParameterInput{
|
||||||
|
&define.SwaggerParameterInput{
|
||||||
|
Type: consts.DataTypeString,
|
||||||
|
Description: "姓名",
|
||||||
|
Name: "name",
|
||||||
|
In: "body",
|
||||||
|
Required: true,
|
||||||
|
EnumList: []interface{}{"zhang", "de", "man"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ResponseList: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
byteData, _ := json.MarshalIndent(res, "", "\t")
|
||||||
|
fmt.Println(string(byteData))
|
||||||
|
}
|
19
util/tool.go
19
util/tool.go
@ -43,3 +43,22 @@ func GetParameterDefaultLocation(requestMethod string) string {
|
|||||||
}
|
}
|
||||||
return consts.RequestLocationQuery
|
return consts.RequestLocationQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetSwaggerType 基于输入的类型,
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 12:23 2024/4/22
|
||||||
|
func GetSwaggerType(inputType string) string {
|
||||||
|
convertTable := map[string]string{
|
||||||
|
consts.DataTypeString: "string",
|
||||||
|
consts.DataTypeInt: "integer",
|
||||||
|
consts.DataTypeUint: "integer",
|
||||||
|
consts.DataTypeFloat: "number",
|
||||||
|
consts.DataTypeBool: "boolean",
|
||||||
|
}
|
||||||
|
if _, exist := convertTable[inputType]; exist {
|
||||||
|
return convertTable[inputType]
|
||||||
|
}
|
||||||
|
return inputType
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user