支持匿名字段 #9

Merged
zhangdeman merged 7 commits from feature/fix_base_arr_anonymity_struct into master 2025-02-18 14:07:25 +08:00
3 changed files with 34 additions and 13 deletions
Showing only changes of commit 848adb5b29 - Show all commits

View File

@ -162,7 +162,9 @@ type Property struct {
//
// Date : 17:13 2024/7/19
type PropertyXOf struct {
Ref string `json:"$ref"` // 引用的结构描述
Type string `json:"type,omitempty"` // 基础类型
Format string `json:"format,omitempty"` // 真实类型
Ref string `json:"$ref,omitempty"` // 引用的结构描述
}
// SchemaDiscriminator 当一个 request bodies 或 response payloads 可以是多种 schemas 时,可以使用一个 discriminator 对象来帮助序列化、反序列化和校验

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数据类型

View File

@ -33,6 +33,8 @@ func Test_parser_Openapi3(t *testing.T) {
Meta `json:"-" deprecated:"false" path:"/user/detail" method:"POST" desc:"测试接口" tag:"用户,搜索" content_type:"application/json" output_content_type:"application/json"`
Name *string `json:"name" d:"zhang" desc:"用户姓名" binding:"required"`
Age string `json:"age" d:"18" desc:"年龄" binding:"required,oneof=12 13 18 90"`
IDList []int64 `json:"id_list" dc:"id_list...." binding:"required"`
IDPtrList []*int64 `json:"id_ptr_list" dc:"id_ptr_list...." binding:"required"`
UserExt
}
type UserDelete struct {