|
- #!/bin/bash
-
- # 生成基础Swagger JSON文件
- swag init --parseDependency --parseInternal --output ./docs
-
- # 提取带有 "open" 标签的路径
- jq '.paths | with_entries(
- select(
- (.value.post?.tags? | type == "array" and any(. == "数据中心-----OPEN")) or
- (.value.get?.tags? | type == "array" and any(. == "数据中心-----OPEN")) or
- (.value.put?.tags? | type == "array" and any(. == "数据中心-----OPEN")) or
- (.value.delete?.tags? | type == "array" and any(. == "数据中心-----OPEN"))
- )
- ) // {} as $filtered_paths | . * { paths: $filtered_paths }' docs/swagger.json > open_paths.json
-
- # 提取 definitions 和 securityDefinitions
- jq '.definitions' docs/swagger.json > definitions.json
- jq '.securityDefinitions' docs/swagger.json > securityDefinitions.json
-
- # 替换 description 中的内容
- jq 'with_entries(
- .value |= (
- if .post then
- .post.parameters |= map(
- if .description == "验证参数Bearer和token空格拼接" then
- .description = "秘钥内容"
- else
- .
- end
- )
- else
- .
- end,
- if .get then
- .get.parameters |= map(
- if .description == "验证参数Bearer和token空格拼接" then
- .description = "秘钥内容"
- else
- .
- end
- )
- else
- .
- end,
- if .put then
- .put.parameters |= map(
- if .description == "验证参数Bearer和token空格拼接" then
- .description = "秘钥内容"
- else
- .
- end
- )
- else
- .
- end,
- if .delete then
- .delete.parameters |= map(
- if .description == "验证参数Bearer和token空格拼接" then
- .description = "秘钥内容"
- else
- .
- end
- )
- else
- .
- end
- )
- )' open_paths.json > updated_tmp_open_paths.json
-
- # 将 "name": "Authorization" 替换成 "name": "AppSecret"
- jq 'walk(if type == "object" and has("name") and .name == "Authorization" then .name = "AppSecret" else . end)' updated_tmp_open_paths.json > updated_open_paths.json
-
- # 创建新的 Swagger 配置文件
- cat <<EOF > temp.json
- {
- "swagger": "2.0",
- "info": {
- "description": "广告联盟接口",
- "title": "广告联盟",
- "termsOfService": "http://swagger.io/terms/",
- "contact": {
- "name": "zhiying",
- "url": "http://www.swagger.io/support",
- "email": "zhiyongos@163.com"
- },
- "license": {
- "name": "Apache 2.0",
- "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
- },
- "version": "1.0"
- },
- "host": "xxxxx.adcms.zhiyingos.cn",
- "paths": {
-
- },
- "definitions": {
-
- },
- "securityDefinitions": {
-
- }
- }
- EOF
-
- # 合并路径信息到open.json
- jq -n '
- input as $paths |
- input as $config |
- $config | .paths = $paths
- ' updated_open_paths.json temp.json > temp_1.json
- jq -n '
- input as $paths |
- input as $config |
- $config | .definitions = $paths
- ' definitions.json temp_1.json > temp_2.json
- jq -n '
- input as $paths |
- input as $config |
- $config | .securityDefinitions = $paths
- ' securityDefinitions.json temp_2.json > open.json
-
- # 删除中间生成的文件
- rm -f open_paths.json temp_1.json temp_2.json updated_open_paths.json temp.json definitions.json securityDefinitions.json updated_tmp_open_paths.json
-
- # 将最终生成的文件移动到docs目录下
- mv open.json docs/open.json
- echo "Generated open.json successfully."
|