diff --git a/common.go b/common.go index a7c03b2..de1731d 100644 --- a/common.go +++ b/common.go @@ -96,7 +96,12 @@ func GetParamLocation(docParamLocation string) consts.RequestDataLocation { } } -// GetRealDefinitionsKey 统统schema下的 $ref 获取真实的 definitions key +// GetRealDefinitionsKey 通过schema下的 $ref 获取真实的 definitions key func GetRealDefinitionsKey(ref string) string { return strings.TrimPrefix(ref, "#/definitions/") } + +// GetRealResponseKey 通过schema下的 $ref 获取真实的 response key +func GetRealResponseKey(ref string) string { + return strings.TrimPrefix(ref, "#/responses/") +} diff --git a/define/swagger.go b/define/swagger.go index 8c1d4b2..1fbfab2 100644 --- a/define/swagger.go +++ b/define/swagger.go @@ -51,6 +51,7 @@ type SwaggerPathConfigParameterSchema struct { type SwaggerPathConfigResponse struct { Description string `json:"description"` // 返回值描述 Schema *SwaggerPathConfigResponseSchema `json:"schema"` // 返回值结构 + Ref string `json:"$ref"` // 引用的数据 } type SwaggerPathConfigResponseSchema struct { @@ -107,6 +108,7 @@ type Swagger struct { Info Info `json:"info"` // 文档描述信息 Paths map[string]map[string]*SwaggerPathConfig `json:"paths"` // 接口列表 : 接口 => 请求方法 => 请求配置 Definitions map[string]*SwaggerDefinition `json:"definitions"` // 数据定义 + Responses map[string]*SwaggerPathConfigResponse `json:"responses"` // 响应结构列表 } // SwaggerInput ... diff --git a/swagger.go b/swagger.go index 897baa9..e9fdb8a 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: nil, + ResultList: buildSwagger2ResultConfig(swaggerDoc, itemUri, methodConfig.Responses), } uriList = append(uriList, uriResult) } @@ -133,3 +133,44 @@ func buildSwagger2ParamConfig(swaggerDoc *define.Swagger, paramConfigList []*def return res } + +// buildSwagger2ResultConfig 构建响应结果配置 +func buildSwagger2ResultConfig(swaggerDoc *define.Swagger, uri string, resultConfig map[string]*define.SwaggerPathConfigResponse) []*define.ResultConfig { + res := make([]*define.ResultConfig, 0) + if nil == resultConfig || len(resultConfig) == 0 { + return res + } + if _, exist := resultConfig["200"]; !exist { + result204Config := resultConfig["204"] + resultConfig["200"] = result204Config + } + + if nil == resultConfig["200"] { + return res + } + definitionsKey := "" + if swaggerDoc.Responses == nil { + definitionsKey = GetRealDefinitionsKey(resultConfig["200"].Schema.Ref) + } else { + responseKey := GetRealResponseKey(resultConfig["200"].Ref) + ref := swaggerDoc.Responses[responseKey].Ref + if len(ref) == 0 { + if nil != swaggerDoc.Responses[responseKey].Schema { + ref = swaggerDoc.Responses[responseKey].Schema.Ref + } + } + definitionsKey = GetRealDefinitionsKey(ref) + } + if len(definitionsKey) == 0 { + // 不是引用类型, 直接定义的具体类型 + res = append(res, &define.ResultConfig{ + Location: consts.ResponseDataLocationBody.String(), + Path: "", + Type: "", + Title: "", + Description: "", + }) + } + + return nil +}