修复list参数不显示问题

This commit is contained in:
白茶清欢 2024-04-23 20:47:10 +08:00
parent a3038b693e
commit 0744c5cd5b
2 changed files with 110 additions and 12 deletions

View File

@ -217,7 +217,7 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
} }
swaggerInfo.Definitions[parentPath].Type = "object" swaggerInfo.Definitions[parentPath].Type = "object"
if paramConfig.Required { if paramConfig.Required {
swaggerInfo.Definitions[parentPath].Required = append(swaggerInfo.Definitions[parentPath].Required, paramConfig.Name) swaggerInfo.Definitions[parentPath].Required = append(swaggerInfo.Definitions[parentPath].Required, subPath)
} }
swaggerInfo.Definitions[parentPath].Properties[subPath] = &define.SwaggerDefinitionProperty{ swaggerInfo.Definitions[parentPath].Properties[subPath] = &define.SwaggerDefinitionProperty{
Description: paramConfig.Description, Description: paramConfig.Description,
@ -246,7 +246,7 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
} }
} else { } else {
if subPathArr[0] == "[]" { if subPathArr[0] == "[]" {
generateParameterDefinitions(swaggerInfo, uri, parentPath+".[]", strings.Join(subPathArr[1:], "."), paramConfig) generateParameterDefinitions(swaggerInfo, uri, parentPath+".item", strings.Join(subPathArr[1:], "."), paramConfig)
return return
} else { } else {
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{ swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{
@ -259,7 +259,7 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
} }
storageSubPath := parentPath + "." + subPathArr[0] storageSubPath := parentPath + "." + subPathArr[0]
if subPathArr[1] == "[]" { if subPathArr[1] == "[]" {
storageSubPath += ".[]" storageSubPath += ".item"
} }
if _, exist := swaggerInfo.Definitions[storageSubPath]; !exist { if _, exist := swaggerInfo.Definitions[storageSubPath]; !exist {
swaggerInfo.Definitions[storageSubPath] = &define.SwaggerDefinition{ swaggerInfo.Definitions[storageSubPath] = &define.SwaggerDefinition{
@ -280,6 +280,18 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
} }
if _, exist := swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]]; !exist { if _, exist := swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]]; !exist {
if subPathArr[1] == "[]" {
nextParentPath := parentPath + "." + subPathArr[0] + ".item"
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{
Description: "参数描述",
Type: "array",
Items: map[string]string{
"$ref": "#/definitions/" + nextParentPath,
},
}
generateParameterDefinitions(swaggerInfo, uri, nextParentPath, strings.Join(subPathArr[2:], "."), paramConfig)
return
}
itemSwaggerDefinition := &define.SwaggerDefinitionProperty{ itemSwaggerDefinition := &define.SwaggerDefinitionProperty{
Description: "对象描述", Description: "对象描述",
Type: wrapper.TernaryOperator.String(subPathArr[1] == "[]", "array", "object").Value(), Type: wrapper.TernaryOperator.String(subPathArr[1] == "[]", "array", "object").Value(),
@ -293,7 +305,7 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
} else if itemSwaggerDefinition.Type == "array" { } else if itemSwaggerDefinition.Type == "array" {
itemSwaggerDefinition.Description = "数组描述" itemSwaggerDefinition.Description = "数组描述"
itemSwaggerDefinition.Items = map[string]string{ itemSwaggerDefinition.Items = map[string]string{
"$ref": "#/definitions/" + parentPath + "." + subPathArr[0] + ".[]", "$ref": "#/definitions/" + parentPath + "." + subPathArr[0] + ".item",
} }
} }
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = itemSwaggerDefinition swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = itemSwaggerDefinition
@ -308,25 +320,40 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
// //
// Date : 18:28 2024/4/22 // Date : 18:28 2024/4/22
func generateResponseDefinitions(swaggerInfo *define.Swagger, uri string, parentPath string, subPath string, responseConfig *define.SwaggerResponseItemInput) { 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 { if nil == swaggerInfo.Definitions {
swaggerInfo.Definitions = map[string]*define.SwaggerDefinition{} swaggerInfo.Definitions = map[string]*define.SwaggerDefinition{}
} }
parentPathArr := strings.Split(parentPath, ".")
checkPath := parentPath
if len(parentPathArr) >= 2 {
checkPath = strings.Join([]string{parentPathArr[0], parentPathArr[1]}, ".")
}
subPathArr := strings.Split(subPath, ".") subPathArr := strings.Split(subPath, ".")
if _, exist := swaggerInfo.Definitions[parentPath]; !exist && len(parentPath) > 0 { 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{ swaggerInfo.Definitions[parentPath] = &define.SwaggerDefinition{
Type: "object", Type: "object",
Required: make([]string, 0), Required: make([]string, 0),
Properties: make(map[string]*define.SwaggerDefinitionProperty), Properties: make(map[string]*define.SwaggerDefinitionProperty),
} }
} }
if len(subPathArr) == 1 { swaggerInfo.Definitions[parentPath].Type = "object"
if len(parentPath) == 0 { if responseConfig.IsRequired {
return swaggerInfo.Definitions[parentPath].Required = append(swaggerInfo.Definitions[parentPath].Required, subPath)
} }
// swaggerInfo.Definitions[parentPath].Type = responseConfig.Type swaggerInfo.Definitions[parentPath].Properties[subPath] = &define.SwaggerDefinitionProperty{
swaggerInfo.Definitions[parentPath].Properties[responseConfig.Field] = &define.SwaggerDefinitionProperty{
Description: responseConfig.Description, Description: responseConfig.Description,
Type: responseConfig.Type, Type: util.GetSwaggerType(responseConfig.Type),
} }
return return
} }
@ -334,16 +361,81 @@ func generateResponseDefinitions(swaggerInfo *define.Swagger, uri string, parent
parentPath = uri + ".output" parentPath = uri + ".output"
} }
if len(subPathArr) == 2 { 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] == "[]" { if subPathArr[1] == "[]" {
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{ swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{
Description: responseConfig.Description, Description: responseConfig.Description,
Type: "array", Type: "array",
Items: map[string]string{
"type": util.GetSwaggerType(responseConfig.Type),
},
} }
} else { } else {
generateResponseDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), responseConfig) 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 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) generateResponseDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), responseConfig)
} }

View File

@ -121,6 +121,12 @@ func TestGenerate(t *testing.T) {
Field: "nick_name", Field: "nick_name",
IsRequired: false, IsRequired: false,
}, },
&define.SwaggerResponseItemInput{
Type: consts.DataTypeString,
Description: "昵称",
Field: "person.nick_name",
IsRequired: false,
},
}, },
}, },
}, },