修复基础数据类型的数组处理

This commit is contained in:
2025-02-18 11:48:39 +08:00
parent f0f15a4df3
commit 848adb5b29
3 changed files with 34 additions and 13 deletions

View File

@ -469,16 +469,23 @@ 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
propertyXOf.Format = sliceItemType
propertyXOf.Ref = ""
}
if len(rootSchemaName) == 0 {
g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeArray
sliceItemType := g.parseSliceItem(schemaName, inputType)
g.docData.Components.Schemas[schemaName].Items = &define.PropertyXOf{Ref: g.getSchemaRef(sliceItemType)}
g.docData.Components.Schemas[schemaName].Items = propertyXOf
} else {
sliceItemType := g.parseSliceItem(schemaName, inputType)
g.docData.Components.Schemas[rootSchemaName].Properties[schemaName] = &define.Property{
Type: consts.SwaggerDataTypeArray,
Format: inputType.String(),
Items: &define.PropertyXOf{Ref: g.getSchemaRef(sliceItemType)},
Items: propertyXOf,
}
}
@ -530,14 +537,20 @@ 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
propertyXOf.Format = sliceItemType
propertyXOf.Ref = ""
}
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{
Type: consts.SwaggerDataTypeArray,
Format: inputType.Field(i).Type.String(),
Description: ParseStructFieldTag.GetParamDesc(inputType.Field(i)),
Items: &define.PropertyXOf{
Ref: g.getSchemaRef(g.parseSliceItem(schemaName, inputType.Field(i).Type)),
},
Properties: map[string]*define.Property{},
Items: propertyXOf,
}
} else {
g.AddComponentsSchema(schemaName, propertyName, inputType.Field(i).Type)
@ -647,7 +660,11 @@ func (g *Generate) getSchemaRef(schemaName string) string {
return ""
}
schemaName = strings.ReplaceAll(schemaName, "*", "") // 去除指针类型 *
return "#/components/schemas/" + strings.ReplaceAll(schemaName, "/", ".")
convertType, isBaseType := g.realBaseType2SwaggerType(schemaName)
if isBaseType {
return convertType
}
return "#/components/schemas/" + strings.ReplaceAll(convertType, "/", ".")
}
// realType2SwaggerType golang 真实数据类型转换为golang数据类型