优化swagger文档生成
This commit is contained in:
@ -62,7 +62,9 @@ func formatDocConfig(docConfig *define.SwaggerInput) {
|
||||
itemPath.Summary = strings.TrimSpace(itemPath.Summary)
|
||||
itemPath.Summary = wrapper.TernaryOperator.String(len(itemPath.Summary) == 0, wrapper.String("接口 : "+itemPath.Uri), wrapper.String(itemPath.Summary)).Value()
|
||||
// 默认标签
|
||||
itemPath.TagList = wrapper.TernaryOperator.Array(len(itemPath.TagList) == 0, wrapper.ArrayType([]string{"未分组"}), wrapper.ArrayType(itemPath.TagList)).ToStringSlice()
|
||||
if len(itemPath.TagList) == 0 {
|
||||
itemPath.TagList = []string{"未分组"}
|
||||
}
|
||||
for _, itemParam := range itemPath.ParameterList {
|
||||
// 填充默认参数位置
|
||||
itemParam.In = strings.TrimSpace(itemParam.In)
|
||||
@ -148,10 +150,12 @@ func generatePathParameterConfig(swaggerInfo *define.Swagger, pathConfig *define
|
||||
Name: realParamName,
|
||||
In: itemParamInput.In,
|
||||
Required: itemParamInput.Required,
|
||||
Schema: map[string]string{},
|
||||
Schema: &define.SwaggerPathConfigParameterSchema{
|
||||
Ref: "",
|
||||
},
|
||||
}
|
||||
if len(parentPath) > 0 {
|
||||
generateParam.Schema[consts.SwaggerRefKey] = getRefValue(pathConfig.Uri + ".jsonBody")
|
||||
generateParam.Schema.Ref = getRefValue(pathConfig.Uri + ".jsonBody")
|
||||
generateParam.Type = ""
|
||||
}
|
||||
swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Parameters = append(swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Parameters, generateParam)
|
||||
@ -225,8 +229,10 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
|
||||
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{
|
||||
Description: paramConfig.Description,
|
||||
Type: consts.SwaggerDataTypeArray,
|
||||
Items: map[string]string{
|
||||
consts.SwaggerTypeKey: util.GetSwaggerType(paramConfig.Type),
|
||||
Items: &define.SwaggerDefinitionPropertyItem{
|
||||
Type: util.GetSwaggerType(paramConfig.Type),
|
||||
Ref: "",
|
||||
Enum: nil,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
@ -266,8 +272,10 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
|
||||
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = &define.SwaggerDefinitionProperty{
|
||||
Description: "参数描述",
|
||||
Type: consts.SwaggerDataTypeArray,
|
||||
Items: map[string]string{
|
||||
consts.SwaggerRefKey: getRefValue(nextParentPath),
|
||||
Items: &define.SwaggerDefinitionPropertyItem{
|
||||
Type: "",
|
||||
Ref: getRefValue(nextParentPath),
|
||||
Enum: nil,
|
||||
},
|
||||
}
|
||||
generateParameterDefinitions(swaggerInfo, uri, nextParentPath, strings.Join(subPathArr[2:], "."), paramConfig)
|
||||
@ -285,8 +293,10 @@ func generateParameterDefinitions(swaggerInfo *define.Swagger, uri string, paren
|
||||
}}
|
||||
} else if itemSwaggerDefinition.Type == consts.SwaggerDataTypeArray {
|
||||
itemSwaggerDefinition.Description = "数组描述"
|
||||
itemSwaggerDefinition.Items = map[string]string{
|
||||
consts.SwaggerRefKey: getRefValue(parentPath + "." + subPathArr[0] + ".item"),
|
||||
itemSwaggerDefinition.Items = &define.SwaggerDefinitionPropertyItem{
|
||||
Type: "",
|
||||
Ref: getRefValue(parentPath + "." + subPathArr[0] + ".item"),
|
||||
Enum: nil,
|
||||
}
|
||||
}
|
||||
swaggerInfo.Definitions[parentPath].Properties[subPathArr[0]] = itemSwaggerDefinition
|
||||
@ -308,8 +318,10 @@ func handleOneLevelSubPath(swaggerInfo *define.Swagger, uri string, parentPath s
|
||||
swaggerInfo.Definitions[parentPath].Properties[subPath] = &define.SwaggerDefinitionProperty{
|
||||
Description: paramConfig.Description,
|
||||
Type: consts.SwaggerDataTypeArray,
|
||||
Items: map[string]string{
|
||||
consts.SwaggerTypeKey: util.GetSwaggerType(strings.TrimSuffix(paramConfig.Type, "[]")),
|
||||
Items: &define.SwaggerDefinitionPropertyItem{
|
||||
Type: util.GetSwaggerType(strings.TrimSuffix(paramConfig.Type, "[]")),
|
||||
Ref: "",
|
||||
Enum: nil,
|
||||
},
|
||||
}
|
||||
return
|
||||
|
@ -46,7 +46,7 @@ func TestGenerate(t *testing.T) {
|
||||
TagList: []string{"测试接口生成"},
|
||||
ParameterList: []*define.SwaggerParameterInput{
|
||||
&define.SwaggerParameterInput{
|
||||
Type: consts.DataTypeString,
|
||||
Type: consts.DataTypeString.String(),
|
||||
Description: "姓名",
|
||||
Name: "name",
|
||||
In: "body",
|
||||
@ -54,7 +54,7 @@ func TestGenerate(t *testing.T) {
|
||||
EnumList: []interface{}{"zhang", "de", "man"},
|
||||
},
|
||||
&define.SwaggerParameterInput{
|
||||
Type: consts.DataTypeString,
|
||||
Type: consts.DataTypeString.String(),
|
||||
Description: "性别",
|
||||
Name: "person.sex",
|
||||
In: "body",
|
||||
@ -62,7 +62,7 @@ func TestGenerate(t *testing.T) {
|
||||
EnumList: []interface{}{"man", "woman", "other"},
|
||||
},
|
||||
&define.SwaggerParameterInput{
|
||||
Type: consts.DataTypeString,
|
||||
Type: consts.DataTypeString.String(),
|
||||
Description: "性别",
|
||||
Name: "person.job.list.[]",
|
||||
In: "body",
|
||||
@ -70,7 +70,7 @@ func TestGenerate(t *testing.T) {
|
||||
EnumList: nil,
|
||||
},
|
||||
&define.SwaggerParameterInput{
|
||||
Type: consts.DataTypeInt,
|
||||
Type: consts.DataTypeInt.String(),
|
||||
Description: "年份",
|
||||
Name: "person.job.year.[]",
|
||||
In: "body",
|
||||
@ -78,7 +78,7 @@ func TestGenerate(t *testing.T) {
|
||||
EnumList: nil,
|
||||
},
|
||||
&define.SwaggerParameterInput{
|
||||
Type: consts.DataTypeInt,
|
||||
Type: consts.DataTypeInt.String(),
|
||||
Description: "测试工作",
|
||||
Name: "person.job.test",
|
||||
In: "body",
|
||||
@ -86,7 +86,7 @@ func TestGenerate(t *testing.T) {
|
||||
EnumList: nil,
|
||||
},
|
||||
&define.SwaggerParameterInput{
|
||||
Type: consts.DataTypeInt,
|
||||
Type: consts.DataTypeInt.String(),
|
||||
Description: "年龄",
|
||||
Name: "age",
|
||||
In: "body",
|
||||
@ -94,7 +94,7 @@ func TestGenerate(t *testing.T) {
|
||||
EnumList: []interface{}{18, 19, 20},
|
||||
},
|
||||
&define.SwaggerParameterInput{
|
||||
Type: consts.DataTypeInt,
|
||||
Type: consts.DataTypeInt.String(),
|
||||
Description: "年龄",
|
||||
Name: "test_list.[].age",
|
||||
In: "body",
|
||||
@ -102,7 +102,7 @@ func TestGenerate(t *testing.T) {
|
||||
EnumList: []interface{}{18, 19, 20},
|
||||
},
|
||||
&define.SwaggerParameterInput{
|
||||
Type: consts.DataTypeString,
|
||||
Type: consts.DataTypeString.String(),
|
||||
Description: "年龄",
|
||||
Name: "test_list.[].name",
|
||||
In: "body",
|
||||
@ -110,7 +110,7 @@ func TestGenerate(t *testing.T) {
|
||||
EnumList: nil,
|
||||
},
|
||||
&define.SwaggerParameterInput{
|
||||
Type: consts.DataTypeMapAnyAny,
|
||||
Type: consts.DataTypeMapAnyAny.String(),
|
||||
Description: "测试global_map",
|
||||
Name: "obj",
|
||||
In: "body",
|
||||
@ -124,19 +124,19 @@ func TestGenerate(t *testing.T) {
|
||||
Description: "成功",
|
||||
List: []*define.SwaggerResponseItemInput{
|
||||
&define.SwaggerResponseItemInput{
|
||||
Type: consts.DataTypeString,
|
||||
Type: consts.DataTypeString.String(),
|
||||
Description: "姓名",
|
||||
Field: "nick_name",
|
||||
IsRequired: false,
|
||||
},
|
||||
&define.SwaggerResponseItemInput{
|
||||
Type: consts.DataTypeString,
|
||||
Type: consts.DataTypeString.String(),
|
||||
Description: "昵称",
|
||||
Field: "person.nick_name",
|
||||
IsRequired: false,
|
||||
},
|
||||
&define.SwaggerResponseItemInput{
|
||||
Type: consts.DataTypeMapAnyAny,
|
||||
Type: consts.DataTypeMapAnyAny.String(),
|
||||
Description: "测试返回生成map",
|
||||
Field: "obj",
|
||||
IsRequired: false,
|
||||
|
Reference in New Issue
Block a user