TRX 1 年間 前
コミット
942dea2df9
1 ファイル変更120 行追加0 行削除
  1. 120 0
      src/main/java/com/github/microservice/utils/OpenAPIScan.java

+ 120 - 0
src/main/java/com/github/microservice/utils/OpenAPIScan.java

@@ -0,0 +1,120 @@
+package com.github.microservice.utils;
+
+import cn.hutool.json.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author TRX
+ * @date 2024/9/29
+ */
+@Slf4j
+public class OpenAPIScan {
+
+    public static List<JSONObject> scanAPI(String str) {
+        List<JSONObject> openAPIS = new ArrayList<>();
+        try {
+            JSONObject jsonObject = new JSONObject(str);
+            JSONObject schemas = new JSONObject();
+            if (jsonObject.containsKey("components")) {
+                schemas = jsonObject.getJSONObject("components").getJSONObject("schemas");
+            }
+
+            if (jsonObject.containsKey("paths")) {
+                JSONObject paths = jsonObject.getJSONObject("paths");
+                JSONObject finalSchemas = schemas;
+
+                paths.keySet().forEach(key -> {
+                    if (key.indexOf("openAPI") >= 0) {
+                        log.info("key; {}", key);
+                        JSONObject tempObj = paths.getJSONObject(key);
+                        String method = "";
+
+                        JSONObject object = null;
+                        if (tempObj.containsKey("post")) {
+                            method = "post";
+                            object = tempObj.getJSONObject("post");
+                        }
+
+                        String requestKey = "";
+                        String responseKey = "";
+                        if (object != null) {
+                            if (object.containsKey("requestBody")) {
+                                JSONObject requestBody = object.getJSONObject("requestBody");
+                                if (requestBody.containsKey("content")) {
+                                    JSONObject content = requestBody.getJSONObject("content");
+                                    if (content.containsKey("application/json")) {
+                                        JSONObject js = content.getJSONObject("application/json");
+                                        if (js.containsKey("schema")) {
+                                            JSONObject schema = js.getJSONObject("schema");
+                                            if (schema.containsKey("$ref")) {
+                                                String _str = schema.getStr("$ref");
+                                                String[] arr = _str.split("/");
+                                                requestKey = arr[arr.length - 1];
+                                            }
+                                        }
+                                    }
+                                }
+                            }
+                            if (object.containsKey("responses")) {
+                                JSONObject responses = object.getJSONObject("responses");
+                                if (responses.containsKey("200")) {
+                                    JSONObject content200 = responses.getJSONObject("200");
+                                    if (content200.containsKey("content")) {
+                                        JSONObject content = content200.getJSONObject("content");
+                                        if (content.containsKey("*/*")) {
+                                            JSONObject body = content.getJSONObject("*/*");
+                                            if (body.containsKey("schema")) {
+                                                JSONObject schema = body.getJSONObject("schema");
+                                                if (schema.containsKey("$ref")) {
+                                                    String _str = schema.getStr("$ref");
+                                                    String[] arr = _str.split("/");
+                                                    responseKey = arr[arr.length - 1];
+                                                }
+                                            }
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                        JSONObject params = new JSONObject();
+                        JSONObject response = new JSONObject();
+                        log.info("requestKey: {}", requestKey);
+                        log.info("responseKey: {}", responseKey);
+                        if (StringUtils.isNotEmpty(requestKey)) {
+                            if (finalSchemas.containsKey(requestKey)) {
+                                params = finalSchemas.getJSONObject(requestKey);
+                            }
+                        }
+                        if (StringUtils.isNotEmpty(responseKey)) {
+                            if (!responseKey.equals("ResultContent")) {
+                                if (responseKey.contains("ResultContent")) {
+                                    responseKey = responseKey.replace("ResultContent", "");
+                                }
+                            }
+                            if (finalSchemas.containsKey(responseKey)) {
+                                response = finalSchemas.getJSONObject(responseKey);
+                            }
+                        }
+                        JSONObject openAPI = new JSONObject();
+                        openAPI.put("method", method);
+                        openAPI.put("path", key);
+                        openAPI.set("params", params);
+                        openAPI.set("response", response);
+                        openAPI.set("description", object.get("description"));
+                        openAPI.set("summary", object.get("summary"));
+                        openAPI.set("operationId", object.get("operationId"));
+                        openAPIS.add(openAPI);
+                    }
+                });
+            }
+        } catch (Exception e) {
+            log.error(e.getMessage());
+        }
+        return openAPIS;
+    }
+
+}