From 219f16c5f52282234505077b9f3abb47f04ac124 Mon Sep 17 00:00:00 2001 From: shalousun Date: Sat, 26 Jun 2021 12:27:12 +0800 Subject: [PATCH] Fix gitee #I3XSE5 --- README_CN.md | 6 ++-- .../com/power/doc/builder/OpenApiBuilder.java | 6 ++-- .../power/doc/builder/PostmanJsonBuilder.java | 6 ++-- .../com/power/doc/helper/JsonBuildHelper.java | 2 +- .../template/SpringBootDocBuildTemplate.java | 6 ++-- .../java/com/power/doc/utils/JsonUtil.java | 32 +++++++++++++++++++ .../java/com/power/doc/utils/TornaUtil.java | 5 --- 7 files changed, 43 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/power/doc/utils/JsonUtil.java diff --git a/README_CN.md b/README_CN.md index ab97634..6f0deda 100644 --- a/README_CN.md +++ b/README_CN.md @@ -202,7 +202,7 @@ smart-doc官方目前已经开发完成[Maven插件](https://gitee.com/smart-doc "apiConstants": [{//从1.8.9开始配置自己的常量类,smart-doc在解析到常量时自动替换为具体的值,非必须 "constantsClassName": "com.power.doc.constants.RequestParamConstant" }], - "responseBodyAdvice":{ //自smart-doc 1.9.8起,ResponseBodyAdvice统一返回设置,可用ignoreResponseBodyAdvice tag来忽略 + "responseBodyAdvice":{ //自smart-doc 1.9.8起,非必须项,ResponseBodyAdvice统一返回设置(不要随便配置根据项目的技术来配置),可用ignoreResponseBodyAdvice tag来忽略 "className":"com.power.common.model.CommonResult" //通用响应体 }, "requestBodyAdvice":{ ////自smart-doc 2.1.4 起,支持设置RequestBodyAdvice统一请求包装类,非必须 @@ -211,8 +211,8 @@ smart-doc官方目前已经开发完成[Maven插件](https://gitee.com/smart-doc } ``` ->上面的JSON配置实例中只有"outPath"是必填项。其他额外配置项请根据实际情况来配置,胡乱复制上面的全配置到自己的项目中 -可能引起使用错误 +>上面的JSON配置实例中只有"outPath"是必填项。其他额外配置项请根据实际情况来配置,请不要胡乱复制上面的全配置到自己的项目中 +可能引起使用错误。 **注意:** 对于老用户完全可以通过`Fastjson`或者是`Gson`库将`ApiConfig`转化成JSON配置。 diff --git a/src/main/java/com/power/doc/builder/OpenApiBuilder.java b/src/main/java/com/power/doc/builder/OpenApiBuilder.java index 561170c..240c251 100644 --- a/src/main/java/com/power/doc/builder/OpenApiBuilder.java +++ b/src/main/java/com/power/doc/builder/OpenApiBuilder.java @@ -22,8 +22,6 @@ */ package com.power.doc.builder; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.power.common.util.CollectionUtil; import com.power.common.util.FileUtil; import com.power.common.util.StringUtil; @@ -32,6 +30,7 @@ import com.power.doc.constants.Methods; import com.power.doc.model.*; import com.power.doc.template.SpringBootDocBuildTemplate; import com.power.doc.utils.DocUtil; +import com.power.doc.utils.JsonUtil; import com.thoughtworks.qdox.JavaProjectBuilder; import java.util.*; @@ -90,8 +89,7 @@ public class OpenApiBuilder { String filePath = config.getOutPath(); filePath = filePath + DocGlobalConstants.OPEN_API_JSON; - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - String data = gson.toJson(json); + String data = JsonUtil.toPrettyJson(json); FileUtil.nioWriteFile(data, filePath); } diff --git a/src/main/java/com/power/doc/builder/PostmanJsonBuilder.java b/src/main/java/com/power/doc/builder/PostmanJsonBuilder.java index c5969bb..5a40e48 100644 --- a/src/main/java/com/power/doc/builder/PostmanJsonBuilder.java +++ b/src/main/java/com/power/doc/builder/PostmanJsonBuilder.java @@ -22,8 +22,6 @@ */ package com.power.doc.builder; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.power.common.util.CollectionUtil; import com.power.common.util.FileUtil; import com.power.common.util.StringUtil; @@ -39,6 +37,7 @@ import com.power.doc.model.postman.request.body.BodyBean; 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.thoughtworks.qdox.JavaProjectBuilder; @@ -255,8 +254,7 @@ public class PostmanJsonBuilder { requestItem.setItem(itemBeans); String filePath = config.getOutPath(); filePath = filePath + DocGlobalConstants.POSTMAN_JSON; - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - String data = gson.toJson(requestItem); + String data = JsonUtil.toPrettyJson(requestItem); FileUtil.nioWriteFile(data, filePath); } diff --git a/src/main/java/com/power/doc/helper/JsonBuildHelper.java b/src/main/java/com/power/doc/helper/JsonBuildHelper.java index 251b26c..46a45c6 100644 --- a/src/main/java/com/power/doc/helper/JsonBuildHelper.java +++ b/src/main/java/com/power/doc/helper/JsonBuildHelper.java @@ -91,7 +91,7 @@ public class JsonBuildHelper { } return StringUtil.removeQuotes(DocUtil.jsonValueByType(typeName)); } - return JsonFormatUtil.formatJson(buildJson(typeName, returnType, Boolean.TRUE, 0, new HashMap<>(), builder)); + return JsonUtil.toPrettyFormat(buildJson(typeName, returnType, Boolean.TRUE, 0, new HashMap<>(), builder)); } /** diff --git a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java index cb441cb..5339a45 100644 --- a/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java +++ b/src/main/java/com/power/doc/template/SpringBootDocBuildTemplate.java @@ -188,7 +188,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { Map authorMap = DocUtil.getParamsComments(method, DocTags.AUTHOR, cls.getName()); String authorValue = String.join(", ", new ArrayList<>(authorMap.keySet())); if (apiConfig.isShowAuthor() && StringUtil.isNotEmpty(authorValue)) { - apiMethodDoc.setAuthor(JsonFormatUtil.formatJson(authorValue)); + apiMethodDoc.setAuthor(JsonUtil.toPrettyFormat(authorValue)); } if (apiConfig.isShowAuthor() && StringUtil.isEmpty(authorValue)) { apiMethodDoc.setAuthor(classAuthor); @@ -389,10 +389,10 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate { .append("\":") .append(DocUtil.handleJsonStr(mockValue)) .append("}"); - requestExample.setJsonBody(JsonFormatUtil.formatJson(builder.toString())).setJson(true); + requestExample.setJsonBody(JsonUtil.toPrettyFormat(builder.toString())).setJson(true); } else { String json = JsonBuildHelper.buildJson(typeName, gicTypeName, Boolean.FALSE, 0, new HashMap<>(), configBuilder); - requestExample.setJsonBody(JsonFormatUtil.formatJson(json)).setJson(true); + requestExample.setJsonBody(JsonUtil.toPrettyFormat(json)).setJson(true); } paramAdded = true; } else if (SpringMvcAnnotations.PATH_VARIABLE.contains(annotationName)) { diff --git a/src/main/java/com/power/doc/utils/JsonUtil.java b/src/main/java/com/power/doc/utils/JsonUtil.java new file mode 100644 index 0000000..3ea94ac --- /dev/null +++ b/src/main/java/com/power/doc/utils/JsonUtil.java @@ -0,0 +1,32 @@ +package com.power.doc.utils; + +import com.google.gson.*; + +/** + * @author yu 2021/6/26. + */ +public class JsonUtil { + + /** + * Convert a JSON string to pretty print + * @param jsonString json string + * @return Format json string + */ + public static String toPrettyFormat(String jsonString) { + JsonObject json = JsonParser.parseString(jsonString).getAsJsonObject(); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + String prettyJson = gson.toJson(json); + return prettyJson; + } + + /** + * Convert a JSON to String and pretty print + * @param src Json + * @return Format json string + */ + public static String toPrettyJson(Object src) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + String prettyJson = gson.toJson(src); + return prettyJson; + } +} diff --git a/src/main/java/com/power/doc/utils/TornaUtil.java b/src/main/java/com/power/doc/utils/TornaUtil.java index dfd1025..f170de2 100644 --- a/src/main/java/com/power/doc/utils/TornaUtil.java +++ b/src/main/java/com/power/doc/utils/TornaUtil.java @@ -2,24 +2,19 @@ package com.power.doc.utils; import com.google.gson.JsonElement; import com.google.gson.JsonParser; -import com.google.gson.internal.$Gson$Types; import com.power.common.model.EnumDictionary; import com.power.common.util.CollectionUtil; import com.power.common.util.StringUtil; -import com.power.doc.builder.BaseDocBuilderTemplate; import com.power.doc.constants.TornaConstants; import com.power.doc.model.*; import com.power.doc.model.rpc.RpcApiDependency; import com.power.doc.model.torna.*; -import okhttp3.internal.http2.ErrorCode; import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import java.util.List; import java.util.Map; -import static com.power.doc.constants.TornaConstants.PUSH; - /** * @author xingzi 2021/4/28 16:15 **/