Update Path match
This commit is contained in:
parent
777eab2fcd
commit
9b9451e41f
11
README_CN.md
11
README_CN.md
|
@ -50,9 +50,12 @@ smart-doc使用和测试可参考[smart-doc demo](https://gitee.com/devin-alan/a
|
|||
```
|
||||
# git clone https://gitee.com/devin-alan/api-doc-test.git
|
||||
```
|
||||
|
||||
你可以启动这个Spring Boot的项目,然后访问`http://localhost:8080/doc/api.html`来浏览smart-doc生成的接口文档。
|
||||
|
||||
maven多模块项目请参考
|
||||
```
|
||||
# git clone https://gitee.com/devin-alan/spring-boot-maven-multiple-module.git
|
||||
```
|
||||
### Add Maven Plugin
|
||||
|
||||
smart-doc官方目前已经开发完成[Maven插件](https://gitee.com/smart-doc-team/smart-doc-maven-plugin)
|
||||
|
@ -187,14 +190,16 @@ smart-doc官方目前已经开发完成[Maven插件](https://gitee.com/smart-doc
|
|||
"desc": "desc",//请求头描述信息
|
||||
"value":"token请求头的值",//不设置默认null
|
||||
"required": false,//是否必须
|
||||
"since": "-"//什么版本添加的改请求头
|
||||
"since": "-",//什么版本添加的改请求头
|
||||
"pathPatterns": "/app/test/**",//请看https://gitee.com/smart-doc-team/smart-doc/wikis/请求头高级配置?sort_id=4178978
|
||||
"excludePathPatterns":"/app/page/**"//请看https://gitee.com/smart-doc-team/smart-doc/wikis/请求头高级配置?sort_id=4178978
|
||||
},{
|
||||
"name": "appkey",//请求头
|
||||
"type": "string",//请求头类型
|
||||
"desc": "desc",//请求头描述信息
|
||||
"value":"appkey请求头的值",//不设置默认null
|
||||
"required": false,//是否必须
|
||||
"urlPatterns": "/test/add;/testConstants/1.0",//正则表达式过滤请求头,url匹配上才会添加该请求头,多个正则用分号隔开
|
||||
"pathPatterns": "/test/add,/testConstants/1.0",//正则表达式过滤请求头,url匹配上才会添加该请求头,多个正则用分号隔开
|
||||
"since": "-"//什么版本添加的改请求头
|
||||
}],
|
||||
"rpcApiDependencies":[{ // 项目开放的dubbo api接口模块依赖,配置后输出到文档方便使用者集成
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -67,7 +67,7 @@
|
|||
<dependency>
|
||||
<groupId>com.github.shalousun</groupId>
|
||||
<artifactId>common-util</artifactId>
|
||||
<version>2.0.6</version>
|
||||
<version>2.0.8</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
|
|
|
@ -38,7 +38,7 @@ import com.power.doc.model.postman.request.header.HeaderBean;
|
|||
import com.power.doc.template.IDocBuildTemplate;
|
||||
import com.power.doc.template.SpringBootDocBuildTemplate;
|
||||
import com.power.doc.utils.JsonUtil;
|
||||
import com.power.doc.utils.PathUtil;
|
||||
import com.power.doc.utils.DocPathUtil;
|
||||
import com.thoughtworks.qdox.JavaProjectBuilder;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
|
@ -131,7 +131,7 @@ public class PostmanJsonBuilder {
|
|||
private static UrlBean buildUrlBean(ApiMethodDoc apiMethodDoc) {
|
||||
UrlBean urlBean = new UrlBean();
|
||||
String url = Optional.ofNullable(apiMethodDoc.getRequestExample().getUrl()).orElse(apiMethodDoc.getUrl());
|
||||
urlBean.setRaw(PathUtil.toPostmanPath(url));
|
||||
urlBean.setRaw(DocPathUtil.toPostmanPath(url));
|
||||
try {
|
||||
URL javaUrl = new java.net.URL(apiMethodDoc.getServerUrl());
|
||||
if (javaUrl.getPort() == -1) {
|
||||
|
@ -154,7 +154,7 @@ public class PostmanJsonBuilder {
|
|||
urlBean.setPath(paths);
|
||||
} catch (MalformedURLException e) {
|
||||
}
|
||||
String shortUrl = PathUtil.toPostmanPath(apiMethodDoc.getPath());
|
||||
String shortUrl = DocPathUtil.toPostmanPath(apiMethodDoc.getPath());
|
||||
String[] paths = shortUrl.split("/");
|
||||
List<String> pathList = new ArrayList<>();
|
||||
if (CollectionUtil.isNotEmpty(urlBean.getPath()) && shortUrl.indexOf(urlBean.getPath().get(0)) < 0) {
|
||||
|
|
|
@ -66,11 +66,17 @@ public class ApiReqHeader {
|
|||
*/
|
||||
private String since = "-";
|
||||
|
||||
/**
|
||||
* @since 2.2.2
|
||||
* Regular expression match request header
|
||||
*/
|
||||
private String pathPatterns;
|
||||
|
||||
/**
|
||||
* @since 2.2.2
|
||||
* Regular expression ignore request header
|
||||
*/
|
||||
private String urlPatterns;
|
||||
private String excludePathPatterns;
|
||||
|
||||
public static ApiReqHeader builder() {
|
||||
return new ApiReqHeader();
|
||||
|
@ -130,12 +136,21 @@ public class ApiReqHeader {
|
|||
return this;
|
||||
}
|
||||
|
||||
public String getUrlPatterns() {
|
||||
return urlPatterns;
|
||||
public String getPathPatterns() {
|
||||
return pathPatterns;
|
||||
}
|
||||
|
||||
public ApiReqHeader setUrlPatterns(String urlPatterns) {
|
||||
this.urlPatterns = urlPatterns;
|
||||
public ApiReqHeader setPathPatterns(String pathPatterns) {
|
||||
this.pathPatterns = pathPatterns;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getExcludePathPatterns() {
|
||||
return excludePathPatterns;
|
||||
}
|
||||
|
||||
public ApiReqHeader setExcludePathPatterns(String excludePathPatterns) {
|
||||
this.excludePathPatterns = excludePathPatterns;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -167,8 +182,10 @@ public class ApiReqHeader {
|
|||
.append(required);
|
||||
sb.append(",\"since\":\"")
|
||||
.append(since).append('\"');
|
||||
sb.append(",\"urlPatterns\":\"")
|
||||
.append(urlPatterns).append('\"');
|
||||
sb.append(",\"pathPatterns\":\"")
|
||||
.append(pathPatterns).append('\"');
|
||||
sb.append(",\"excludePathPatterns\":\"")
|
||||
.append(excludePathPatterns).append('\"');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
|
|
|
@ -225,10 +225,14 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
|
|||
allApiReqHeaders = apiReqHeaders;
|
||||
}
|
||||
allApiReqHeaders.removeIf(apiReqHeader -> {
|
||||
if (StringUtil.isNotEmpty(apiReqHeader.getUrlPatterns())) {
|
||||
return !PathUtil.isMatchUrl(requestMapping.getShortUrl(), apiReqHeader.getUrlPatterns());
|
||||
}
|
||||
if (StringUtil.isEmpty(apiReqHeader.getPathPatterns())
|
||||
&& StringUtil.isEmpty(apiReqHeader.getExcludePathPatterns())) {
|
||||
return false;
|
||||
} else {
|
||||
boolean flag = DocPathUtil.matches(requestMapping.getShortUrl(), apiReqHeader.getPathPatterns()
|
||||
, apiReqHeader.getExcludePathPatterns());
|
||||
return !flag;
|
||||
}
|
||||
});
|
||||
//reduce create in template
|
||||
apiMethodDoc.setHeaders(this.createDocRenderHeaders(allApiReqHeaders, apiConfig.isAdoc()));
|
||||
|
@ -805,7 +809,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
|
|||
List<ApiParam> queryParams = new ArrayList<>();
|
||||
List<ApiParam> bodyParams = new ArrayList<>();
|
||||
for (ApiParam param : paramList) {
|
||||
param.setValue(StringUtil.removeQuotes(param.getValue()));
|
||||
param.setValue(StringUtil.removeDoubleQuotes(param.getValue()));
|
||||
if (param.isPathParam()) {
|
||||
param.setId(pathParams.size() + 1);
|
||||
pathParams.add(param);
|
||||
|
|
|
@ -22,14 +22,15 @@
|
|||
*/
|
||||
package com.power.doc.utils;
|
||||
|
||||
import com.power.common.util.PathUtil;
|
||||
import com.power.common.util.StringUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class PathUtil {
|
||||
public class DocPathUtil {
|
||||
|
||||
/**
|
||||
* Get the java class name
|
||||
|
@ -65,31 +66,22 @@ public class PathUtil {
|
|||
}
|
||||
|
||||
/**
|
||||
* match
|
||||
* @param url url
|
||||
* @param urlPatterns patterns
|
||||
* @return true if match
|
||||
* Determine a match for the given lookup path.
|
||||
*
|
||||
* @param lookupPath the request path
|
||||
* @param includePatterns the path patterns to map (empty for matching to all paths)
|
||||
* @param excludePatterns the path patterns to exclude (empty for no specific excludes)
|
||||
* @return {@code true} if matched the request path
|
||||
*/
|
||||
public static boolean isMatchUrl(String url , String urlPatterns){
|
||||
List<String> urlPatternList;
|
||||
if (StringUtil.isNotEmpty(urlPatterns)) {
|
||||
urlPatternList = Arrays.asList(urlPatterns.split(";", 0));
|
||||
} else {
|
||||
return false;
|
||||
public static boolean matches(String lookupPath, String includePatterns, String excludePatterns) {
|
||||
List<String> includePatternList = null;
|
||||
if (StringUtil.isNotEmpty(includePatterns)) {
|
||||
includePatternList = Arrays.asList(includePatterns.split(",", 0));
|
||||
}
|
||||
for (String str : urlPatternList) {
|
||||
if (str.endsWith("/*")) {
|
||||
String name = str.substring(0, str.length() - 1);
|
||||
if (url.contains(name)) {
|
||||
return true;
|
||||
List<String> excludePatternList = null;
|
||||
if (StringUtil.isNotEmpty(excludePatterns)) {
|
||||
excludePatternList = Arrays.asList(excludePatterns.split(",", 0));
|
||||
}
|
||||
} else {
|
||||
Pattern pattern = Pattern.compile(str);
|
||||
if (pattern.matcher(url).matches()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return PathUtil.matches(lookupPath,includePatternList,excludePatternList);
|
||||
}
|
||||
}
|
|
@ -41,7 +41,7 @@ public class JsonUtil {
|
|||
public static String toPrettyFormat(String jsonString) {
|
||||
try {
|
||||
JsonElement jsonElement = JsonParser.parseString(jsonString);
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
|
||||
String prettyJson = gson.toJson(jsonElement);
|
||||
return prettyJson;
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package com.power.doc.util;
|
||||
|
||||
import com.power.doc.utils.DocPathUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author yu 2021/6/27.
|
||||
*/
|
||||
public class DocPathUtilTest {
|
||||
|
||||
@Test
|
||||
public void testMatches() {
|
||||
String pattern = "/app/page/**";
|
||||
String path = "/app/page/{pageIndex}/{pageSize}/{ag}";
|
||||
System.out.println(DocPathUtil.matches(path,null,pattern));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.power.doc.util;
|
||||
|
||||
import com.power.doc.utils.JsonUtil;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author yu 2021/6/27.
|
||||
*/
|
||||
public class JsonUtilTest {
|
||||
|
||||
@Test
|
||||
public void toPrettyFormat() {
|
||||
String json = "{\"MAX_SPEED\":210,\"gender\":0,\"simpleEnum\":\"RED\",\"username\":\"梓晨.田\",\"password\":\"slujk7\",\"nickName\":\"select * from table where field = 'value'\",\"mobile\":\"17658638153\"}";
|
||||
System.out.println(JsonUtil.toPrettyFormat(json));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue