完善数据每一项数据类型解析

This commit is contained in:
白茶清欢 2025-02-18 12:23:51 +08:00
parent 848adb5b29
commit d407826f88

View File

@ -469,14 +469,16 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
}
// 数组
if inputType.Kind() == reflect.Slice || inputType.Kind() == reflect.Array {
sliceItemType := g.parseSliceItem(schemaName, inputType)
propertyXOf := &define.PropertyXOf{
Ref: g.getSchemaRef(sliceItemType),
}
if !strings.HasPrefix(propertyXOf.Ref, "#") {
propertyXOf.Type = propertyXOf.Ref
sliceItemType, itemIsBaseType := g.parseSliceItem(schemaName, inputType)
propertyXOf := &define.PropertyXOf{}
if itemIsBaseType {
propertyXOf.Type, _ = g.realBaseType2SwaggerType(sliceItemType)
propertyXOf.Format = sliceItemType
propertyXOf.Ref = ""
} else {
propertyXOf.Type = ""
propertyXOf.Format = ""
propertyXOf.Ref = g.getSchemaRef(sliceItemType)
}
if len(rootSchemaName) == 0 {
g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeArray
@ -537,14 +539,16 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
}
} else if inputType.Field(i).Type.Kind() == reflect.Array ||
inputType.Field(i).Type.Kind() == reflect.Slice {
sliceItemType := g.parseSliceItem(schemaName, inputType.Field(i).Type)
propertyXOf := &define.PropertyXOf{
Ref: g.getSchemaRef(sliceItemType),
}
if !strings.HasPrefix(propertyXOf.Ref, "#") {
propertyXOf.Type = propertyXOf.Ref
sliceItemType, itemIsBaseType := g.parseSliceItem(schemaName, inputType.Field(i).Type)
propertyXOf := &define.PropertyXOf{}
if itemIsBaseType {
propertyXOf.Type, _ = g.realBaseType2SwaggerType(sliceItemType)
propertyXOf.Format = sliceItemType
propertyXOf.Ref = ""
} else {
propertyXOf.Type = ""
propertyXOf.Format = ""
propertyXOf.Ref = g.getSchemaRef(sliceItemType)
}
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{
Type: consts.SwaggerDataTypeArray,
@ -633,21 +637,26 @@ func (g *Generate) handleAnonymousField(schemaName string, field reflect.StructF
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:33 2025/2/8
func (g *Generate) parseSliceItem(rootSchemaName string, inputType reflect.Type) string {
func (g *Generate) parseSliceItem(rootSchemaName string, inputType reflect.Type) (string, bool) {
if inputType.Kind() != reflect.Slice && inputType.Kind() != reflect.Array {
// 不是数组
return ""
return "", false
}
sliceValue := reflect.MakeSlice(inputType, 1, 1)
sliceItemType := sliceValue.Index(0).Type()
realSliceItemType := sliceItemType.String()
if sliceItemType.Kind() == reflect.Ptr {
sliceItemType = sliceItemType.Elem()
}
_, isBaseType := g.realBaseType2SwaggerType(sliceItemType.String())
if isBaseType {
return realSliceItemType, true
}
g.AddComponentsSchema(rootSchemaName, sliceItemType.PkgPath(), sliceItemType)
if len(sliceItemType.PkgPath()) == 0 {
return sliceItemType.String()
return realSliceItemType, false
}
return sliceItemType.PkgPath() + "." + sliceItemType.Name()
return sliceItemType.PkgPath() + "." + sliceItemType.Name(), false
}
// getSchemaRef 获取引用的类型