参数 + 返回值支持自动展开一层数组

This commit is contained in:
白茶清欢 2024-12-25 14:38:08 +08:00
parent f58f8da722
commit e70634a318
2 changed files with 61 additions and 6 deletions

View File

@ -74,7 +74,7 @@ func GetDataType(docParamType string, formatType string) consts.DataType {
return consts.DataTypeSliceAny return consts.DataTypeSliceAny
} }
default: default:
return consts.DataTypeMapStrAny return consts.DataTypeAny
} }
} }
@ -120,3 +120,48 @@ func GetSuccessResponseConfig(resultConfig map[string]*define.SwaggerPathConfigR
} }
return nil return nil
} }
// DataTypeIsArray 判断数据类型是否为数组
func DataTypeIsArray(docDataType string) bool {
return strings.ToLower(docDataType) == "array"
}
// ExpandArrayParam 展开详细的数组配置
func ExpandArrayParam(swaggerDoc *define.Swagger, ref string, rootPath string) []*define.ParamConfig {
pathPrefix := ""
if len(rootPath) > 0 {
pathPrefix = rootPath + "."
}
res := make([]*define.ParamConfig, 0)
for itemKey, itemConfig := range swaggerDoc.Definitions[GetRealDefinitionsKey(ref)].Properties {
res = append(res, &define.ParamConfig{
Location: consts.RequestDataLocationBody.String(),
Path: pathPrefix + "{{#idx#}}." + itemKey,
Type: GetDataType(itemConfig.Type, "").String(),
Title: pathPrefix + itemKey,
Description: pathPrefix + itemKey,
Required: false,
})
}
return res
}
// ExpandArrayResult 展开返回值配置
func ExpandArrayResult(swaggerDoc *define.Swagger, ref string, rootPath string) []*define.ResultConfig {
pathPrefix := ""
if len(rootPath) > 0 && rootPath != consts.ResponseDataLocationBodyRoot.String() {
pathPrefix = rootPath + "."
}
ref = GetRealDefinitionsKey(ref)
res := make([]*define.ResultConfig, 0)
for itemKey, itemConfig := range swaggerDoc.Definitions[ref].Properties {
res = append(res, &define.ResultConfig{
Location: consts.ResponseDataLocationBody.String(),
Path: pathPrefix + "{{#idx#}}." + itemKey,
Type: GetDataType(itemConfig.Type, "").String(),
Title: pathPrefix + itemKey,
Description: pathPrefix + itemKey,
})
}
return res
}

View File

@ -57,7 +57,7 @@ func buildUriList(swaggerDoc *define.Swagger) ([]*define.UriBaseConfig, error) {
Summary: methodConfig.Summary, Summary: methodConfig.Summary,
Description: methodConfig.Description, Description: methodConfig.Description,
ParamList: buildSwagger2ParamConfig(swaggerDoc, methodConfig.Parameters), ParamList: buildSwagger2ParamConfig(swaggerDoc, methodConfig.Parameters),
ResultList: buildSwagger2ResultConfig(swaggerDoc, itemUri, methodConfig.Responses), ResultList: buildSwagger2ResultConfig(swaggerDoc, methodConfig.Responses),
} }
uriList = append(uriList, uriResult) uriList = append(uriList, uriResult)
} }
@ -128,6 +128,10 @@ func buildSwagger2ParamConfig(swaggerDoc *define.Swagger, paramConfigList []*def
Required: requiredTable[paramName], Required: requiredTable[paramName],
} }
res = append(res, paramConfigBuildConfig) res = append(res, paramConfigBuildConfig)
if DataTypeIsArray(paramMoreConfig.Type) && nil != paramMoreConfig.Items && len(paramMoreConfig.Items.Ref) > 0 {
// 是数组,且每一项是对象, 继续展开
res = append(res, ExpandArrayParam(swaggerDoc, paramMoreConfig.Items.Ref, paramConfigBuildConfig.Path)...)
}
} }
} }
@ -135,7 +139,7 @@ func buildSwagger2ParamConfig(swaggerDoc *define.Swagger, paramConfigList []*def
} }
// buildSwagger2ResultConfig 构建响应结果配置 // buildSwagger2ResultConfig 构建响应结果配置
func buildSwagger2ResultConfig(swaggerDoc *define.Swagger, uri string, resultConfig map[string]*define.SwaggerPathConfigResponse) []*define.ResultConfig { func buildSwagger2ResultConfig(swaggerDoc *define.Swagger, resultConfig map[string]*define.SwaggerPathConfigResponse) []*define.ResultConfig {
res := make([]*define.ResultConfig, 0) res := make([]*define.ResultConfig, 0)
if nil == resultConfig || len(resultConfig) == 0 { if nil == resultConfig || len(resultConfig) == 0 {
return res return res
@ -173,19 +177,25 @@ func buildSwagger2ResultConfig(swaggerDoc *define.Swagger, uri string, resultCon
responseKey := GetRealResponseKey(successResponseConfig.Ref) responseKey := GetRealResponseKey(successResponseConfig.Ref)
responseTypeDefine := swaggerDoc.Responses[responseKey] responseTypeDefine := swaggerDoc.Responses[responseKey]
responseType := "" responseType := ""
if nil != responseTypeDefine.Schema { schemaType := ""
if nil == responseTypeDefine.Schema {
// 204 等场景下可能没有响应body // 204 等场景下可能没有响应body
responseType = consts.DataTypeAny.String() responseType = consts.DataTypeAny.String()
} else { } else {
schemaType = responseTypeDefine.Schema.Type
responseType = GetDataType(responseTypeDefine.Schema.Type, "").String() responseType = GetDataType(responseTypeDefine.Schema.Type, "").String()
} }
res = append(res, &define.ResultConfig{ resCfg := &define.ResultConfig{
Location: consts.ResponseDataLocationBody.String(), Location: consts.ResponseDataLocationBody.String(),
Path: consts.ResponseDataLocationBodyRoot.String(), Path: consts.ResponseDataLocationBodyRoot.String(),
Type: responseType, Type: responseType,
Title: "response body", Title: "response body",
Description: "response body", Description: "response body",
}) }
res = append(res, resCfg)
if DataTypeIsArray(schemaType) && len(responseTypeDefine.Schema.Items.Ref) > 0 {
res = append(res, ExpandArrayResult(swaggerDoc, responseTypeDefine.Schema.Items.Ref, resCfg.Path)...)
}
} else { } else {
responseTypeDefine := swaggerDoc.Definitions[definitionsKey] responseTypeDefine := swaggerDoc.Definitions[definitionsKey]
for responseKey, responseKeyConfig := range responseTypeDefine.Properties { for responseKey, responseKeyConfig := range responseTypeDefine.Properties {