diff --git a/generate.go b/generate.go index 58821e2..6f04556 100644 --- a/generate.go +++ b/generate.go @@ -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(), diff --git a/parser_test.go b/parser_test.go index a33b373..a9fd398 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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