diff --git a/define/types.go b/define/types.go index a6f0525..f84a3a5 100644 --- a/define/types.go +++ b/define/types.go @@ -61,13 +61,13 @@ type SwaggerPathConfig struct { // // Date : 16:53 2024/4/19 type SwaggerPathConfigParameter struct { - Type string `json:"type"` // 类型 - Description string `json:"description"` // 描述 - Name string `json:"name"` // 参数名称 - In string `json:"in"` // 参数位置 - Required bool `json:"required"` // 是否必传 - EnumList []interface{} `json:"enum_list"` // 枚举值列表 - Schema map[string]string `json:"schema"` // 参数schema + Type string `json:"type"` // 类型 + Description string `json:"description"` // 描述 + Name string `json:"name"` // 参数名称 + In string `json:"in"` // 参数位置 + Required bool `json:"required"` // 是否必传 + EnumList []interface{} `json:"enum_list,omitempty"` // 枚举值列表 + Schema map[string]string `json:"schema"` // 参数schema } // SwaggerPathConfigResponse ... @@ -158,12 +158,12 @@ type SwaggerResponseInput struct { // // Date : 10:25 2024/4/22 type SwaggerParameterInput struct { - Type string `json:"type"` // 类型 - Description string `json:"description"` // 描述 - Name string `json:"name"` // 参数名称 - In string `json:"in"` // 参数位置 - Required bool `json:"required"` // 是否必传 - EnumList []interface{} `json:"enum_list"` // 枚举值列表 + Type string `json:"type"` // 类型 + Description string `json:"description"` // 描述 + Name string `json:"name"` // 参数名称 + In string `json:"in"` // 参数位置 + Required bool `json:"required"` // 是否必传 + EnumList []interface{} `json:"enum_list,omitempty"` // 枚举值列表 } // SwaggerResponseItemInput 响应数据 diff --git a/swagger/run.go b/swagger/run.go index 8713d20..b98ef27 100644 --- a/swagger/run.go +++ b/swagger/run.go @@ -129,20 +129,28 @@ func generatePathParameterConfig(swaggerInfo *define.Swagger, pathConfig *define continue } // name 可能是 x.x.x 递归数组, 或者 x.x.[].x - generateParameterDefinitions(swaggerInfo, pathConfig.Uri, "", itemParamInput.Name, itemParamInput) namePath := strings.Split(itemParamInput.Name, ".") + realParamName := namePath[0] + parentPath := "" + if !strings.Contains(realParamName, ".") { + realParamName = "jsonBody" + parentPath = pathConfig.Uri + ".jsonBody" + } + generateParameterDefinitions(swaggerInfo, pathConfig.Uri, parentPath, itemParamInput.Name, itemParamInput) if _, exist := hasDealTable[namePath[0]]; !exist { hasDealTable[namePath[0]] = true - swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Parameters = append(swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Parameters, &define.SwaggerPathConfigParameter{ + generateParam := &define.SwaggerPathConfigParameter{ Type: itemParamInput.Type, Description: itemParamInput.Description, - Name: namePath[0], + Name: realParamName, In: itemParamInput.In, Required: itemParamInput.Required, - Schema: map[string]string{ - // "$ref": "#/definitions/" + pathConfig.Uri + ".input", - }, - }) + Schema: map[string]string{}, + } + if len(parentPath) > 0 { + generateParam.Schema["$ref"] = "#/definitions/" + strings.TrimLeft(pathConfig.Uri, "/") + ".jsonBody" + } + swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Parameters = append(swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Parameters, generateParam) } } @@ -187,18 +195,33 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren Properties: make(map[string]*define.SwaggerDefinitionProperty), } } + if len(subPathArr) == 1 { - if len(parentPath) == 0 { + if paramConfig.In != strings.ToLower(consts.RequestLocationBody) { + // 长度为1, 还不在 body, 无需生成结构体 return } + if _, exist := swaggerInfo.Definitions[parentPath]; !exist { + swaggerInfo.Definitions[parentPath] = &define.SwaggerDefinition{ + Type: "object", + Required: make([]string, 0), + Properties: make(map[string]*define.SwaggerDefinitionProperty), + } + } swaggerInfo.Definitions[parentPath].Type = paramConfig.Type if paramConfig.Required { swaggerInfo.Definitions[parentPath].Required = append(swaggerInfo.Definitions[parentPath].Required, paramConfig.Name) } - /*swaggerInfo.Definitions[parentPath].Properties[paramConfig.Name] = &define.SwaggerDefinitionProperty{ - Description: paramConfig.Description, - Type: paramConfig.Type, - }*/ + if len(parentPath) > 0 { + swaggerInfo.Definitions[parentPath].Properties[paramConfig.Name] = &define.SwaggerDefinitionProperty{ + Description: paramConfig.Description, + Type: paramConfig.Type, + } + if paramConfig.Required { + swaggerInfo.Definitions[parentPath].Required = append(swaggerInfo.Definitions[parentPath].Required, paramConfig.Name) + } + } + return } if len(parentPath) == 0 {