完成基础的返回结果数据机构解析

This commit is contained in:
白茶清欢 2024-12-25 11:55:22 +08:00
parent 3722798c42
commit cc4ef13e23
4 changed files with 72 additions and 23 deletions

View File

@ -8,6 +8,7 @@
package api_doc
import (
"fmt"
"git.zhangdeman.cn/gateway/api-doc/define"
"git.zhangdeman.cn/zhangdeman/consts"
"strings"
@ -39,12 +40,12 @@ func GetUriPathParamList(uriPath string) []*define.ParamConfig {
return result
}
// GetParamType 将文档配置的数据类型转换为归一化处理后的数据类型
// GetDataType 将文档配置的数据类型转换为归一化处理后的数据类型
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:52 2024/12/24
func GetParamType(docParamType string, formatType string) consts.DataType {
func GetDataType(docParamType string, formatType string) consts.DataType {
docParamType = strings.ToLower(docParamType)
formatType = strings.ToLower(formatType)
if len(formatType) == 0 {
@ -60,6 +61,10 @@ func GetParamType(docParamType string, formatType string) consts.DataType {
return consts.DataTypeString
case "object":
return consts.DataTypeMapStrAny
case "boolean":
return consts.DataTypeBool
case "number", "float", "double", "float32", "float64":
return consts.DataTypeFloat
case "array":
if formatType == "integer" {
return consts.DataTypeSliceInt
@ -105,3 +110,13 @@ func GetRealDefinitionsKey(ref string) string {
func GetRealResponseKey(ref string) string {
return strings.TrimPrefix(ref, "#/responses/")
}
// GetSuccessResponseConfig 获取成功的响应配置
func GetSuccessResponseConfig(resultConfig map[string]*define.SwaggerPathConfigResponse) *define.SwaggerPathConfigResponse {
for httpCode := 200; httpCode <= 299; httpCode++ {
if cfg := resultConfig[fmt.Sprintf("%d", httpCode)]; nil != cfg {
return cfg
}
}
return nil
}

4
go.mod
View File

@ -3,10 +3,10 @@ module git.zhangdeman.cn/gateway/api-doc
go 1.22.2
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125100843-b1b286c7a701
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241225030757-c00354fe4630
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240422034417-8c922be06d95
github.com/tidwall/gjson v1.16.0
github.com/tidwall/gjson v1.18.0
)
require (

4
go.sum
View File

@ -1,5 +1,7 @@
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125100843-b1b286c7a701 h1:G+lGQmjMOBWGspZfijZvenGUAKpjBBrkRXLg3+GZp0U=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125100843-b1b286c7a701/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241225030757-c00354fe4630 h1:KTp27pHr25X5khTi4B/OdFv/34UWkq6sgDfz5aBtj74=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241225030757-c00354fe4630/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd h1:q7GG14qgXKB4MEXQFOe7/UYebsqMfPaSX80TcPdOosI=
@ -28,6 +30,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/gjson v1.16.0 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg=
github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=

View File

@ -99,7 +99,7 @@ func buildSwagger2ParamConfig(swaggerDoc *define.Swagger, paramConfigList []*def
paramConfigBuildConfig := &define.ParamConfig{
Location: GetParamLocation(paramConfig.In).String(),
Path: paramConfig.Name,
Type: GetParamType(paramConfig.Type, paramConfig.Format).String(),
Type: GetDataType(paramConfig.Type, paramConfig.Format).String(),
Title: paramConfig.Name,
Description: paramConfig.Description,
Required: paramConfig.Required,
@ -122,7 +122,7 @@ func buildSwagger2ParamConfig(swaggerDoc *define.Swagger, paramConfigList []*def
paramConfigBuildConfig := &define.ParamConfig{
Location: GetParamLocation(paramConfig.In).String(),
Path: paramName,
Type: GetParamType(paramMoreConfig.Type, "").String(),
Type: GetDataType(paramMoreConfig.Type, "").String(),
Title: paramMoreConfig.Description,
Description: paramMoreConfig.Description,
Required: requiredTable[paramName],
@ -140,19 +140,26 @@ func buildSwagger2ResultConfig(swaggerDoc *define.Swagger, uri string, resultCon
if nil == resultConfig || len(resultConfig) == 0 {
return res
}
if _, exist := resultConfig["200"]; !exist {
result204Config := resultConfig["204"]
resultConfig["200"] = result204Config
}
if nil == resultConfig["200"] {
successResponseConfig := GetSuccessResponseConfig(resultConfig)
if nil == successResponseConfig {
return res
}
definitionsKey := ""
if swaggerDoc.Responses == nil {
definitionsKey = GetRealDefinitionsKey(resultConfig["200"].Schema.Ref)
definitionsKey = successResponseConfig.Schema.Ref
} else {
responseKey := GetRealResponseKey(resultConfig["200"].Ref)
responseKey := GetRealResponseKey(successResponseConfig.Ref)
if len(responseKey) == 0 {
// 204 场景下可能没有响应body
res = append(res, &define.ResultConfig{
Location: consts.ResponseDataLocationBody.String(),
Path: consts.ResponseDataLocationBodyRoot.String(),
Type: consts.DataTypeAny.String(),
Title: "response body no content",
Description: "no content",
})
return res
}
ref := swaggerDoc.Responses[responseKey].Ref
if len(ref) == 0 {
if nil != swaggerDoc.Responses[responseKey].Schema {
@ -163,14 +170,37 @@ func buildSwagger2ResultConfig(swaggerDoc *define.Swagger, uri string, resultCon
}
if len(definitionsKey) == 0 {
// 不是引用类型, 直接定义的具体类型
res = append(res, &define.ResultConfig{
Location: consts.ResponseDataLocationBody.String(),
Path: "",
Type: "",
Title: "",
Description: "",
})
responseKey := GetRealResponseKey(successResponseConfig.Ref)
responseTypeDefine := swaggerDoc.Responses[responseKey]
if nil == responseTypeDefine.Schema {
// 204 场景下可能没有响应body
res = append(res, &define.ResultConfig{
Location: consts.ResponseDataLocationBody.String(),
Path: consts.ResponseDataLocationBodyRoot.String(),
Type: consts.DataTypeAny.String(),
Title: "response body",
Description: responseTypeDefine.Description,
})
} else {
res = append(res, &define.ResultConfig{
Location: consts.ResponseDataLocationBody.String(),
Path: consts.ResponseDataLocationBodyRoot.String(),
Type: GetDataType(responseTypeDefine.Schema.Type, "").String(),
Title: "response body",
Description: responseTypeDefine.Description,
})
}
} else {
responseTypeDefine := swaggerDoc.Definitions[definitionsKey]
for responseKey, responseKeyConfig := range responseTypeDefine.Properties {
res = append(res, &define.ResultConfig{
Location: consts.ResponseDataLocationBody.String(),
Path: responseKey,
Type: GetDataType(responseKeyConfig.Type, "").String(),
Title: responseKey,
Description: responseKeyConfig.Description,
})
}
}
return nil
return res
}