合并response define 生成 和 request define生成
This commit is contained in:
parent
987758a64c
commit
f04648f3e9
178
swagger/run.go
178
swagger/run.go
@ -46,8 +46,8 @@ func formatDocConfig(docConfig *define.SwaggerInput) {
|
||||
docConfig.Schemes = []string{consts.SchemeHTTP}
|
||||
}
|
||||
docConfig.Host = wrapper.String(docConfig.Host).ReplaceChar(map[string]string{
|
||||
"http://": "",
|
||||
"https://": "",
|
||||
consts.SchemeHTTP + "://": "",
|
||||
consts.SchemeHTTPS + "://": "",
|
||||
}).Value()
|
||||
for _, itemPath := range docConfig.PathConfigList {
|
||||
// 默认请求类型 application/json
|
||||
@ -163,7 +163,14 @@ func generatePathParameterConfig(swaggerInfo *define.Swagger, pathConfig *define
|
||||
}
|
||||
// name 可能是 x.x.x 递归数组, 或者 x.x.[].x
|
||||
outputDefine := strings.TrimLeft(pathConfig.Uri, "/") + "." + itemResponseConfig.Code + ".output"
|
||||
generateResponseDefinitions(swaggerInfo, pathConfig.Uri, outputDefine, itemResponseInput.Field, itemResponseInput)
|
||||
generateParameterDefinitions(swaggerInfo, pathConfig.Uri, outputDefine, itemResponseInput.Field, &define.SwaggerParameterInput{
|
||||
Type: itemResponseInput.Type,
|
||||
Description: itemResponseInput.Description,
|
||||
Name: itemResponseInput.Field,
|
||||
In: consts.RequestLocationBody,
|
||||
Required: false,
|
||||
EnumList: nil,
|
||||
})
|
||||
namePath := strings.Split(itemResponseInput.Field, ".")
|
||||
if _, exist := hasDealResponseTable[namePath[0]]; !exist {
|
||||
hasDealResponseTable[namePath[0]] = true
|
||||
@ -314,131 +321,46 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
|
||||
generateParameterDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), paramConfig)
|
||||
}
|
||||
|
||||
// generateResponseDefinitions ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:28 2024/4/22
|
||||
func generateResponseDefinitions(swaggerInfo *define.Swagger, uri string, parentPath string, subPath string, responseConfig *define.SwaggerResponseItemInput) {
|
||||
uri = strings.TrimLeft(uri, "/")
|
||||
parentPath = strings.TrimLeft(parentPath, "/")
|
||||
if nil == swaggerInfo.Definitions {
|
||||
swaggerInfo.Definitions = map[string]*define.SwaggerDefinition{}
|
||||
}
|
||||
parentPathArr := strings.Split(parentPath, ".")
|
||||
checkPath := parentPath
|
||||
if len(parentPathArr) >= 3 {
|
||||
checkPath = strings.Join([]string{parentPathArr[0], parentPathArr[1], parentPathArr[2]}, ".")
|
||||
}
|
||||
subPathArr := strings.Split(subPath, ".")
|
||||
if _, exist := swaggerInfo.Definitions[checkPath]; !exist && len(parentPath) > 0 {
|
||||
swaggerInfo.Definitions[checkPath] = &define.SwaggerDefinition{
|
||||
Type: "object",
|
||||
Required: make([]string, 0),
|
||||
Properties: make(map[string]*define.SwaggerDefinitionProperty),
|
||||
}
|
||||
}
|
||||
|
||||
if len(subPathArr) == 1 {
|
||||
if _, exist := swaggerInfo.Definitions[parentPath]; !exist {
|
||||
swaggerInfo.Definitions[parentPath] = &define.SwaggerDefinition{
|
||||
Type: "object",
|
||||
Required: make([]string, 0),
|
||||
Properties: make(map[string]*define.SwaggerDefinitionProperty),
|
||||
}
|
||||
}
|
||||
swaggerInfo.Definitions[parentPath].Type = "object"
|
||||
if responseConfig.IsRequired {
|
||||
swaggerInfo.Definitions[parentPath].Required = append(swaggerInfo.Definitions[parentPath].Required, subPath)
|
||||
}
|
||||
swaggerInfo.Definitions[parentPath].Properties[subPath] = &define.SwaggerDefinitionProperty{
|
||||
Description: responseConfig.Description,
|
||||
Type: util.GetSwaggerType(responseConfig.Type),
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(parentPath) == 0 {
|
||||
parentPath = uri + ".output"
|
||||
}
|
||||
if len(subPathArr) == 2 {
|
||||
if _, exist := swaggerInfo.Definitions[parentPath]; !exist {
|
||||
swaggerInfo.Definitions[parentPath] = &define.SwaggerDefinition{
|
||||
Type: "object",
|
||||
Required: make([]string, 0),
|
||||
Properties: make(map[string]*define.SwaggerDefinitionProperty),
|
||||
}
|
||||
}
|
||||
if subPathArr[1] == "[]" {
|
||||
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{
|
||||
Description: responseConfig.Description,
|
||||
Type: "array",
|
||||
Items: map[string]string{
|
||||
"type": util.GetSwaggerType(responseConfig.Type),
|
||||
},
|
||||
}
|
||||
} else {
|
||||
if subPathArr[0] == "[]" {
|
||||
generateResponseDefinitions(swaggerInfo, uri, parentPath+".item", strings.Join(subPathArr[1:], "."), responseConfig)
|
||||
return
|
||||
} else {
|
||||
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{
|
||||
Description: "返回值描述",
|
||||
Type: "object",
|
||||
AllOf: []map[string]string{map[string]string{
|
||||
"$ref": "#/definitions/" + parentPath + "." + subPathArr[0],
|
||||
}},
|
||||
}
|
||||
}
|
||||
storageSubPath := parentPath + "." + subPathArr[0]
|
||||
if subPathArr[1] == "[]" {
|
||||
storageSubPath += ".item"
|
||||
}
|
||||
if _, exist := swaggerInfo.Definitions[storageSubPath]; !exist {
|
||||
swaggerInfo.Definitions[storageSubPath] = &define.SwaggerDefinition{
|
||||
Type: wrapper.TernaryOperator.String(strings.HasSuffix(parentPath, "[]"), "array", "object").Value(),
|
||||
Required: make([]string, 0),
|
||||
Properties: make(map[string]*define.SwaggerDefinitionProperty),
|
||||
}
|
||||
}
|
||||
if responseConfig.IsRequired {
|
||||
swaggerInfo.Definitions[storageSubPath].Required = append(swaggerInfo.Definitions[storageSubPath].Required, subPathArr[1])
|
||||
}
|
||||
swaggerInfo.Definitions[storageSubPath].Properties[subPathArr[1]] = &define.SwaggerDefinitionProperty{
|
||||
Description: responseConfig.Description,
|
||||
Type: util.GetSwaggerType(responseConfig.Type),
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if _, exist := swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]]; !exist {
|
||||
if subPathArr[1] == "[]" {
|
||||
nextParentPath := parentPath + "." + subPathArr[0] + ".item"
|
||||
generateResponseDefinitions(swaggerInfo, uri, nextParentPath, strings.Join(subPathArr[2:], "."), responseConfig)
|
||||
return
|
||||
}
|
||||
itemSwaggerDefinition := &define.SwaggerDefinitionProperty{
|
||||
Description: "对象描述",
|
||||
Type: wrapper.TernaryOperator.String(subPathArr[1] == "[]", "array", "object").Value(),
|
||||
Items: nil,
|
||||
AllOf: nil,
|
||||
}
|
||||
if itemSwaggerDefinition.Type == "object" {
|
||||
itemSwaggerDefinition.AllOf = []map[string]string{map[string]string{
|
||||
"$ref": "#/definitions/" + parentPath + "." + subPathArr[0],
|
||||
}}
|
||||
} else if itemSwaggerDefinition.Type == "array" {
|
||||
itemSwaggerDefinition.Description = "数组描述"
|
||||
itemSwaggerDefinition.Items = map[string]string{
|
||||
"$ref": "#/definitions/" + parentPath + "." + subPathArr[0] + ".item",
|
||||
}
|
||||
}
|
||||
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = itemSwaggerDefinition
|
||||
}
|
||||
// fmt.Println(parentPath + "." + subPathArr[0])
|
||||
generateResponseDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), responseConfig)
|
||||
}
|
||||
|
||||
func getUri(uri string) string {
|
||||
return "/" + strings.TrimLeft(uri, "/")
|
||||
}
|
||||
|
||||
// setGlobalMapDefinition ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:38 2024/4/25
|
||||
func setGlobalMapDefinition(swaggerInfo *define.Swagger) {
|
||||
if nil == swaggerInfo.Definitions {
|
||||
swaggerInfo.Definitions = map[string]*define.SwaggerDefinition{}
|
||||
}
|
||||
if _, exist := swaggerInfo.Definitions["global_map"]; exist {
|
||||
return
|
||||
}
|
||||
swaggerInfo.Definitions["global_map"] = &define.SwaggerDefinition{
|
||||
Type: "object",
|
||||
Required: nil,
|
||||
Properties: make(map[string]*define.SwaggerDefinitionProperty),
|
||||
}
|
||||
}
|
||||
|
||||
// isGlobalMapType ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:46 2024/4/25
|
||||
func isGlobalMapType(path string, dataType string) bool {
|
||||
pathArr := strings.Split(path, ".")
|
||||
if len(pathArr) > 2 {
|
||||
return false
|
||||
}
|
||||
return wrapper.ArrayType([]string{
|
||||
consts.DataTypeMapAnyAny,
|
||||
consts.DataTypeMapStrUint,
|
||||
consts.DataTypeMapStrInt,
|
||||
consts.DataTypeMapStrSlice,
|
||||
consts.DataTypeMapStrFloat,
|
||||
consts.DataTypeMapStrBool,
|
||||
consts.DataTypeMapStrAny,
|
||||
}).Has(dataType) >= 0
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user