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: "年龄",