diff --git a/doc-apis-core/pom.xml b/doc-apis-core/pom.xml index 92a7454702472ee6df91d01c832eb6a9713bbce9..c37053ed6807e5957a0a2ad264fca5b199eec6a6 100644 --- a/doc-apis-core/pom.xml +++ b/doc-apis-core/pom.xml @@ -7,7 +7,7 @@ com.doc-apis doc-apis-parent - 1.0.0 + 1.0.1 ../doc-apis-parent diff --git a/doc-apis-core/src/main/java/com/docapis/core/DocsConfig.java b/doc-apis-core/src/main/java/com/docapis/core/DocsConfig.java index 8d4151f4bd5b76c3637c2bdcaaa781767b0931aa..0d03801a51b371d25ae2f5e3cad6e6cef4487d51 100644 --- a/doc-apis-core/src/main/java/com/docapis/core/DocsConfig.java +++ b/doc-apis-core/src/main/java/com/docapis/core/DocsConfig.java @@ -23,6 +23,7 @@ public class DocsConfig { Boolean autoGenerate = Boolean.FALSE; // 自动生成所有Controller的接口文档,不需要@DocApi注解 Locale locale = Locale.getDefault(); Boolean openReflection = Boolean.TRUE; // 是否开启对象反射 + Boolean enableCamelToUnderline = Boolean.FALSE; // 是否开启驼峰转下划线 String rapHost; String rapLoginCookie; @@ -162,4 +163,12 @@ public class DocsConfig { public void setClassificationLevel(String classificationLevel) { this.classificationLevel = classificationLevel; } + + public Boolean getEnableCamelToUnderline() { + return enableCamelToUnderline; + } + + public void setEnableCamelToUnderline(Boolean enableCamelToUnderline) { + this.enableCamelToUnderline = enableCamelToUnderline; + } } diff --git a/doc-apis-core/src/main/java/com/docapis/core/Utils.java b/doc-apis-core/src/main/java/com/docapis/core/Utils.java index c69d41f4ae577176e8981bb2de0a881d4117c48f..3b35eaab6b4a807edf096a80c144967de449b953 100644 --- a/doc-apis-core/src/main/java/com/docapis/core/Utils.java +++ b/doc-apis-core/src/main/java/com/docapis/core/Utils.java @@ -231,18 +231,29 @@ public class Utils { return parts[parts.length - 1]; } - private static BuildToolTypeEnum getProjectBuildTool(File projectDir) { - if (new File(projectDir, "settings.gradle").exists()) { - return BuildToolTypeEnum.GRADLE; + /** + * 字符串驼峰转下划线格式 + * + * @param param 需要转换的字符串 + * @return 转换好的字符串 + */ + public static String camelToUnderline(String param) { + if (param == null || param.trim().length() <= 0) { + return ""; } - - if (new File(projectDir, "pom.xml").exists()) { - return BuildToolTypeEnum.MAVEN; + int len = param.length(); + StringBuilder sb = new StringBuilder(len); + for (int i = 0; i < len; i++) { + char c = param.charAt(i); + if (Character.isUpperCase(c) && i > 0) { + sb.append("_"); + } + sb.append(Character.toLowerCase(c)); } - - return BuildToolTypeEnum.UNKNOWN; + return sb.toString(); } + public static List getModuleNames(File projectDir) { BuildToolTypeEnum buildToolTypeEnum = getProjectBuildTool(projectDir); @@ -462,4 +473,16 @@ public class Utils { return cleanedStr1.equals(cleanedStr2); } + + private static BuildToolTypeEnum getProjectBuildTool(File projectDir) { + if (new File(projectDir, "settings.gradle").exists()) { + return BuildToolTypeEnum.GRADLE; + } + + if (new File(projectDir, "pom.xml").exists()) { + return BuildToolTypeEnum.MAVEN; + } + + return BuildToolTypeEnum.UNKNOWN; + } } diff --git a/doc-apis-core/src/main/java/com/docapis/core/constant/CoreConstants.java b/doc-apis-core/src/main/java/com/docapis/core/constant/CoreConstants.java index ca17f1d991594825edb342336581db56e56c9e23..b196aa2422e6661aa26cd096a686867ad13af089 100644 --- a/doc-apis-core/src/main/java/com/docapis/core/constant/CoreConstants.java +++ b/doc-apis-core/src/main/java/com/docapis/core/constant/CoreConstants.java @@ -12,7 +12,7 @@ public interface CoreConstants { /** * 版本号发版及包调整时须更新此字段,死贫道不能死道友,此举可使用户可免配 */ - String VERSION = "1.0.0"; + String VERSION = "1.0.1"; /** * jar 完整路径 */ diff --git a/doc-apis-core/src/main/java/com/docapis/core/parser/AbsControllerParser.java b/doc-apis-core/src/main/java/com/docapis/core/parser/AbsControllerParser.java index 5eea04db5325817b71d40d50dd2ffcb26d415479..2833e82b9e57d389cbf4c68714323afd99ef1e1d 100644 --- a/doc-apis-core/src/main/java/com/docapis/core/parser/AbsControllerParser.java +++ b/doc-apis-core/src/main/java/com/docapis/core/parser/AbsControllerParser.java @@ -1,5 +1,9 @@ package com.docapis.core.parser; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.parser.Feature; import com.docapis.core.*; import com.docapis.core.constant.ChangeFlag; import com.github.javaparser.ast.CompilationUnit; @@ -70,6 +74,14 @@ public abstract class AbsControllerParser { afterHandleController(controllerNode, c); }); } + + // 驼峰转下划线 + Optional.ofNullable(DocContext.getDocsConfig()).ifPresent(config -> { + if (config.getEnableCamelToUnderline()) { + this.requestNodesCamelToUnderline(); + } + }); + return controllerNode; } @@ -315,4 +327,88 @@ public abstract class AbsControllerParser { && Utils.toJson(requestNode.getHeader()).equals(Utils.toJson(lastRequestNode.getHeader())) && requestNode.getResponseNode().toJsonApi().equals(lastRequestNode.getResponseNode().toJsonApi()); } + + + private void requestNodesCamelToUnderline() { + if (controllerNode.getRequestNodes() != null) { + Optional.ofNullable(controllerNode.getRequestNodes()) + .ifPresent(requestNodes -> requestNodes.forEach(requestNode -> { + // 入参 + Optional.ofNullable(requestNode.getParamNodes()) + .ifPresent(paramNodes -> paramNodes.forEach(paramNode -> { + if (paramNode == null) { + return; + } + if (paramNode.getJsonBody()) { + try { + // 使用 Feature.OrderedField 保持 JSON 键的顺序 + JSONObject jsonObject = JSON.parseObject(paramNode.getDescription(), Feature.OrderedField); + JSONObject converted = camelToUnderline(jsonObject); + paramNode.setDescription(Utils.toPrettyJson(converted)); + } catch (Throwable e) { + paramNode.setDescription(Utils.camelToUnderline(paramNode.getDescription())); + } + } else { + paramNode.setName(Utils.camelToUnderline(paramNode.getName())); + } + })); + + // 出参 + Optional.ofNullable(requestNode.getResponseNode()) + .flatMap(responseNode -> Optional.ofNullable(responseNode.getChildNodes())) + .ifPresent(this::camelToUnderline); + })); + } + } + + private JSONObject camelToUnderline(JSONObject jsonObject) { + if (jsonObject == null) { + return null; + } + JSONObject result = new JSONObject(true); // 使用有序的 JSONObject + for (String key : jsonObject.keySet()) { + Object value = jsonObject.get(key); + String newKey = Utils.camelToUnderline(key); + // 递归处理嵌套的 JSONObject 和 JSONArray + if (value instanceof JSONObject) { + value = camelToUnderline((JSONObject) value); + } else if (value instanceof JSONArray) { + value = camelToUnderline((JSONArray) value); + } + result.put(newKey, value); + } + return result; + } + + private JSONArray camelToUnderline(JSONArray jsonArray) { + if (jsonArray == null) { + return null; + } + JSONArray result = new JSONArray(); + for (Object item : jsonArray) { + if (item instanceof JSONObject) { + result.add(camelToUnderline((JSONObject) item)); + } else if (item instanceof JSONArray) { + result.add(camelToUnderline((JSONArray) item)); + } else { + result.add(item); + } + } + return result; + } + + private void camelToUnderline(List fieldNodes) { + if (fieldNodes == null || fieldNodes.isEmpty()) { + return; + } + fieldNodes.forEach(fieldNode -> { + if (fieldNode == null) { + return; + } + if (fieldNode.getChildNode() != null) { + camelToUnderline(fieldNode.getChildNode().getChildNodes()); + } + fieldNode.setName(Utils.camelToUnderline(fieldNode.getName())); + }); + } } diff --git a/doc-apis-parent/pom.xml b/doc-apis-parent/pom.xml index 524db8d81caca513007c00dc20a4ab5f029041d6..c4c8fc008aeaf4162006047deb0e3172187d505c 100644 --- a/doc-apis-parent/pom.xml +++ b/doc-apis-parent/pom.xml @@ -8,7 +8,8 @@ com.doc-apis doc-apis-parent - 1.0.0 + 1.0.1 + doc-apis-parent doc generator @@ -31,7 +32,7 @@ - 1.0.0 + 1.0.1 1.8 1.2.83 3.25.10 diff --git a/doc-apis-starter/pom.xml b/doc-apis-starter/pom.xml index 39dab582b5c1030c24f2fb61a08aebb8ce503576..85770f1fb4fd634866490816aa17847475a2efbe 100644 --- a/doc-apis-starter/pom.xml +++ b/doc-apis-starter/pom.xml @@ -7,7 +7,7 @@ com.doc-apis doc-apis-parent - 1.0.0 + 1.0.1 ../doc-apis-parent diff --git a/doc-apis-starter/src/main/java/com/docapis/starter/config/AutoConfig.java b/doc-apis-starter/src/main/java/com/docapis/starter/config/AutoConfig.java index d7018e9f4cdd8686f01c8e279a5ce61c7ca291f2..e8c8d22d9b5ebe3f98b406782e73258c3a6eca96 100644 --- a/doc-apis-starter/src/main/java/com/docapis/starter/config/AutoConfig.java +++ b/doc-apis-starter/src/main/java/com/docapis/starter/config/AutoConfig.java @@ -52,6 +52,9 @@ public class AutoConfig implements InitializingBean { config.setDocsPath(docPlusConfigProperties.getDocPath()); // 配置自动生成 config.setAutoGenerate(docPlusConfigProperties.isAutoGenerate()); + // 驼峰转下划线 + config.setEnableCamelToUnderline(docPlusConfigProperties.isEnableCamelToUnderline()); + // 添加MarkDown文档 if (docPlusConfigProperties.isGenerateMarkDown()) { config.addPlugin(new MarkdownDocPlugin()); diff --git a/doc-apis-starter/src/main/java/com/docapis/starter/property/DocPlusConfigProperties.java b/doc-apis-starter/src/main/java/com/docapis/starter/property/DocPlusConfigProperties.java index 85606b9cedf1fe4f7645c17e18259ac0d46480cd..ee4525762093d1d2ffdfaf5c62eec4dcb573a411 100644 --- a/doc-apis-starter/src/main/java/com/docapis/starter/property/DocPlusConfigProperties.java +++ b/doc-apis-starter/src/main/java/com/docapis/starter/property/DocPlusConfigProperties.java @@ -51,6 +51,10 @@ public class DocPlusConfigProperties { * 接口文档密级 */ private String classificationLevel; + /** + * 是否开启驼峰转下划线 + */ + private boolean enableCamelToUnderline = true; /** * 跨域相关配置 @@ -150,4 +154,12 @@ public class DocPlusConfigProperties { public void setCorsConfig(CorsConfigProperties corsConfig) { this.corsConfig = corsConfig; } + + public boolean isEnableCamelToUnderline() { + return enableCamelToUnderline; + } + + public void setEnableCamelToUnderline(boolean enableCamelToUnderline) { + this.enableCamelToUnderline = enableCamelToUnderline; + } } diff --git a/doc-apis-test/pom.xml b/doc-apis-test/pom.xml index 5c74d03065cfbe9afcf5566b094b0d61f3e1fb01..8c8213b40da5018b2a8093c390505568649a3546 100644 --- a/doc-apis-test/pom.xml +++ b/doc-apis-test/pom.xml @@ -7,7 +7,7 @@ com.doc-apis doc-apis - 1.0.0 + 1.0.1 doc-apis-test @@ -38,7 +38,7 @@ org.dromara.easy-es easy-es-boot-starter - 2.0.0-beta8 + 3.0.0 diff --git a/doc-apis-test/src/main/resources/application.yml b/doc-apis-test/src/main/resources/application.yml index 8b137891791fe96927ad78e64b0aad7bded08bdc..55be95730b102476855c72b98b81d8f7524ab9a6 100644 --- a/doc-apis-test/src/main/resources/application.yml +++ b/doc-apis-test/src/main/resources/application.yml @@ -1 +1,2 @@ - +doc-apis: + enable-camel-to-underline: true diff --git a/pom.xml b/pom.xml index 847190e042ee585d8c5c74ba8276d6958b4c123d..ba1f5fb2dcd3e31bbb0f6904f0e0da1e64fd5ec0 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.doc-apis doc-apis - 1.0.0 + 1.0.1 doc-apis doc generator @@ -18,7 +18,7 @@ - 1.0.0 + 1.0.1 2.7.6 5.3.3