From e70634a3183de7a5cde8b7e0060d1674c397191d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 25 Dec 2024 14:38:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=82=E6=95=B0=20+=20=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=80=BC=E6=94=AF=E6=8C=81=E8=87=AA=E5=8A=A8=E5=B1=95=E5=BC=80?= =?UTF-8?q?=E4=B8=80=E5=B1=82=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++- swagger.go | 20 +++++++++++++++----- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/common.go b/common.go index 7cd76eb..34ec72f 100644 --- a/common.go +++ b/common.go @@ -74,7 +74,7 @@ func GetDataType(docParamType string, formatType string) consts.DataType { return consts.DataTypeSliceAny } default: - return consts.DataTypeMapStrAny + return consts.DataTypeAny } } @@ -120,3 +120,48 @@ func GetSuccessResponseConfig(resultConfig map[string]*define.SwaggerPathConfigR } 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 +} diff --git a/swagger.go b/swagger.go index b5d35fe..447a5ba 100644 --- a/swagger.go +++ b/swagger.go @@ -57,7 +57,7 @@ func buildUriList(swaggerDoc *define.Swagger) ([]*define.UriBaseConfig, error) { Summary: methodConfig.Summary, Description: methodConfig.Description, ParamList: buildSwagger2ParamConfig(swaggerDoc, methodConfig.Parameters), - ResultList: buildSwagger2ResultConfig(swaggerDoc, itemUri, methodConfig.Responses), + ResultList: buildSwagger2ResultConfig(swaggerDoc, methodConfig.Responses), } uriList = append(uriList, uriResult) } @@ -128,6 +128,10 @@ func buildSwagger2ParamConfig(swaggerDoc *define.Swagger, paramConfigList []*def Required: requiredTable[paramName], } 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 构建响应结果配置 -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) if nil == resultConfig || len(resultConfig) == 0 { return res @@ -173,19 +177,25 @@ func buildSwagger2ResultConfig(swaggerDoc *define.Swagger, uri string, resultCon responseKey := GetRealResponseKey(successResponseConfig.Ref) responseTypeDefine := swaggerDoc.Responses[responseKey] responseType := "" - if nil != responseTypeDefine.Schema { + schemaType := "" + if nil == responseTypeDefine.Schema { // 204 等场景下,可能没有响应body responseType = consts.DataTypeAny.String() } else { + schemaType = responseTypeDefine.Schema.Type responseType = GetDataType(responseTypeDefine.Schema.Type, "").String() } - res = append(res, &define.ResultConfig{ + resCfg := &define.ResultConfig{ Location: consts.ResponseDataLocationBody.String(), Path: consts.ResponseDataLocationBodyRoot.String(), Type: responseType, Title: "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 { responseTypeDefine := swaggerDoc.Definitions[definitionsKey] for responseKey, responseKeyConfig := range responseTypeDefine.Properties {