From 9b9451e41fe978091d40c5e544eb9e4ad6058ed1 Mon Sep 17 00:00:00 2001 From: shalousun Date: Sun, 27 Jun 2021 22:55:21 +0800 Subject: [PATCH] Update Path match --- README_CN.md | 11 +++-- pom.xml | 2 +- .../power/doc/builder/PostmanJsonBuilder.java | 6 +-- .../com/power/doc/model/ApiReqHeader.java | 31 ++++++++++--- .../template/SpringBootDocBuildTemplate.java | 12 +++-- .../utils/{PathUtil.java => DocPathUtil.java} | 44 ++++++++----------- .../java/com/power/doc/utils/JsonUtil.java | 2 +- .../com/power/doc/util/DocPathUtilTest.java | 17 +++++++ .../java/com/power/doc/util/JsonUtilTest.java | 16 +++++++ 9 files changed, 96 insertions(+), 45 deletions(-) rename src/main/java/com/power/doc/utils/{PathUtil.java => DocPathUtil.java} (66%) create mode 100644 src/test/java/com/power/doc/util/DocPathUtilTest.java create mode 100644 src/test/java/com/power/doc/util/JsonUtilTest.java diff --git a/README_CN.md b/README_CN.md index b7d0825..c905a30 100644 --- a/README_CN.md +++ b/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接口模块依赖,配置后输出到文档方便使用者集成 diff --git a/pom.xml b/pom.xml index 524c165..8e99531 100644 --- a/pom.xml +++ b/pom.xml @@ -67,7 +67,7 @@ com.github.shalousun common-util - 2.0.6 + 2.0.8 com.google.code.gson diff --git a/src/main/java/com/power/doc/builder/PostmanJsonBuilder.java b/src/main/java/com/power/doc/builder/PostmanJsonBuilder.java index 5a40e48..005a15b 100644 --- a/src/main/java/com/power/doc/builder/PostmanJsonBuilder.java +++ b/src/main/java/com/power/doc/builder/PostmanJsonBuilder.java @@ -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 pathList = new ArrayList<>(); if (CollectionUtil.isNotEmpty(urlBean.getPath()) && shortUrl.indexOf(urlBean.getPath().get(0)) < 0) { diff --git a/src/main/java/com/power/doc/model/ApiReqHeader.java b/src/main/java/com/power/doc/model/ApiReqHeader.java index 5960d0c..4ba464b 100644 --- a/src/main/java/com/power/doc/model/ApiReqHeader.java +++ b/src/main/java/com/power/doc/model/ApiReqHeader.java @@ -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(); } diff --git a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java index 1f5bd94..d72c61d 100644 --- a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java +++ b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java @@ -225,10 +225,14 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { 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; } - return false; }); //reduce create in template apiMethodDoc.setHeaders(this.createDocRenderHeaders(allApiReqHeaders, apiConfig.isAdoc())); @@ -805,7 +809,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { List queryParams = new ArrayList<>(); List 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); diff --git a/src/main/java/com/power/doc/utils/PathUtil.java b/src/main/java/com/power/doc/utils/DocPathUtil.java similarity index 66% rename from src/main/java/com/power/doc/utils/PathUtil.java rename to src/main/java/com/power/doc/utils/DocPathUtil.java index 397b6a5..cf4ea26 100644 --- a/src/main/java/com/power/doc/utils/PathUtil.java +++ b/src/main/java/com/power/doc/utils/DocPathUtil.java @@ -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 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 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; - } - } else { - Pattern pattern = Pattern.compile(str); - if (pattern.matcher(url).matches()) { - return true; - } - } + List excludePatternList = null; + if (StringUtil.isNotEmpty(excludePatterns)) { + excludePatternList = Arrays.asList(excludePatterns.split(",", 0)); } - return false; + return PathUtil.matches(lookupPath,includePatternList,excludePatternList); } } diff --git a/src/main/java/com/power/doc/utils/JsonUtil.java b/src/main/java/com/power/doc/utils/JsonUtil.java index 0ffe648..6e1e1df 100644 --- a/src/main/java/com/power/doc/utils/JsonUtil.java +++ b/src/main/java/com/power/doc/utils/JsonUtil.java @@ -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) { diff --git a/src/test/java/com/power/doc/util/DocPathUtilTest.java b/src/test/java/com/power/doc/util/DocPathUtilTest.java new file mode 100644 index 0000000..6542c43 --- /dev/null +++ b/src/test/java/com/power/doc/util/DocPathUtilTest.java @@ -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)); + } +} diff --git a/src/test/java/com/power/doc/util/JsonUtilTest.java b/src/test/java/com/power/doc/util/JsonUtilTest.java new file mode 100644 index 0000000..4dfc79a --- /dev/null +++ b/src/test/java/com/power/doc/util/JsonUtilTest.java @@ -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)); + } +}