From 848adb5b292ae5147c5cb1b1630cd158476feb94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 18 Feb 2025 11:48:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9F=BA=E7=A1=80=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=B1=BB=E5=9E=8B=E7=9A=84=E6=95=B0=E7=BB=84=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/openapi.go | 4 +++- generate.go | 35 ++++++++++++++++++++++++++--------- parser_test.go | 8 +++++--- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/define/openapi.go b/define/openapi.go index f4f45d0..c590a60 100644 --- a/define/openapi.go +++ b/define/openapi.go @@ -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 对象来帮助序列化、反序列化和校验 diff --git a/generate.go b/generate.go index 4495927..8d5f3dd 100644 --- a/generate.go +++ b/generate.go @@ -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数据类型 diff --git a/parser_test.go b/parser_test.go index feb4558..aa72e77 100644 --- a/parser_test.go +++ b/parser_test.go @@ -30,9 +30,11 @@ func Test_parser_Openapi3(t *testing.T) { Height string `json:"height" dc:"height" binding:"required"` } type User struct { - 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"` + 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 {