Merge pull request '修复结构体指针解析错误' (#5) from feature/fix_struct_ptr into master

Reviewed-on: #5
This commit is contained in:
白茶清欢 2025-02-15 15:51:04 +08:00
commit ec0d894d36
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 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++ { for i := 0; i < inputType.NumField(); i++ {
propertyName := ParseStructField.GetParamName(inputType.Field(i)) propertyName := ParseStructField.GetParamName(inputType.Field(i))
if propertyName == "-" { if propertyName == "-" {
continue continue
} }
if inputType.Field(i).Type.Kind() == reflect.Ptr || if inputType.Field(i).Type.Kind() == reflect.Ptr {
inputType.Field(i).Type.Kind() == reflect.Struct || // 处理指针
inputType.Field(i).Type.Kind() == reflect.Map || if inputType.Field(i).Type.Elem().Kind() == reflect.Struct {
inputType.Field(i).Type.Kind() == reflect.Array || // 结构体指针
inputType.Field(i).Type.Kind() == reflect.Slice { schemaNameNext := g.AddComponentsSchema(schemaName, propertyName, inputType.Field(i).Type.Elem())
if convertType := g.realBaseType2SwaggerType(inputType.Field(i).Type.String()); !strings.HasPrefix(convertType, "[]") && convertType != inputType.Field(i).Type.Kind().String() {
// 针对基础类型指针
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{ 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(), Format: inputType.Field(i).Type.String(),
Default: ParseStructField.GetDefaultValue(inputType.Field(i)), Default: ParseStructField.GetDefaultValue(inputType.Field(i)),
Description: ParseStructField.GetParamDesc(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 || if inputType.Field(i).Type.Kind() == reflect.Struct ||
inputType.Field(i).Type.Kind() == reflect.Map { inputType.Field(i).Type.Kind() == reflect.Map {
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{ 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{}, Properties: map[string]*define.Property{},
} }
} else if inputType.Field(i).Type.Kind() == reflect.Ptr {
} else { } else {
g.AddComponentsSchema(schemaName, propertyName, inputType.Field(i).Type) 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 { if inputType.Kind() == reflect.Ptr {
convertType := g.realBaseType2SwaggerType(inputType.String()) if inputType.Elem().Kind() == reflect.Struct {
if convertType == inputType.String() {
// 非基础数据类型 // 非基础数据类型
return g.AddComponentsSchema(schemaName, inputType.Elem().String(), inputType.Elem()) return g.AddComponentsSchema(schemaName, inputType.Elem().String(), inputType.Elem())
} else { } else {
convertType := g.realBaseType2SwaggerType(inputType.String())
g.docData.Components.Schemas[schemaName].Properties[schemaName] = &define.Property{ g.docData.Components.Schemas[schemaName].Properties[schemaName] = &define.Property{
Type: convertType, Type: convertType,
Format: inputType.String(), 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"` Age string `json:"age" d:"18" desc:"年龄" binding:"required,oneof=12 13 18 90"`
} }
type List struct { type List struct {
Total int64 `json:"total" binding:"required"` Total int64 `json:"total" binding:"required"`
UserList []*User `json:"user_list"` UserList []*User `json:"user_list"`
UserDetail *User `json:"user_detail" desc:"用户详情"`
} }
var o *List var o *List
var f *User var f *User