修复对象数组文档生成的BUg

This commit is contained in:
白茶清欢 2024-04-23 16:53:40 +08:00
parent c46e7ee8de
commit a3038b693e
2 changed files with 46 additions and 14 deletions

View File

@ -219,7 +219,7 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
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, paramConfig.Name)
} }
swaggerInfo.Definitions[parentPath].Properties[paramConfig.Name] = &define.SwaggerDefinitionProperty{ swaggerInfo.Definitions[parentPath].Properties[subPath] = &define.SwaggerDefinitionProperty{
Description: paramConfig.Description, Description: paramConfig.Description,
Type: util.GetSwaggerType(paramConfig.Type), Type: util.GetSwaggerType(paramConfig.Type),
} }
@ -244,6 +244,10 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
"type": util.GetSwaggerType(paramConfig.Type), "type": util.GetSwaggerType(paramConfig.Type),
}, },
} }
} else {
if subPathArr[0] == "[]" {
generateParameterDefinitions(swaggerInfo, uri, parentPath+".[]", strings.Join(subPathArr[1:], "."), paramConfig)
return
} else { } else {
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{ swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{
Description: "参数描述", Description: "参数描述",
@ -252,11 +256,14 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
"$ref": "#/definitions/" + parentPath + "." + subPathArr[0], "$ref": "#/definitions/" + parentPath + "." + subPathArr[0],
}}, }},
} }
}
storageSubPath := parentPath + "." + subPathArr[0] storageSubPath := parentPath + "." + subPathArr[0]
if subPathArr[1] == "[]" {
storageSubPath += ".[]"
}
if _, exist := swaggerInfo.Definitions[storageSubPath]; !exist { if _, exist := swaggerInfo.Definitions[storageSubPath]; !exist {
swaggerInfo.Definitions[storageSubPath] = &define.SwaggerDefinition{ swaggerInfo.Definitions[storageSubPath] = &define.SwaggerDefinition{
Type: "object", Type: wrapper.TernaryOperator.String(strings.HasSuffix(parentPath, "[]"), "array", "object").Value(),
Required: make([]string, 0), Required: make([]string, 0),
Properties: make(map[string]*define.SwaggerDefinitionProperty), Properties: make(map[string]*define.SwaggerDefinitionProperty),
} }
@ -273,14 +280,23 @@ 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 {
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{ itemSwaggerDefinition := &define.SwaggerDefinitionProperty{
Description: "对象描述", Description: "对象描述",
Type: "object", Type: wrapper.TernaryOperator.String(subPathArr[1] == "[]", "array", "object").Value(),
Items: nil, Items: nil,
AllOf: []map[string]string{map[string]string{ AllOf: nil,
"$ref": "#/definitions/" + parentPath + "." + subPathArr[0],
}},
} }
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] + ".[]",
}
}
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = itemSwaggerDefinition
} }
// fmt.Println(parentPath + "." + subPathArr[0]) // fmt.Println(parentPath + "." + subPathArr[0])
generateParameterDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), paramConfig) generateParameterDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), paramConfig)

View File

@ -93,6 +93,22 @@ func TestGenerate(t *testing.T) {
Required: true, Required: true,
EnumList: []interface{}{18, 19, 20}, EnumList: []interface{}{18, 19, 20},
}, },
&define.SwaggerParameterInput{
Type: consts.DataTypeInt,
Description: "年龄",
Name: "test_list.[].age",
In: "body",
Required: true,
EnumList: []interface{}{18, 19, 20},
},
&define.SwaggerParameterInput{
Type: consts.DataTypeString,
Description: "年龄",
Name: "test_list.[].name",
In: "body",
Required: true,
EnumList: nil,
},
}, },
ResponseList: []*define.SwaggerResponseInput{ ResponseList: []*define.SwaggerResponseInput{
&define.SwaggerResponseInput{ &define.SwaggerResponseInput{