swagger2版本文档解析1.0版本 #2

Merged
zhangdeman merged 14 commits from feature/upgrade_parse_swag into master 2024-12-25 15:00:17 +08:00
3 changed files with 50 additions and 2 deletions
Showing only changes of commit 3722798c42 - Show all commits

View File

@ -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 { func GetRealDefinitionsKey(ref string) string {
return strings.TrimPrefix(ref, "#/definitions/") return strings.TrimPrefix(ref, "#/definitions/")
} }
// GetRealResponseKey 通过schema下的 $ref 获取真实的 response key
func GetRealResponseKey(ref string) string {
return strings.TrimPrefix(ref, "#/responses/")
}

View File

@ -51,6 +51,7 @@ type SwaggerPathConfigParameterSchema struct {
type SwaggerPathConfigResponse struct { type SwaggerPathConfigResponse struct {
Description string `json:"description"` // 返回值描述 Description string `json:"description"` // 返回值描述
Schema *SwaggerPathConfigResponseSchema `json:"schema"` // 返回值结构 Schema *SwaggerPathConfigResponseSchema `json:"schema"` // 返回值结构
Ref string `json:"$ref"` // 引用的数据
} }
type SwaggerPathConfigResponseSchema struct { type SwaggerPathConfigResponseSchema struct {
@ -107,6 +108,7 @@ type Swagger struct {
Info Info `json:"info"` // 文档描述信息 Info Info `json:"info"` // 文档描述信息
Paths map[string]map[string]*SwaggerPathConfig `json:"paths"` // 接口列表 : 接口 => 请求方法 => 请求配置 Paths map[string]map[string]*SwaggerPathConfig `json:"paths"` // 接口列表 : 接口 => 请求方法 => 请求配置
Definitions map[string]*SwaggerDefinition `json:"definitions"` // 数据定义 Definitions map[string]*SwaggerDefinition `json:"definitions"` // 数据定义
Responses map[string]*SwaggerPathConfigResponse `json:"responses"` // 响应结构列表
} }
// SwaggerInput ... // SwaggerInput ...

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: nil, ResultList: buildSwagger2ResultConfig(swaggerDoc, itemUri, methodConfig.Responses),
} }
uriList = append(uriList, uriResult) uriList = append(uriList, uriResult)
} }
@ -133,3 +133,44 @@ func buildSwagger2ParamConfig(swaggerDoc *define.Swagger, paramConfigList []*def
return res 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
}