diff --git a/common.go b/common.go index 34ec72f..469b561 100644 --- a/common.go +++ b/common.go @@ -57,7 +57,7 @@ func GetDataType(docParamType string, formatType string) consts.DataType { return consts.DataTypeInt } return consts.DataTypeUint - case "string": + case "string", "apikey": return consts.DataTypeString case "object": return consts.DataTypeMapStrAny diff --git a/define/generate.go b/define/generate.go index 7f66fcf..7ecfe1b 100644 --- a/define/generate.go +++ b/define/generate.go @@ -15,10 +15,11 @@ import "regexp" // // Date : 10:30 2024/12/24 type DocParseResult struct { - Domain string `json:"domain"` // 域名 - Title string `json:"title"` // 标题 - Description string `json:"description"` // 描述 - UriList []*UriBaseConfig `json:"uri_list"` // 接口列表 + Domain string `json:"domain"` // 域名 + Title string `json:"title"` // 标题 + Description string `json:"description"` // 描述 + UriList []*UriBaseConfig `json:"uri_list"` // 接口列表 + GlobalSecurityParamList []*ParamConfig `json:"global_security_param_list"` // 全局安全类参数配置 } // UriBaseConfig 添加接口时的基础配置 diff --git a/define/swagger.go b/define/swagger.go index 1fbfab2..096159a 100644 --- a/define/swagger.go +++ b/define/swagger.go @@ -101,14 +101,15 @@ type SwaggerDefinitionPropertyItem struct { // // Date : 16:12 2024/4/19 type Swagger struct { - Schemes []string `json:"schemes"` - Swagger string `json:"swagger"` // swagger版本 - 2.0 - Host string `json:"host"` // 服务域名 - BasePath string `json:"basePath"` // 基础path - Info Info `json:"info"` // 文档描述信息 - Paths map[string]map[string]*SwaggerPathConfig `json:"paths"` // 接口列表 : 接口 => 请求方法 => 请求配置 - Definitions map[string]*SwaggerDefinition `json:"definitions"` // 数据定义 - Responses map[string]*SwaggerPathConfigResponse `json:"responses"` // 响应结构列表 + Schemes []string `json:"schemes"` + Swagger string `json:"swagger"` // swagger版本 - 2.0 + Host string `json:"host"` // 服务域名 + BasePath string `json:"basePath"` // 基础path + Info Info `json:"info"` // 文档描述信息 + Paths map[string]map[string]*SwaggerPathConfig `json:"paths"` // 接口列表 : 接口 => 请求方法 => 请求配置 + Definitions map[string]*SwaggerDefinition `json:"definitions"` // 数据定义 + Responses map[string]*SwaggerPathConfigResponse `json:"responses"` // 响应结构列表 + SecurityDefinitions map[string]*SwaggerPathConfigParameter `json:"securityDefinitions"` // 安全选项 } // SwaggerInput ... diff --git a/swagger.go b/swagger.go index 447a5ba..5df17fe 100644 --- a/swagger.go +++ b/swagger.go @@ -31,17 +31,38 @@ func ParseSwagger2(docContent []byte) (*define.DocParseResult, error) { return nil, errors.New("parse swagger json fail : " + err.Error()) } docResult := &define.DocParseResult{ - Domain: swaggerDoc.Host, - Title: swaggerDoc.Info.Title, - Description: swaggerDoc.Info.Description, - UriList: make([]*define.UriBaseConfig, 0), + Domain: swaggerDoc.Host, + Title: swaggerDoc.Info.Title, + Description: swaggerDoc.Info.Description, + UriList: make([]*define.UriBaseConfig, 0), + GlobalSecurityParamList: make([]*define.ParamConfig, 0), } if docResult.UriList, err = buildUriList(&swaggerDoc); nil != err { return nil, err } + docResult.GlobalSecurityParamList = buildSwagger2GlobalSecurityParamList(&swaggerDoc) return docResult, nil } +// buildSwagger2GlobalSecurityParamList 构建全局安全类参数 +func buildSwagger2GlobalSecurityParamList(swaggerDoc *define.Swagger) []*define.ParamConfig { + result := make([]*define.ParamConfig, 0) + for paramName, paramConfig := range swaggerDoc.SecurityDefinitions { + if len(paramConfig.In) == 0 { + continue + } + result = append(result, &define.ParamConfig{ + Location: GetParamLocation(paramConfig.In).String(), + Path: paramName, + Type: GetDataType(paramConfig.Type, "").String(), + Title: paramName, + Description: paramConfig.Description, + Required: false, + }) + } + return result +} + // 解析uri列表 func buildUriList(swaggerDoc *define.Swagger) ([]*define.UriBaseConfig, error) { uriList := make([]*define.UriBaseConfig, 0)