From 4ef4e5ae3bf9bad00301b8b389f92e5b5fb9cd31 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, 23 Apr 2024 14:43:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AF=B9=E8=B1=A1=E5=B5=8C?= =?UTF-8?q?=E5=A5=97=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/types.go | 6 ++++-- swagger/run.go | 33 ++++++++++++++++++++++++++++++--- swagger/run_test.go | 8 ++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/define/types.go b/define/types.go index ba7e191..ef323e5 100644 --- a/define/types.go +++ b/define/types.go @@ -97,8 +97,10 @@ type SwaggerDefinition struct { // // Date : 17:25 2024/4/19 type SwaggerDefinitionProperty struct { - Description string `json:"description"` // 描述 - Type string `json:"type"` // 类型 + Description string `json:"description"` // 描述 + Type string `json:"type"` // 类型 + Items map[string]string `json:"items,omitempty"` // 引用类型中的引用(数组) + AllOf []map[string]string `json:"allOf,omitempty"` // 引用类型中的引用(对象) } // Swagger 文档整体结构定义 diff --git a/swagger/run.go b/swagger/run.go index 730d08d..7081a86 100644 --- a/swagger/run.go +++ b/swagger/run.go @@ -189,9 +189,14 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren if nil == swaggerInfo.Definitions { swaggerInfo.Definitions = map[string]*define.SwaggerDefinition{} } + parentPathArr := strings.Split(parentPath, ".") + checkPath := parentPath + if len(parentPathArr) >= 2 { + checkPath = strings.Join([]string{parentPathArr[0], parentPathArr[1]}, ".") + } subPathArr := strings.Split(subPath, ".") - if _, exist := swaggerInfo.Definitions[parentPath]; !exist && len(parentPath) > 0 { - swaggerInfo.Definitions[parentPath] = &define.SwaggerDefinition{ + if _, exist := swaggerInfo.Definitions[checkPath]; !exist && len(parentPath) > 0 { + swaggerInfo.Definitions[checkPath] = &define.SwaggerDefinition{ Type: "object", Required: make([]string, 0), Properties: make(map[string]*define.SwaggerDefinitionProperty), @@ -230,7 +235,29 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren Type: "Array", } } else { - generateParameterDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), paramConfig) + swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{ + Description: "参数描述", + Type: "object", + AllOf: []map[string]string{map[string]string{ + "$ref": "#/definitions/" + parentPath + "." + subPathArr[0], + }}, + } + + storageSubPath := parentPath + "." + subPathArr[0] + if _, exist := swaggerInfo.Definitions[storageSubPath]; !exist { + swaggerInfo.Definitions[storageSubPath] = &define.SwaggerDefinition{ + Type: "object", + Required: make([]string, 0), + Properties: make(map[string]*define.SwaggerDefinitionProperty), + } + } + if paramConfig.Required { + swaggerInfo.Definitions[storageSubPath].Required = append(swaggerInfo.Definitions[storageSubPath].Required, subPathArr[1]) + } + swaggerInfo.Definitions[storageSubPath].Properties[subPathArr[1]] = &define.SwaggerDefinitionProperty{ + Description: paramConfig.Description, + Type: paramConfig.Type, + } } return } diff --git a/swagger/run_test.go b/swagger/run_test.go index d9e3287..942cb35 100644 --- a/swagger/run_test.go +++ b/swagger/run_test.go @@ -53,6 +53,14 @@ func TestGenerate(t *testing.T) { Required: true, EnumList: []interface{}{"zhang", "de", "man"}, }, + &define.SwaggerParameterInput{ + Type: consts.DataTypeString, + Description: "性别", + Name: "person.sex", + In: "body", + Required: true, + EnumList: []interface{}{"man", "woman", "other"}, + }, &define.SwaggerParameterInput{ Type: consts.DataTypeInt, Description: "年龄",