From 9a25ddd318e80a4a9b8017b3a53c8f679a1a196c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E5=AD=90=E7=83=9C?= <2061403103@qq.com> Date: Sat, 30 Aug 2025 13:02:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E9=A9=BC=E5=B3=B0=E8=BD=AC=E4=B8=8B?= =?UTF-8?q?=E5=88=92=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc-apis-core/pom.xml | 2 +- .../java/com/docapis/core/DocsConfig.java | 9 ++ .../src/main/java/com/docapis/core/Utils.java | 39 ++++++-- .../docapis/core/constant/CoreConstants.java | 2 +- .../core/parser/AbsControllerParser.java | 96 +++++++++++++++++++ doc-apis-parent/pom.xml | 5 +- doc-apis-starter/pom.xml | 2 +- .../docapis/starter/config/AutoConfig.java | 3 + .../property/DocPlusConfigProperties.java | 12 +++ doc-apis-test/pom.xml | 4 +- .../src/main/resources/application.yml | 3 +- pom.xml | 4 +- 12 files changed, 163 insertions(+), 18 deletions(-) diff --git a/doc-apis-core/pom.xml b/doc-apis-core/pom.xml index 92a7454..c37053e 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 8d4151f..0d03801 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 c69d41f..3b35eaa 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 ca17f1d..b196aa2 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 5eea04d..2833e82 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 524db8d..c4c8fc0 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 39dab58..85770f1 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 d7018e9..e8c8d22 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 85606b9..ee45257 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 5c74d03..8c8213b 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 8b13789..55be957 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 847190e..ba1f5fb 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 -- Gitee