广告平台(站长使用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate_swagger.sh 3.7 KiB

4 weken geleden
3 weken geleden
4 weken geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/bash
  2. # 生成基础Swagger JSON文件
  3. swag init --parseDependency --parseInternal --output ./docs
  4. # 提取带有 "open" 标签的路径
  5. jq '.paths | with_entries(
  6. select(
  7. (.value.post?.tags? | type == "array" and any(. == "数据中心------OPEN")) or
  8. (.value.get?.tags? | type == "array" and any(. == "数据中心------OPEN")) or
  9. (.value.put?.tags? | type == "array" and any(. == "数据中心------OPEN")) or
  10. (.value.delete?.tags? | type == "array" and any(. == "数据中心------OPEN"))
  11. )
  12. ) // {} as $filtered_paths | . * { paths: $filtered_paths }' docs/swagger.json > open_paths.json
  13. # 提取 definitions 和 securityDefinitions
  14. jq '.definitions' docs/swagger.json > definitions.json
  15. jq '.securityDefinitions' docs/swagger.json > securityDefinitions.json
  16. # 替换 description 中的内容
  17. jq 'with_entries(
  18. .value |= (
  19. if .post then
  20. .post.parameters |= map(
  21. if .description == "验证参数Bearer和token空格拼接" then
  22. .description = "秘钥内容"
  23. else
  24. .
  25. end
  26. )
  27. else
  28. .
  29. end,
  30. if .get then
  31. .get.parameters |= map(
  32. if .description == "验证参数Bearer和token空格拼接" then
  33. .description = "秘钥内容"
  34. else
  35. .
  36. end
  37. )
  38. else
  39. .
  40. end,
  41. if .put then
  42. .put.parameters |= map(
  43. if .description == "验证参数Bearer和token空格拼接" then
  44. .description = "秘钥内容"
  45. else
  46. .
  47. end
  48. )
  49. else
  50. .
  51. end,
  52. if .delete then
  53. .delete.parameters |= map(
  54. if .description == "验证参数Bearer和token空格拼接" then
  55. .description = "秘钥内容"
  56. else
  57. .
  58. end
  59. )
  60. else
  61. .
  62. end
  63. )
  64. )' open_paths.json > updated_tmp_open_paths.json
  65. # 将 "name": "Authorization" 替换成 "name": "AppSecret"
  66. 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
  67. # 创建新的 Swagger 配置文件
  68. cat <<EOF > temp.json
  69. {
  70. "swagger": "2.0",
  71. "info": {
  72. "description": "广告联盟接口",
  73. "title": "广告联盟",
  74. "termsOfService": "http://swagger.io/terms/",
  75. "contact": {
  76. "name": "zhiying",
  77. "url": "http://www.swagger.io/support",
  78. "email": "zhiyongos@163.com"
  79. },
  80. "license": {
  81. "name": "Apache 2.0",
  82. "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
  83. },
  84. "version": "1.0"
  85. },
  86. "host": "xxxxx.adcms.zhiyingos.cn",
  87. "paths": {
  88. },
  89. "definitions": {
  90. },
  91. "securityDefinitions": {
  92. }
  93. }
  94. EOF
  95. # 合并路径信息到open.json
  96. jq -n '
  97. input as $paths |
  98. input as $config |
  99. $config | .paths = $paths
  100. ' updated_open_paths.json temp.json > temp_1.json
  101. jq -n '
  102. input as $paths |
  103. input as $config |
  104. $config | .definitions = $paths
  105. ' definitions.json temp_1.json > temp_2.json
  106. jq -n '
  107. input as $paths |
  108. input as $config |
  109. $config | .securityDefinitions = $paths
  110. ' securityDefinitions.json temp_2.json > open.json
  111. # 删除中间生成的文件
  112. 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
  113. # 将最终生成的文件移动到docs目录下
  114. mv open.json docs/open.json
  115. echo "Generated open.json successfully."