support api sort

This commit is contained in:
oppofind 2020-09-04 23:41:35 +08:00
parent a1ff21969e
commit 64a913b796
8 changed files with 109 additions and 80 deletions

View File

@ -1,5 +1,12 @@
## smart-doc版本
版本小于1.0都属于试用正式1.0起始发布将会等到文中提到的问题解决后才发布。
#### 版本号1.9.4
- 更新日期: 2020-09-06
- 更新内容:
1. 添加order tag支持对api做排序。
2. 优化一些重复的代码。
3. 修改基础url中使用常量出现空格的问题。
4. 添加生成yapi文件的功能。
#### 版本号1.9.3
- 更新日期: 2020-08-30
- 更新内容:

View File

@ -28,9 +28,9 @@ $\color{red}{我因不将就而诞生,用了无数个日日夜夜来成长,
- 支持maven、gradle插件式轻松集成。
- 支持Apache Dubbo RPC接口文档生成。
## Getting started
smart-doc使用和测试可参考[smart-doc demo](https://gitee.com/smart-doc-team/api-doc-test.git)。
smart-doc使用和测试可参考[smart-doc demo](https://gitee.com/sunyurepository/api-doc-test.git)。
```
# git clone https://gitee.com/smart-doc-team/api-doc-test.git
# git clone https://gitee.com/sunyurepository/api-doc-test.git
```
你可以启动这个Spring Boot的项目然后访问`http://localhost:8080/doc/api.html`来浏览smart-doc生成的接口文档。
### Add Maven plugin
@ -76,7 +76,7 @@ smart-doc官方目前已经开发完成[maven插件](https://gitee.com/smart-doc
**最小配置单元:**
```
{
"outPath": "D://md2" //指定文档的输出路径
"outPath": "D://md2" //指定文档的输出路径,相对路径时请用./开头eg:./src/main/resources/static/doc
}
```
仅仅需要上面一行配置就能启动smart-doc-maven-plugin插件根据自己项目情况更多详细的配置参考下面。
@ -179,6 +179,8 @@ mvn -Dfile.encoding=UTF-8 smart-doc:markdown
mvn -Dfile.encoding=UTF-8 smart-doc:adoc
//生成postman json数据
mvn -Dfile.encoding=UTF-8 smart-doc:postman
// 生成 Open Api 3.0+,Since smart-doc-maven-plugin 1.1.5
mvn -Dfile.encoding = UTF-8 smart-doc:openapi
// Apache Dubbo Rpc文档
// Generate html

View File

@ -1,57 +1,62 @@
package com.power.doc.constants;
/**
* @author yu 2019/9/13.
*/
public interface DocTags {
/**
* java since tag
*/
String SINCE = "since";
/**
* java required tag
*/
String REQUIRED = "required";
/**
* java param tag
*/
String PARAM = "param";
/**
* java apiNote tag for method detail
*/
String API_NOTE = "apiNote";
/**
* java author tag for method author
*/
String AUTHOR = "author";
/**
* java verion tag
*/
String VERSION = "version";
/**
* custom ignore tag
*/
String IGNORE = "ignore";
/**
* custom @mock tag
*/
String MOCK = "mock";
/**
* custom @dubbo tag
*/
String DUBBO = "dubbo";
/**
* custom @api tag
*/
String REST_API = "restApi";
}
package com.power.doc.constants;
/**
* @author yu 2019/9/13.
*/
public interface DocTags {
/**
* java since tag
*/
String SINCE = "since";
/**
* java required tag
*/
String REQUIRED = "required";
/**
* java param tag
*/
String PARAM = "param";
/**
* java apiNote tag for method detail
*/
String API_NOTE = "apiNote";
/**
* java author tag for method author
*/
String AUTHOR = "author";
/**
* java verion tag
*/
String VERSION = "version";
/**
* custom ignore tag
*/
String IGNORE = "ignore";
/**
* custom @mock tag
*/
String MOCK = "mock";
/**
* custom @dubbo tag
*/
String DUBBO = "dubbo";
/**
* custom @api tag
*/
String REST_API = "restApi";
/**
* custom @order tag
*/
String ORDER = "order";
}

View File

@ -22,10 +22,7 @@
*/
package com.power.doc.template;
import com.power.common.util.JsonFormatUtil;
import com.power.common.util.RandomUtil;
import com.power.common.util.StringUtil;
import com.power.common.util.UrlUtil;
import com.power.common.util.*;
import com.power.doc.builder.ProjectDocConfigBuilder;
import com.power.doc.constants.*;
import com.power.doc.handler.SpringMVCRequestHeaderHandler;
@ -41,6 +38,7 @@ import com.thoughtworks.qdox.model.*;
import com.thoughtworks.qdox.model.expression.AnnotationValue;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -55,6 +53,11 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
private List<ApiReqHeader> headers;
/**
* api index
*/
private AtomicInteger atomicInteger = new AtomicInteger(1);
@Override
public List<ApiDoc> getApiData(ProjectDocConfigBuilder projectBuilder) {
ApiConfig apiConfig = projectBuilder.getApiConfig();
@ -62,26 +65,38 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
List<ApiDoc> apiDocList = new ArrayList<>();
int order = 0;
Collection<JavaClass> classes = projectBuilder.getJavaProjectBuilder().getClasses();
boolean setCustomOrder = false;
for (JavaClass cls : classes) {
String ignoreTag = JavaClassUtil.getClassTagsValue(cls, DocTags.IGNORE, Boolean.FALSE);
if (!checkController(cls) || StringUtil.isNotEmpty(ignoreTag)) {
continue;
}
if (StringUtil.isNotEmpty(apiConfig.getPackageFilters())) {
if (DocUtil.isMatch(apiConfig.getPackageFilters(), cls.getCanonicalName())) {
order++;
List<ApiMethodDoc> apiMethodDocs = buildControllerMethod(cls, apiConfig, projectBuilder);
this.handleApiDoc(cls, apiDocList, apiMethodDocs, order, apiConfig.isMd5EncryptedHtmlName());
if (!DocUtil.isMatch(apiConfig.getPackageFilters(), cls.getCanonicalName())) {
continue;
}
} else {
order++;
List<ApiMethodDoc> apiMethodDocs = buildControllerMethod(cls, apiConfig, projectBuilder);
this.handleApiDoc(cls, apiDocList, apiMethodDocs, order, apiConfig.isMd5EncryptedHtmlName());
}
String strOrder = JavaClassUtil.getClassTagsValue(cls, DocTags.ORDER, Boolean.TRUE);
order++;
if (ValidateUtil.isNonnegativeInteger(strOrder)) {
setCustomOrder = true;
order = Integer.parseInt(strOrder);
}
List<ApiMethodDoc> apiMethodDocs = buildControllerMethod(cls, apiConfig, projectBuilder);
this.handleApiDoc(cls, apiDocList, apiMethodDocs, order, apiConfig.isMd5EncryptedHtmlName());
}
// sort
if (apiConfig.isSortByTitle()) {
Collections.sort(apiDocList);
} else if (setCustomOrder) {
// while set custom oder
List<ApiDoc> sortedApiDoc = apiDocList.stream()
.sorted(Comparator.comparing(ApiDoc::getOrder))
.map(p -> {
p.setOrder(atomicInteger.getAndAdd(1));
return p;
}).collect(Collectors.toList());
return sortedApiDoc;
}
return apiDocList;
}

File diff suppressed because one or more lines are too long

View File

@ -15,7 +15,7 @@
<footer class="page-footer">
<span class="copyright">Generated by smart-doc at ${createTime}</span>
<span class="footer-modification">Suggestions, contact, support and error reporting on
<a href="https://gitee.com/sunyurepository/smart-doc" target="_blank">Gitee</a> or
<a href="https://gitee.com/smart-doc-team/smart-doc" target="_blank">Gitee</a> or
<a href="https://github.com/smart-doc-group/smart-doc" target="_blank">Github</a>
</span>
</footer>

File diff suppressed because one or more lines are too long

View File

@ -124,7 +124,7 @@
<footer class="page-footer">
<span class="copyright">Generated by smart-doc at ${createTime}</span>
<span class="footer-modification">Suggestions, contact, support and error reporting on
<a href="https://gitee.com/sunyurepository/smart-doc" target="_blank">Gitee</a> or
<a href="https://gitee.com/smart-doc-team/smart-doc" target="_blank">Gitee</a> or
<a href="https://github.com/smart-doc-group/smart-doc" target="_blank">Github</a>
</span>
</footer>