修复结构体指针解析错误

This commit is contained in:
白茶清欢 2025-02-15 15:50:40 +08:00
parent d70cfe3e50
commit 7f069e3c7e
2 changed files with 22 additions and 21 deletions

View File

@ -454,32 +454,34 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
}
// 结构体
if inputType.Kind() == reflect.Struct {
if len(rootSchemaName) == 0 {
} else {
// g.docData.Components.Schemas[schemaName].Properties[""] = schemaName
}
// g.docData.Components.Schemas[schemaName].Ref = consts.SwaggerDataTypeObject
for i := 0; i < inputType.NumField(); i++ {
propertyName := ParseStructField.GetParamName(inputType.Field(i))
if propertyName == "-" {
continue
}
if inputType.Field(i).Type.Kind() == reflect.Ptr ||
inputType.Field(i).Type.Kind() == reflect.Struct ||
inputType.Field(i).Type.Kind() == reflect.Map ||
inputType.Field(i).Type.Kind() == reflect.Array ||
inputType.Field(i).Type.Kind() == reflect.Slice {
if convertType := g.realBaseType2SwaggerType(inputType.Field(i).Type.String()); !strings.HasPrefix(convertType, "[]") && convertType != inputType.Field(i).Type.Kind().String() {
// 针对基础类型指针
if inputType.Field(i).Type.Kind() == reflect.Ptr {
// 处理指针
if inputType.Field(i).Type.Elem().Kind() == reflect.Struct {
// 结构体指针
schemaNameNext := g.AddComponentsSchema(schemaName, propertyName, inputType.Field(i).Type.Elem())
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{
Type: g.realBaseType2SwaggerType(convertType),
Ref: g.getSchemaRef(schemaNameNext),
}
} else {
// 当做默认基础类型, 默认不会出现 *map *[]
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{
Type: g.realBaseType2SwaggerType(g.realBaseType2SwaggerType(inputType.Field(i).Type.String())),
Format: inputType.Field(i).Type.String(),
Default: ParseStructField.GetDefaultValue(inputType.Field(i)),
Description: ParseStructField.GetParamDesc(inputType.Field(i)),
}
continue
}
continue
}
if inputType.Field(i).Type.Kind() == reflect.Struct ||
inputType.Field(i).Type.Kind() == reflect.Map ||
inputType.Field(i).Type.Kind() == reflect.Array ||
inputType.Field(i).Type.Kind() == reflect.Slice {
if inputType.Field(i).Type.Kind() == reflect.Struct ||
inputType.Field(i).Type.Kind() == reflect.Map {
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{
@ -499,8 +501,6 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
},
Properties: map[string]*define.Property{},
}
} else if inputType.Field(i).Type.Kind() == reflect.Ptr {
} else {
g.AddComponentsSchema(schemaName, propertyName, inputType.Field(i).Type)
}
@ -520,11 +520,11 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
}
// 指针
if inputType.Kind() == reflect.Ptr {
convertType := g.realBaseType2SwaggerType(inputType.String())
if convertType == inputType.String() {
if inputType.Elem().Kind() == reflect.Struct {
// 非基础数据类型
return g.AddComponentsSchema(schemaName, inputType.Elem().String(), inputType.Elem())
} else {
convertType := g.realBaseType2SwaggerType(inputType.String())
g.docData.Components.Schemas[schemaName].Properties[schemaName] = &define.Property{
Type: convertType,
Format: inputType.String(),

View File

@ -51,8 +51,9 @@ func Test_parser_Openapi3(t *testing.T) {
Age string `json:"age" d:"18" desc:"年龄" binding:"required,oneof=12 13 18 90"`
}
type List struct {
Total int64 `json:"total" binding:"required"`
UserList []*User `json:"user_list"`
Total int64 `json:"total" binding:"required"`
UserList []*User `json:"user_list"`
UserDetail *User `json:"user_detail" desc:"用户详情"`
}
var o *List
var f *User