From a3038b693e925265d284756faf7f02960d8cb2d7 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 16:53:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AF=B9=E8=B1=A1=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E6=96=87=E6=A1=A3=E7=94=9F=E6=88=90=E7=9A=84BUg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- swagger/run.go | 44 ++++++++++++++++++++++++++++++-------------- swagger/run_test.go | 16 ++++++++++++++++ 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/swagger/run.go b/swagger/run.go index 9123944..a352d82 100644 --- a/swagger/run.go +++ b/swagger/run.go @@ -219,7 +219,7 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren if paramConfig.Required { swaggerInfo.Definitions[parentPath].Required = append(swaggerInfo.Definitions[parentPath].Required, paramConfig.Name) } - swaggerInfo.Definitions[parentPath].Properties[paramConfig.Name] = &define.SwaggerDefinitionProperty{ + swaggerInfo.Definitions[parentPath].Properties[subPath] = &define.SwaggerDefinitionProperty{ Description: paramConfig.Description, Type: util.GetSwaggerType(paramConfig.Type), } @@ -245,18 +245,25 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren }, } } else { - swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{ - Description: "参数描述", - Type: "object", - AllOf: []map[string]string{map[string]string{ - "$ref": "#/definitions/" + parentPath + "." + subPathArr[0], - }}, + if subPathArr[0] == "[]" { + generateParameterDefinitions(swaggerInfo, uri, parentPath+".[]", strings.Join(subPathArr[1:], "."), paramConfig) + return + } else { + 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 subPathArr[1] == "[]" { + storageSubPath += ".[]" + } if _, exist := swaggerInfo.Definitions[storageSubPath]; !exist { swaggerInfo.Definitions[storageSubPath] = &define.SwaggerDefinition{ - Type: "object", + Type: wrapper.TernaryOperator.String(strings.HasSuffix(parentPath, "[]"), "array", "object").Value(), Required: make([]string, 0), Properties: make(map[string]*define.SwaggerDefinitionProperty), } @@ -273,14 +280,23 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren } if _, exist := swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]]; !exist { - swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{ + itemSwaggerDefinition := &define.SwaggerDefinitionProperty{ Description: "对象描述", - Type: "object", + Type: wrapper.TernaryOperator.String(subPathArr[1] == "[]", "array", "object").Value(), Items: nil, - AllOf: []map[string]string{map[string]string{ - "$ref": "#/definitions/" + parentPath + "." + subPathArr[0], - }}, + AllOf: nil, } + if itemSwaggerDefinition.Type == "object" { + itemSwaggerDefinition.AllOf = []map[string]string{map[string]string{ + "$ref": "#/definitions/" + parentPath + "." + subPathArr[0], + }} + } else if itemSwaggerDefinition.Type == "array" { + itemSwaggerDefinition.Description = "数组描述" + itemSwaggerDefinition.Items = map[string]string{ + "$ref": "#/definitions/" + parentPath + "." + subPathArr[0] + ".[]", + } + } + swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = itemSwaggerDefinition } // fmt.Println(parentPath + "." + subPathArr[0]) generateParameterDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), paramConfig) diff --git a/swagger/run_test.go b/swagger/run_test.go index 0db0fb9..bfb4980 100644 --- a/swagger/run_test.go +++ b/swagger/run_test.go @@ -93,6 +93,22 @@ func TestGenerate(t *testing.T) { Required: true, EnumList: []interface{}{18, 19, 20}, }, + &define.SwaggerParameterInput{ + Type: consts.DataTypeInt, + Description: "年龄", + Name: "test_list.[].age", + In: "body", + Required: true, + EnumList: []interface{}{18, 19, 20}, + }, + &define.SwaggerParameterInput{ + Type: consts.DataTypeString, + Description: "年龄", + Name: "test_list.[].name", + In: "body", + Required: true, + EnumList: nil, + }, }, ResponseList: []*define.SwaggerResponseInput{ &define.SwaggerResponseInput{