package com.github.microservice.utils; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.github.microservice.models.openAPI.OpenApiInfo; 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 scanAPI(String str) { List 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); } } OpenApiInfo openAPI = new OpenApiInfo(); openAPI.setPath(key); openAPI.setRequestMethod(method); openAPI.setContentType("application/json"); openAPI.setDescription(object.getStr("description")); openAPI.setName(object.getStr("operationId")); openAPI.setGroup("一卡通平台"); openAPI.setRequestParam(JSONUtil.toJsonStr(params)); openAPI.setResponseParam(JSONUtil.toJsonStr(response)); openAPIS.add(openAPI); } }); } } catch (Exception e) { log.error(e.getMessage()); } return openAPIS; } }