修复对象嵌套的BUG

This commit is contained in:
白茶清欢 2024-04-23 14:43:23 +08:00
parent ee48b651a8
commit 4ef4e5ae3b
3 changed files with 42 additions and 5 deletions

View File

@ -97,8 +97,10 @@ type SwaggerDefinition struct {
//
// Date : 17:25 2024/4/19
type SwaggerDefinitionProperty struct {
Description string `json:"description"` // 描述
Type string `json:"type"` // 类型
Description string `json:"description"` // 描述
Type string `json:"type"` // 类型
Items map[string]string `json:"items,omitempty"` // 引用类型中的引用(数组)
AllOf []map[string]string `json:"allOf,omitempty"` // 引用类型中的引用(对象)
}
// Swagger 文档整体结构定义

View File

@ -189,9 +189,14 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
if nil == swaggerInfo.Definitions {
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, ".")
if _, exist := swaggerInfo.Definitions[parentPath]; !exist && len(parentPath) > 0 {
swaggerInfo.Definitions[parentPath] = &define.SwaggerDefinition{
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),
@ -230,7 +235,29 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
Type: "Array",
}
} else {
generateParameterDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), paramConfig)
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 _, exist := swaggerInfo.Definitions[storageSubPath]; !exist {
swaggerInfo.Definitions[storageSubPath] = &define.SwaggerDefinition{
Type: "object",
Required: make([]string, 0),
Properties: make(map[string]*define.SwaggerDefinitionProperty),
}
}
if paramConfig.Required {
swaggerInfo.Definitions[storageSubPath].Required = append(swaggerInfo.Definitions[storageSubPath].Required, subPathArr[1])
}
swaggerInfo.Definitions[storageSubPath].Properties[subPathArr[1]] = &define.SwaggerDefinitionProperty{
Description: paramConfig.Description,
Type: paramConfig.Type,
}
}
return
}

View File

@ -53,6 +53,14 @@ func TestGenerate(t *testing.T) {
Required: true,
EnumList: []interface{}{"zhang", "de", "man"},
},
&define.SwaggerParameterInput{
Type: consts.DataTypeString,
Description: "性别",
Name: "person.sex",
In: "body",
Required: true,
EnumList: []interface{}{"man", "woman", "other"},
},
&define.SwaggerParameterInput{
Type: consts.DataTypeInt,
Description: "年龄",