From d407826f883eca45daaff2817b2189f5af4763d8 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 12:23:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=95=B0=E6=8D=AE=E6=AF=8F?= =?UTF-8?q?=E4=B8=80=E9=A1=B9=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B=E8=A7=A3?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generate.go | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/generate.go b/generate.go index 8d5f3dd..7544aee 100644 --- a/generate.go +++ b/generate.go @@ -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 获取引用的类型