From c46e7ee8deddaab2cc54d109ed63827812ac3744 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 15:54:06 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=92=E5=BD=92=E7=94=9F=E6=88=90=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- swagger/run.go | 28 +++++++++++++++++++++++++--- swagger/run_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/swagger/run.go b/swagger/run.go index 7081a86..9123944 100644 --- a/swagger/run.go +++ b/swagger/run.go @@ -229,10 +229,20 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren parentPath = uri + ".input" } if len(subPathArr) == 2 { + if _, exist := swaggerInfo.Definitions[parentPath]; !exist { + swaggerInfo.Definitions[parentPath] = &define.SwaggerDefinition{ + Type: "object", + Required: make([]string, 0), + Properties: make(map[string]*define.SwaggerDefinitionProperty), + } + } if subPathArr[1] == "[]" { swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{ Description: paramConfig.Description, - Type: "Array", + Type: "array", + Items: map[string]string{ + "type": util.GetSwaggerType(paramConfig.Type), + }, } } else { swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{ @@ -256,11 +266,23 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren } swaggerInfo.Definitions[storageSubPath].Properties[subPathArr[1]] = &define.SwaggerDefinitionProperty{ Description: paramConfig.Description, - Type: paramConfig.Type, + Type: util.GetSwaggerType(paramConfig.Type), } } return } + + if _, exist := swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]]; !exist { + swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{ + Description: "对象描述", + Type: "object", + Items: nil, + AllOf: []map[string]string{map[string]string{ + "$ref": "#/definitions/" + parentPath + "." + subPathArr[0], + }}, + } + } + // fmt.Println(parentPath + "." + subPathArr[0]) generateParameterDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), paramConfig) } @@ -299,7 +321,7 @@ func generateResponseDefinitions(swaggerInfo *define.Swagger, uri string, parent if subPathArr[1] == "[]" { swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{ Description: responseConfig.Description, - Type: "Array", + Type: "array", } } else { generateResponseDefinitions(swaggerInfo, uri, parentPath+"."+subPathArr[0], strings.Join(subPathArr[1:], "."), responseConfig) diff --git a/swagger/run_test.go b/swagger/run_test.go index 942cb35..0db0fb9 100644 --- a/swagger/run_test.go +++ b/swagger/run_test.go @@ -61,6 +61,30 @@ func TestGenerate(t *testing.T) { Required: true, EnumList: []interface{}{"man", "woman", "other"}, }, + &define.SwaggerParameterInput{ + Type: consts.DataTypeString, + Description: "性别", + Name: "person.job.list.[]", + In: "body", + Required: true, + EnumList: nil, + }, + &define.SwaggerParameterInput{ + Type: consts.DataTypeInt, + Description: "年份", + Name: "person.job.year.[]", + In: "body", + Required: true, + EnumList: nil, + }, + &define.SwaggerParameterInput{ + Type: consts.DataTypeInt, + Description: "测试工作", + Name: "person.job.test", + In: "body", + Required: true, + EnumList: nil, + }, &define.SwaggerParameterInput{ Type: consts.DataTypeInt, Description: "年龄",