递归生成参数结构

This commit is contained in:
白茶清欢 2024-04-23 15:54:06 +08:00
parent 4ef4e5ae3b
commit c46e7ee8de
2 changed files with 49 additions and 3 deletions

View File

@ -229,10 +229,20 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
parentPath = uri + ".input" parentPath = uri + ".input"
} }
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: paramConfig.Description, Description: paramConfig.Description,
Type: "Array", Type: "array",
Items: map[string]string{
"type": util.GetSwaggerType(paramConfig.Type),
},
} }
} else { } else {
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{ swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{
@ -256,11 +266,23 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
} }
swaggerInfo.Definitions[storageSubPath].Properties[subPathArr[1]] = &define.SwaggerDefinitionProperty{ swaggerInfo.Definitions[storageSubPath].Properties[subPathArr[1]] = &define.SwaggerDefinitionProperty{
Description: paramConfig.Description, Description: paramConfig.Description,
Type: paramConfig.Type, Type: util.GetSwaggerType(paramConfig.Type),
} }
} }
return return
} }
if _, exist := swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]]; !exist {
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{
Description: "对象描述",
Type: "object",
Items: nil,
AllOf: []map[string]string{map[string]string{
"$ref": "#/definitions/" + 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)
} }
@ -299,7 +321,7 @@ func generateResponseDefinitions(swaggerInfo *define.Swagger, uri string, parent
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",
} }
} else { } else {
generateResponseDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), responseConfig) generateResponseDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), responseConfig)

View File

@ -61,6 +61,30 @@ func TestGenerate(t *testing.T) {
Required: true, Required: true,
EnumList: []interface{}{"man", "woman", "other"}, EnumList: []interface{}{"man", "woman", "other"},
}, },
&define.SwaggerParameterInput{
Type: consts.DataTypeString,
Description: "性别",
Name: "person.job.list.[]",
In: "body",
Required: true,
EnumList: nil,
},
&define.SwaggerParameterInput{
Type: consts.DataTypeInt,
Description: "年份",
Name: "person.job.year.[]",
In: "body",
Required: true,
EnumList: nil,
},
&define.SwaggerParameterInput{
Type: consts.DataTypeInt,
Description: "测试工作",
Name: "person.job.test",
In: "body",
Required: true,
EnumList: nil,
},
&define.SwaggerParameterInput{ &define.SwaggerParameterInput{
Type: consts.DataTypeInt, Type: consts.DataTypeInt,
Description: "年龄", Description: "年龄",