support convert Camel to Underline for request or response field.

This commit is contained in:
oppofind 2020-05-24 15:42:18 +08:00
parent 05cef3879d
commit 70f2efad21
6 changed files with 54 additions and 2 deletions

View File

@ -17,6 +17,7 @@ Or a static html document. If you are tired of the numerous annotations and stro
- Support for loading source code from outside the project to generate field comments (including the sources jar package).
- Support for generating multiple formats of documents: Markdown, HTML5, Asciidoctor,Postman Collection json.
- Support for exporting error codes and data dictionary codes to API documentation.
- Support Apache Dubbo RPC。
## Getting started
[Smart-doc Samples](https://github.com/shalousun/smart-doc-demo.git)。
```
@ -87,6 +88,8 @@ When you need to use smart-doc to generate more API document information, you ca
  "md5EncryptedHtmlName": false, // only used if each controller generates an html file
  "projectName": "smart-doc", // Configure your own project name
  "skipTransientField": true, // Not currently implemented
"requestFieldToUnderline":true, //convert request field to underline
"responseFieldToUnderline":true,//convert response field to underline
  "dataDictionaries": [// Configure the data dictionary, no need to set
    {
      "title": "Order Status", // The name of the data dictionary
@ -201,7 +204,7 @@ Thanks to the following people who have submitted major pull requests:
## Who is using
These are only part of the companies using smart-doc, for reference only. If you are using smart-doc, please [add your company here](https://github.com/shalousun/smart-doc/issues/12) to tell us your scenario to make smart-doc better.
![iFLYTEK](https://raw.githubusercontent.com/shalousun/smart-doc/dev/images/known-users/iflytek.png)
![IFLYTEK](https://raw.githubusercontent.com/shalousun/smart-doc/dev/images/known-users/iflytek.png)
&nbsp;&nbsp;<img src="https://raw.githubusercontent.com/shalousun/smart-doc/dev/images/known-users/oneplus.png" title="OnePlus" width="83px" height="83px"/>
&nbsp;&nbsp;<img src="https://raw.githubusercontent.com/shalousun/smart-doc/dev/images/known-users/xiaomi.png" title="Xiaomi" width="170px" height="83px"/>
<img src="https://raw.githubusercontent.com/shalousun/smart-doc/dev/images/known-users/yuanmengjiankang.png" title="yuanmengjiankang" width="260px" height="83px"/>

View File

@ -19,6 +19,7 @@ smart-doc完全基于接口源码分析来生成接口文档完全做到零
- 开放文档数据,可自由实现接入文档管理系统。
- 支持导出错误码和定义在代码中的各种字典码到接口文档。
- 支持maven、gradle插件式轻松集成。
- 支持Apache Dubbo RPC接口文档生成。
## Getting started
smart-doc使用和测试可参考[smart-doc demo](https://gitee.com/sunyurepository/api-doc-test.git)。
```
@ -85,6 +86,8 @@ smart-doc官方目前已经开发完成maven 插件和gradle你可以根据
"projectName": "smart-doc",//配置自己的项目名称
"skipTransientField": true,//目前未实现
"showAuthor":true,//是否显示接口作者名称默认是true,不想显示可关闭
"requestFieldToUnderline":true,//自动将驼峰入参字段在文档中转为下划线格式,//@since 1.8.7版本开始
"responseFieldToUnderline":true,//自动将驼峰入参字段在文档中转为下划线格式,//@since 1.8.7版本开始
"dataDictionaries": [ //配置数据字典,没有需求可以不设置
{
"title": "http状态码字典", //数据字典的名称
@ -190,7 +193,7 @@ Smart-doc is under the Apache 2.0 license. See the [LICENSE](https://gitee.com
## Who is using
> 排名不分先后,更多接入公司,欢迎在[https://gitee.com/sunyurepository/smart-doc/issues/I1594T](https://gitee.com/sunyurepository/smart-doc/issues/I1594T)登记(仅供开源用户参考)
![iFLYTEK](https://gitee.com/sunyurepository/smart-doc/raw/master/images/known-users/iflytek.png)
![IFLYTEK](https://gitee.com/sunyurepository/smart-doc/raw/master/images/known-users/iflytek.png)
&nbsp;&nbsp;<img src="https://gitee.com/sunyurepository/smart-doc/raw/master/images/known-users/oneplus.png" title="一加" width="83px" height="83px"/>
&nbsp;&nbsp;<img src="https://gitee.com/sunyurepository/smart-doc/raw/master/images/known-users/xiaomi.png" title="小米" width="170px" height="83px"/>
<img src="https://gitee.com/sunyurepository/smart-doc/raw/master/images/known-users/yuanmengjiankang.png" title="远盟健康" width="260px" height="83px"/>

View File

@ -152,6 +152,8 @@ public class JsonBuildHelper {
data.append("{\"object\":\" any object\"},");
// throw new RuntimeException("Please do not return java.lang.Object directly in api interface.");
} else {
boolean requestFieldToUnderline = builder.getApiConfig().isRequestFieldToUnderline();
boolean responseFieldToUnderline = builder.getApiConfig().isResponseFieldToUnderline();
List<DocJavaField> fields = JavaClassUtil.getFields(cls, 0);
boolean isGenerics = JavaFieldUtil.checkGenerics(fields);
int i = 0;
@ -164,6 +166,9 @@ public class JsonBuildHelper {
JavaClassValidateUtil.isIgnoreFieldTypes(subTypeName)) {
continue;
}
if ((responseFieldToUnderline && isResp) || (requestFieldToUnderline && !isResp)) {
fieldName = StringUtil.camelToUnderline(fieldName);
}
Map<String, String> tagsMap = DocUtil.getFieldTagsValue(field);
if (!isResp) {
if (tagsMap.containsKey(DocTags.IGNORE)) {

View File

@ -59,6 +59,8 @@ public class ParamsBuildHelper {
if (registryClasses.containsKey(className) && level > registryClasses.size()) {
return paramList;
}
boolean requestFieldToUnderline = projectBuilder.getApiConfig().isRequestFieldToUnderline();
boolean responseFieldToUnderline = projectBuilder.getApiConfig().isResponseFieldToUnderline();
// Registry class
registryClasses.put(className, className);
String simpleName = DocClassUtil.getSimpleName(className);
@ -99,6 +101,9 @@ public class ParamsBuildHelper {
JavaClassValidateUtil.isIgnoreFieldTypes(subTypeName)) {
continue;
}
if ((responseFieldToUnderline && isResp) || (requestFieldToUnderline && !isResp)) {
fieldName = StringUtil.camelToUnderline(fieldName);
}
String typeSimpleName = field.getType().getSimpleName();
String fieldGicName = field.getType().getGenericCanonicalName();
List<JavaAnnotation> javaAnnotations = field.getAnnotations();

View File

@ -153,6 +153,18 @@ public class ApiConfig {
*/
private boolean showAuthor = true;
/**
* convert request field to underline
* @since 1.8.7
*/
private boolean requestFieldToUnderline;
/**
* convert response field to underline
* @since 1.8.7
*/
private boolean responseFieldToUnderline;
public String getServerUrl() {
return serverUrl;
@ -325,4 +337,20 @@ public class ApiConfig {
public void setShowAuthor(boolean showAuthor) {
this.showAuthor = showAuthor;
}
public boolean isRequestFieldToUnderline() {
return requestFieldToUnderline;
}
public void setRequestFieldToUnderline(boolean requestFieldToUnderline) {
this.requestFieldToUnderline = requestFieldToUnderline;
}
public boolean isResponseFieldToUnderline() {
return responseFieldToUnderline;
}
public void setResponseFieldToUnderline(boolean responseFieldToUnderline) {
this.responseFieldToUnderline = responseFieldToUnderline;
}
}

View File

@ -193,6 +193,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
if (parameterList.size() < 1) {
return ApiRequestExample.builder().setUrl(apiMethodDoc.getUrl());
}
boolean requestFieldToUnderline = configBuilder.getApiConfig().isRequestFieldToUnderline();
Map<String, String> replacementMap = configBuilder.getReplaceClassMap();
Map<String, String> pathParamsMap = new LinkedHashMap<>();
Map<String, String> paramsComments = DocUtil.getParamsComments(method, DocTags.PARAM, null);
@ -239,6 +240,9 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
mockValue = DocUtil.getValByTypeAndFieldName(simpleTypeName, paramName, Boolean.TRUE);
}
}
if (requestFieldToUnderline) {
paramName = StringUtil.camelToUnderline(paramName);
}
List<JavaAnnotation> annotations = parameter.getAnnotations();
boolean paramAdded = false;
for (JavaAnnotation annotation : annotations) {
@ -395,6 +399,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
if (parameterList.size() < 1) {
return null;
}
boolean requestFieldToUnderline = builder.getApiConfig().isRequestFieldToUnderline();
List<String> springMvcRequestAnnotations = SpringMvcRequestAnnotationsEnum.listSpringMvcRequestAnnotations();
Set<String> jsonParamSet = this.jsonParamSet(parameterList);
List<ApiParam> paramList = new ArrayList<>();
@ -431,6 +436,9 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
+ paramName + "\" in method " + javaMethod.getName() + " from " + className);
}
String comment = this.paramCommentResolve(paramTagMap.get(paramName));
if (requestFieldToUnderline) {
paramName = StringUtil.camelToUnderline(paramName);
}
//file upload
if (typeName.contains(DocGlobalConstants.MULTIPART_FILE_FULLY)) {
ApiParam param = ApiParam.of().setField(paramName).setType("file")