OpenAPIScan.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package com.github.microservice.utils;
  2. import cn.hutool.json.JSONObject;
  3. import cn.hutool.json.JSONUtil;
  4. import com.github.microservice.models.openAPI.OpenApiInfo;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.lang3.StringUtils;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /**
  10. * @author TRX
  11. * @date 2024/9/29
  12. */
  13. @Slf4j
  14. public class OpenAPIScan {
  15. public static List<OpenApiInfo> scanAPI(String str) {
  16. List<OpenApiInfo> openAPIS = new ArrayList<>();
  17. try {
  18. JSONObject jsonObject = new JSONObject(str);
  19. JSONObject schemas = new JSONObject();
  20. if (jsonObject.containsKey("components")) {
  21. schemas = jsonObject.getJSONObject("components").getJSONObject("schemas");
  22. }
  23. if (jsonObject.containsKey("paths")) {
  24. JSONObject paths = jsonObject.getJSONObject("paths");
  25. JSONObject finalSchemas = schemas;
  26. paths.keySet().forEach(key -> {
  27. if (key.indexOf("openAPI") >= 0) {
  28. log.info("key; {}", key);
  29. JSONObject tempObj = paths.getJSONObject(key);
  30. String method = "";
  31. JSONObject object = null;
  32. if (tempObj.containsKey("post")) {
  33. method = "post";
  34. object = tempObj.getJSONObject("post");
  35. }
  36. String requestKey = "";
  37. String responseKey = "";
  38. if (object != null) {
  39. if (object.containsKey("requestBody")) {
  40. JSONObject requestBody = object.getJSONObject("requestBody");
  41. if (requestBody.containsKey("content")) {
  42. JSONObject content = requestBody.getJSONObject("content");
  43. if (content.containsKey("application/json")) {
  44. JSONObject js = content.getJSONObject("application/json");
  45. if (js.containsKey("schema")) {
  46. JSONObject schema = js.getJSONObject("schema");
  47. if (schema.containsKey("$ref")) {
  48. String _str = schema.getStr("$ref");
  49. String[] arr = _str.split("/");
  50. requestKey = arr[arr.length - 1];
  51. }
  52. }
  53. }
  54. }
  55. }
  56. if (object.containsKey("responses")) {
  57. JSONObject responses = object.getJSONObject("responses");
  58. if (responses.containsKey("200")) {
  59. JSONObject content200 = responses.getJSONObject("200");
  60. if (content200.containsKey("content")) {
  61. JSONObject content = content200.getJSONObject("content");
  62. if (content.containsKey("*/*")) {
  63. JSONObject body = content.getJSONObject("*/*");
  64. if (body.containsKey("schema")) {
  65. JSONObject schema = body.getJSONObject("schema");
  66. if (schema.containsKey("$ref")) {
  67. String _str = schema.getStr("$ref");
  68. String[] arr = _str.split("/");
  69. responseKey = arr[arr.length - 1];
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. JSONObject params = new JSONObject();
  78. JSONObject response = new JSONObject();
  79. log.info("requestKey: {}", requestKey);
  80. log.info("responseKey: {}", responseKey);
  81. if (StringUtils.isNotEmpty(requestKey)) {
  82. if (finalSchemas.containsKey(requestKey)) {
  83. params = finalSchemas.getJSONObject(requestKey);
  84. }
  85. }
  86. if (StringUtils.isNotEmpty(responseKey)) {
  87. if (!responseKey.equals("ResultContent")) {
  88. if (responseKey.contains("ResultContent")) {
  89. responseKey = responseKey.replace("ResultContent", "");
  90. }
  91. }
  92. if (finalSchemas.containsKey(responseKey)) {
  93. response = finalSchemas.getJSONObject(responseKey);
  94. }
  95. }
  96. OpenApiInfo openAPI = new OpenApiInfo();
  97. openAPI.setPath(key);
  98. openAPI.setRequestMethod(method);
  99. openAPI.setContentType("application/json");
  100. openAPI.setDescription(object.getStr("description"));
  101. openAPI.setName(object.getStr("operationId"));
  102. openAPI.setGroup("一卡通平台");
  103. openAPI.setRequestParam(JSONUtil.toJsonStr(params));
  104. openAPI.setResponseParam(JSONUtil.toJsonStr(response));
  105. openAPIS.add(openAPI);
  106. }
  107. });
  108. }
  109. } catch (Exception e) {
  110. log.error(e.getMessage());
  111. }
  112. return openAPIS;
  113. }
  114. }