swagger2版本文档解析1.0版本 #2
19
common.go
19
common.go
@ -8,6 +8,7 @@
|
|||||||
package api_doc
|
package api_doc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"git.zhangdeman.cn/gateway/api-doc/define"
|
"git.zhangdeman.cn/gateway/api-doc/define"
|
||||||
"git.zhangdeman.cn/zhangdeman/consts"
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
"strings"
|
"strings"
|
||||||
@ -39,12 +40,12 @@ func GetUriPathParamList(uriPath string) []*define.ParamConfig {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetParamType 将文档配置的数据类型转换为归一化处理后的数据类型
|
// GetDataType 将文档配置的数据类型转换为归一化处理后的数据类型
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 11:52 2024/12/24
|
// 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)
|
docParamType = strings.ToLower(docParamType)
|
||||||
formatType = strings.ToLower(formatType)
|
formatType = strings.ToLower(formatType)
|
||||||
if len(formatType) == 0 {
|
if len(formatType) == 0 {
|
||||||
@ -60,6 +61,10 @@ func GetParamType(docParamType string, formatType string) consts.DataType {
|
|||||||
return consts.DataTypeString
|
return consts.DataTypeString
|
||||||
case "object":
|
case "object":
|
||||||
return consts.DataTypeMapStrAny
|
return consts.DataTypeMapStrAny
|
||||||
|
case "boolean":
|
||||||
|
return consts.DataTypeBool
|
||||||
|
case "number", "float", "double", "float32", "float64":
|
||||||
|
return consts.DataTypeFloat
|
||||||
case "array":
|
case "array":
|
||||||
if formatType == "integer" {
|
if formatType == "integer" {
|
||||||
return consts.DataTypeSliceInt
|
return consts.DataTypeSliceInt
|
||||||
@ -105,3 +110,13 @@ func GetRealDefinitionsKey(ref string) string {
|
|||||||
func GetRealResponseKey(ref string) string {
|
func GetRealResponseKey(ref string) string {
|
||||||
return strings.TrimPrefix(ref, "#/responses/")
|
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
4
go.mod
@ -3,10 +3,10 @@ module git.zhangdeman.cn/gateway/api-doc
|
|||||||
go 1.22.2
|
go 1.22.2
|
||||||
|
|
||||||
require (
|
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/serialize v0.0.0-20241223084948-de2e49144fcd
|
||||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240422034417-8c922be06d95
|
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 (
|
require (
|
||||||
|
4
go.sum
4
go.sum
@ -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 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-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 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
|
||||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
|
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=
|
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/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 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg=
|
||||||
github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
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 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||||
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||||
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
|
62
swagger.go
62
swagger.go
@ -99,7 +99,7 @@ func buildSwagger2ParamConfig(swaggerDoc *define.Swagger, paramConfigList []*def
|
|||||||
paramConfigBuildConfig := &define.ParamConfig{
|
paramConfigBuildConfig := &define.ParamConfig{
|
||||||
Location: GetParamLocation(paramConfig.In).String(),
|
Location: GetParamLocation(paramConfig.In).String(),
|
||||||
Path: paramConfig.Name,
|
Path: paramConfig.Name,
|
||||||
Type: GetParamType(paramConfig.Type, paramConfig.Format).String(),
|
Type: GetDataType(paramConfig.Type, paramConfig.Format).String(),
|
||||||
Title: paramConfig.Name,
|
Title: paramConfig.Name,
|
||||||
Description: paramConfig.Description,
|
Description: paramConfig.Description,
|
||||||
Required: paramConfig.Required,
|
Required: paramConfig.Required,
|
||||||
@ -122,7 +122,7 @@ func buildSwagger2ParamConfig(swaggerDoc *define.Swagger, paramConfigList []*def
|
|||||||
paramConfigBuildConfig := &define.ParamConfig{
|
paramConfigBuildConfig := &define.ParamConfig{
|
||||||
Location: GetParamLocation(paramConfig.In).String(),
|
Location: GetParamLocation(paramConfig.In).String(),
|
||||||
Path: paramName,
|
Path: paramName,
|
||||||
Type: GetParamType(paramMoreConfig.Type, "").String(),
|
Type: GetDataType(paramMoreConfig.Type, "").String(),
|
||||||
Title: paramMoreConfig.Description,
|
Title: paramMoreConfig.Description,
|
||||||
Description: paramMoreConfig.Description,
|
Description: paramMoreConfig.Description,
|
||||||
Required: requiredTable[paramName],
|
Required: requiredTable[paramName],
|
||||||
@ -140,19 +140,26 @@ func buildSwagger2ResultConfig(swaggerDoc *define.Swagger, uri string, resultCon
|
|||||||
if nil == resultConfig || len(resultConfig) == 0 {
|
if nil == resultConfig || len(resultConfig) == 0 {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
if _, exist := resultConfig["200"]; !exist {
|
successResponseConfig := GetSuccessResponseConfig(resultConfig)
|
||||||
result204Config := resultConfig["204"]
|
if nil == successResponseConfig {
|
||||||
resultConfig["200"] = result204Config
|
|
||||||
}
|
|
||||||
|
|
||||||
if nil == resultConfig["200"] {
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
definitionsKey := ""
|
definitionsKey := ""
|
||||||
if swaggerDoc.Responses == nil {
|
if swaggerDoc.Responses == nil {
|
||||||
definitionsKey = GetRealDefinitionsKey(resultConfig["200"].Schema.Ref)
|
definitionsKey = successResponseConfig.Schema.Ref
|
||||||
} else {
|
} 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
|
ref := swaggerDoc.Responses[responseKey].Ref
|
||||||
if len(ref) == 0 {
|
if len(ref) == 0 {
|
||||||
if nil != swaggerDoc.Responses[responseKey].Schema {
|
if nil != swaggerDoc.Responses[responseKey].Schema {
|
||||||
@ -163,14 +170,37 @@ func buildSwagger2ResultConfig(swaggerDoc *define.Swagger, uri string, resultCon
|
|||||||
}
|
}
|
||||||
if len(definitionsKey) == 0 {
|
if len(definitionsKey) == 0 {
|
||||||
// 不是引用类型, 直接定义的具体类型
|
// 不是引用类型, 直接定义的具体类型
|
||||||
|
responseKey := GetRealResponseKey(successResponseConfig.Ref)
|
||||||
|
responseTypeDefine := swaggerDoc.Responses[responseKey]
|
||||||
|
if nil == responseTypeDefine.Schema {
|
||||||
|
// 204 场景下,可能没有响应body
|
||||||
res = append(res, &define.ResultConfig{
|
res = append(res, &define.ResultConfig{
|
||||||
Location: consts.ResponseDataLocationBody.String(),
|
Location: consts.ResponseDataLocationBody.String(),
|
||||||
Path: "",
|
Path: consts.ResponseDataLocationBodyRoot.String(),
|
||||||
Type: "",
|
Type: consts.DataTypeAny.String(),
|
||||||
Title: "",
|
Title: "response body",
|
||||||
Description: "",
|
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 {
|
||||||
return nil
|
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 res
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user