| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- 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<OpenApiInfo> scanAPI(String str) {
- List<OpenApiInfo> 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;
- }
- }
|