修复对象嵌套的BUG
This commit is contained in:
parent
ee48b651a8
commit
4ef4e5ae3b
@ -97,8 +97,10 @@ type SwaggerDefinition struct {
|
|||||||
//
|
//
|
||||||
// Date : 17:25 2024/4/19
|
// Date : 17:25 2024/4/19
|
||||||
type SwaggerDefinitionProperty struct {
|
type SwaggerDefinitionProperty struct {
|
||||||
Description string `json:"description"` // 描述
|
Description string `json:"description"` // 描述
|
||||||
Type string `json:"type"` // 类型
|
Type string `json:"type"` // 类型
|
||||||
|
Items map[string]string `json:"items,omitempty"` // 引用类型中的引用(数组)
|
||||||
|
AllOf []map[string]string `json:"allOf,omitempty"` // 引用类型中的引用(对象)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Swagger 文档整体结构定义
|
// Swagger 文档整体结构定义
|
||||||
|
@ -189,9 +189,14 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
|
|||||||
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[parentPath] = &define.SwaggerDefinition{
|
swaggerInfo.Definitions[checkPath] = &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),
|
||||||
@ -230,7 +235,29 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
|
|||||||
Type: "Array",
|
Type: "Array",
|
||||||
}
|
}
|
||||||
} else {
|
} 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
|
return
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,14 @@ func TestGenerate(t *testing.T) {
|
|||||||
Required: true,
|
Required: true,
|
||||||
EnumList: []interface{}{"zhang", "de", "man"},
|
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{
|
&define.SwaggerParameterInput{
|
||||||
Type: consts.DataTypeInt,
|
Type: consts.DataTypeInt,
|
||||||
Description: "年龄",
|
Description: "年龄",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user