You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/06/04 02:58:13 UTC

[incubator-shenyu] branch master updated: Remove fastjson (#3481)

This is an automated email from the ASF dual-hosted git repository.

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new dd4bcf151 Remove fastjson (#3481)
dd4bcf151 is described below

commit dd4bcf151413cedbe9d623f362d6117d6e44567d
Author: renzhuyan <40...@qq.com>
AuthorDate: Fri Jun 3 21:58:08 2022 -0500

    Remove fastjson (#3481)
---
 shenyu-admin/pom.xml                               |   7 --
 .../apache/shenyu/admin/model/bean/DocInfo.java    |   4 +-
 .../shenyu/admin/service/manager/DocParser.java    |   4 +-
 .../admin/service/manager/impl/DocManagerImpl.java |  10 +-
 .../service/manager/impl/SwaggerDocParser.java     | 112 +++++++++++----------
 5 files changed, 65 insertions(+), 72 deletions(-)

diff --git a/shenyu-admin/pom.xml b/shenyu-admin/pom.xml
index 41bb7f751..872b1bdb5 100644
--- a/shenyu-admin/pom.xml
+++ b/shenyu-admin/pom.xml
@@ -26,7 +26,6 @@
     <artifactId>shenyu-admin</artifactId>
 
     <properties>
-        <fastjson.version>1.2.80</fastjson.version>
         <commons-io.version>2.11.0</commons-io.version>
     </properties>
 
@@ -248,12 +247,6 @@
             <artifactId>okhttp</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>com.alibaba</groupId>
-            <artifactId>fastjson</artifactId>
-            <version>${fastjson.version}</version>
-        </dependency>
-
         <dependency>
             <groupId>commons-io</groupId>
             <artifactId>commons-io</artifactId>
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/bean/DocInfo.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/bean/DocInfo.java
index 95d4da562..8470b4b5b 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/bean/DocInfo.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/bean/DocInfo.java
@@ -17,7 +17,6 @@
 
 package org.apache.shenyu.admin.model.bean;
 
-import com.alibaba.fastjson.annotation.JSONField;
 import java.util.List;
 
 /**
@@ -27,8 +26,7 @@ public class DocInfo {
 
     private String title;
 
-    @JSONField(serialize = false)
-    private String clusterName;
+    private transient String clusterName;
 
     private List<DocModule> docModuleList;
 
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/DocParser.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/DocParser.java
index fae7d3988..1aed7a863 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/DocParser.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/DocParser.java
@@ -17,7 +17,7 @@
 
 package org.apache.shenyu.admin.service.manager;
 
-import com.alibaba.fastjson.JSONObject;
+import com.google.gson.JsonObject;
 import org.apache.shenyu.admin.model.bean.DocInfo;
 
 /**
@@ -31,5 +31,5 @@ public interface DocParser {
      * @param docRoot docRoot
      * @return DocInfo DocInfo
      */
-    DocInfo parseJson(JSONObject docRoot);
+    DocInfo parseJson(JsonObject docRoot);
 }
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/DocManagerImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/DocManagerImpl.java
index ae1e70d9c..d84bc839e 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/DocManagerImpl.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/DocManagerImpl.java
@@ -17,9 +17,6 @@
 
 package org.apache.shenyu.admin.service.manager.impl;
 
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
-import com.alibaba.fastjson.parser.Feature;
 import java.nio.charset.StandardCharsets;
 import java.util.Collection;
 import java.util.HashMap;
@@ -28,12 +25,15 @@ import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.function.Consumer;
+
+import com.google.gson.JsonObject;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.shenyu.admin.model.bean.DocInfo;
 import org.apache.shenyu.admin.model.bean.DocItem;
 import org.apache.shenyu.admin.model.bean.DocModule;
 import org.apache.shenyu.admin.service.manager.DocManager;
 import org.apache.shenyu.admin.service.manager.DocParser;
+import org.apache.shenyu.common.utils.GsonUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
@@ -94,8 +94,8 @@ public class DocManagerImpl implements DocManager {
 
     private DocInfo getDocInfo(final String clusterName, final String docInfoJson) {
         try {
-            JSONObject docRoot = JSON.parseObject(docInfoJson, Feature.OrderedField, Feature.DisableCircularReferenceDetect);
-            docRoot.put("basePath", "/" + clusterName);
+            JsonObject docRoot = GsonUtils.getInstance().fromJson(docInfoJson, JsonObject.class);
+            docRoot.addProperty("basePath", "/" + clusterName);
             DocInfo docInfo = SWAGGER_DOC_PARSER.parseJson(docRoot);
             docInfo.setClusterName(clusterName);
             return docInfo;
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/SwaggerDocParser.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/SwaggerDocParser.java
index aaf3770ce..e53df5c9d 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/SwaggerDocParser.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/SwaggerDocParser.java
@@ -17,9 +17,6 @@
 
 package org.apache.shenyu.admin.service.manager.impl;
 
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
 import com.google.common.collect.Sets;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -32,6 +29,10 @@ import java.util.Optional;
 import java.util.Set;
 import java.util.UUID;
 import java.util.stream.Collectors;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+import com.google.gson.reflect.TypeToken;
 import org.apache.commons.lang3.math.NumberUtils;
 import org.apache.shenyu.admin.model.bean.CustomCode;
 import org.apache.shenyu.admin.model.bean.DocInfo;
@@ -39,6 +40,7 @@ import org.apache.shenyu.admin.model.bean.DocItem;
 import org.apache.shenyu.admin.model.bean.DocModule;
 import org.apache.shenyu.admin.model.bean.DocParameter;
 import org.apache.shenyu.admin.service.manager.DocParser;
+import org.apache.shenyu.common.utils.GsonUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.util.CollectionUtils;
 
@@ -54,26 +56,26 @@ public class SwaggerDocParser implements DocParser {
      * @return DocInfo
      */
     @Override
-    public DocInfo parseJson(final JSONObject docRoot) {
-        final String basePath = docRoot.getString("basePath");
-        final String title = Optional.ofNullable(docRoot.getJSONObject("info")).map(jsonObject -> jsonObject.getString("title")).orElse(basePath);
+    public DocInfo parseJson(final JsonObject docRoot) {
+        final String basePath = docRoot.get("basePath").getAsString();
+        final String title = Optional.ofNullable(docRoot.getAsJsonObject("info")).map(jsonObject -> jsonObject.get("title").getAsString()).orElse(basePath);
         final List<DocItem> docItems = new ArrayList<>();
 
-        JSONObject paths = docRoot.getJSONObject("paths");
+        JsonObject paths = docRoot.getAsJsonObject("paths");
         if (paths == null) {
-            paths = new JSONObject();
+            paths = new JsonObject();
         }
         Set<String> pathNameSet = paths.keySet();
         for (String apiPath : pathNameSet) {
-            JSONObject pathInfo = paths.getJSONObject(apiPath);
+            JsonObject pathInfo = paths.getAsJsonObject(apiPath);
             // key: get,post,head...
             Collection<String> httpMethodList = getHttpMethods(pathInfo);
             Optional<String> first = httpMethodList.stream().findFirst();
             if (first.isPresent()) {
                 String method = first.get();
-                JSONObject docInfo = pathInfo.getJSONObject(method);
-                docInfo.putIfAbsent("real_req_path", apiPath);
-                docInfo.put("basePath", basePath);
+                JsonObject docInfo = pathInfo.getAsJsonObject(method);
+                docInfo.addProperty("real_req_path", apiPath);
+                docInfo.addProperty("basePath", basePath);
                 DocItem docItem = buildDocItem(docInfo, docRoot);
                 if (Objects.isNull(docItem)) {
                     continue;
@@ -119,7 +121,7 @@ public class SwaggerDocParser implements DocParser {
         return docItemList.get(0).getModuleOrder();
     }
 
-    protected Collection<String> getHttpMethods(final JSONObject pathInfo) {
+    protected Collection<String> getHttpMethods(final JsonObject pathInfo) {
         // key: get,post,head...
         List<String> retList;
         Set<String> httpMethodList = pathInfo.keySet();
@@ -136,24 +138,24 @@ public class SwaggerDocParser implements DocParser {
         return retList;
     }
 
-    protected DocItem buildDocItem(final JSONObject docInfo, final JSONObject docRoot) {
-        String apiName = docInfo.getString("real_req_path");
-        String basePath = docInfo.getString("basePath");
+    protected DocItem buildDocItem(final JsonObject docInfo, final JsonObject docRoot) {
+        String apiName = docInfo.get("real_req_path").getAsString();
+        String basePath = docInfo.get("basePath").getAsString();
         apiName = basePath + apiName;
 
         DocItem docItem = new DocItem();
         docItem.setId(UUID.randomUUID().toString());
         docItem.setName(apiName);
-        docItem.setSummary(docInfo.getString("summary"));
-        docItem.setDescription(docInfo.getString("description"));
-        docItem.setProduces(docInfo.getJSONArray("produces").toJavaList(String.class));
-        docItem.setMultiple(docInfo.getString("multiple") != null);
-        String apiResponseStr = docInfo.getString("apiResponse");
+        docItem.setSummary(docInfo.get("summary").getAsString());
+        docItem.setDescription(docInfo.get("description").getAsString());
+        docItem.setProduces(GsonUtils.getGson().fromJson(docInfo.getAsJsonArray("produces"), new TypeToken<List<String>>() { }.getType()));
+        docItem.setMultiple(docInfo.get("multiple").getAsString() != null);
+        String apiResponseStr = docInfo.get("apiResponse").getAsString();
         if (apiResponseStr != null) {
-            docItem.setBizCodeList(JSON.parseArray(apiResponseStr, CustomCode.class));
+            docItem.setBizCodeList(GsonUtils.getInstance().fromList(apiResponseStr, CustomCode.class));
         }
-        docItem.setModuleOrder(NumberUtils.toInt(docInfo.getString("module_order"), 0));
-        docItem.setApiOrder(NumberUtils.toInt(docInfo.getString("api_order"), 0));
+        docItem.setModuleOrder(NumberUtils.toInt(docInfo.get("module_order").getAsString(), 0));
+        docItem.setApiOrder(NumberUtils.toInt(docInfo.get("api_order").getAsString(), 0));
         String moduleName = this.buildModuleName(docInfo, docRoot, basePath);
         docItem.setModule(moduleName);
         List<DocParameter> docParameterList = this.buildRequestParameterList(docInfo, docRoot);
@@ -164,22 +166,22 @@ public class SwaggerDocParser implements DocParser {
         return docItem;
     }
 
-    protected String buildModuleName(final JSONObject docInfo, final JSONObject docRoot, final String basePath) {
-        final String title = Optional.ofNullable(docRoot.getJSONObject("info")).map(jsonObject -> jsonObject.getString("title")).orElse(basePath);
-        JSONArray tags = docInfo.getJSONArray("tags");
+    protected String buildModuleName(final JsonObject docInfo, final JsonObject docRoot, final String basePath) {
+        final String title = Optional.ofNullable(docRoot.getAsJsonObject("info")).map(jsonObject -> jsonObject.get("title").getAsString()).orElse(basePath);
+        JsonArray tags = docInfo.getAsJsonArray("tags");
         if (Objects.nonNull(tags) && tags.size() > 0) {
-            return tags.getString(0);
+            return tags.get(0).getAsString();
         }
         return title;
     }
 
-    protected List<DocParameter> buildRequestParameterList(final JSONObject docInfo, final JSONObject docRoot) {
-        Optional<JSONArray> parametersOptional = Optional.ofNullable(docInfo.getJSONArray("parameters"));
-        JSONArray parameters = parametersOptional.orElse(new JSONArray());
+    protected List<DocParameter> buildRequestParameterList(final JsonObject docInfo, final JsonObject docRoot) {
+        Optional<JsonArray> parametersOptional = Optional.ofNullable(docInfo.getAsJsonArray("parameters"));
+        JsonArray parameters = parametersOptional.orElse(new JsonArray());
         List<DocParameter> docParameterList = new ArrayList<>();
         for (int i = 0; i < parameters.size(); i++) {
-            JSONObject fieldJson = parameters.getJSONObject(i);
-            JSONObject schema = fieldJson.getJSONObject("schema");
+            JsonObject fieldJson = parameters.get(i).getAsJsonObject();
+            JsonObject schema = fieldJson.getAsJsonObject("schema");
             if (Objects.nonNull(schema)) {
                 RefInfo refInfo = getRefInfo(schema);
                 if (Objects.nonNull(refInfo)) {
@@ -187,7 +189,7 @@ public class SwaggerDocParser implements DocParser {
                     docParameterList.addAll(parameterList);
                 }
             } else {
-                DocParameter docParameter = fieldJson.toJavaObject(DocParameter.class);
+                DocParameter docParameter = GsonUtils.getInstance().fromJson(fieldJson, DocParameter.class);
                 docParameterList.add(docParameter);
             }
         }
@@ -220,7 +222,7 @@ public class SwaggerDocParser implements DocParser {
             .collect(Collectors.toList());
     }
 
-    protected List<DocParameter> buildResponseParameterList(final JSONObject docInfo, final JSONObject docRoot) {
+    protected List<DocParameter> buildResponseParameterList(final JsonObject docInfo, final JsonObject docRoot) {
         RefInfo refInfo = getResponseRefInfo(docInfo);
         List<DocParameter> respParameterList = Collections.emptyList();
         if (refInfo != null) {
@@ -238,29 +240,29 @@ public class SwaggerDocParser implements DocParser {
         return respParameterList;
     }
 
-    protected List<DocParameter> buildDocParameters(final String ref, final JSONObject docRoot, final boolean doSubRef) {
-        JSONObject responseObject = docRoot.getJSONObject("definitions").getJSONObject(ref);
-        String className = responseObject.getString("title");
-        JSONObject extProperties = docRoot.getJSONObject(className);
-        JSONArray requiredProperties = responseObject.getJSONArray("required");
-        JSONObject properties = responseObject.getJSONObject("properties");
+    protected List<DocParameter> buildDocParameters(final String ref, final JsonObject docRoot, final boolean doSubRef) {
+        JsonObject responseObject = docRoot.getAsJsonObject("definitions").getAsJsonObject(ref);
+        String className = responseObject.get("title").getAsString();
+        JsonObject extProperties = docRoot.getAsJsonObject(className);
+        JsonArray requiredProperties = responseObject.getAsJsonArray("required");
+        JsonObject properties = responseObject.getAsJsonObject("properties");
         List<DocParameter> docParameterList = new ArrayList<>();
         if (Objects.isNull(properties)) {
             return docParameterList;
         }
         Set<String> fieldNames = properties.keySet();
         for (String fieldName : fieldNames) {
-            JSONObject fieldInfo = properties.getJSONObject(fieldName);
-            DocParameter docParameter = fieldInfo.toJavaObject(DocParameter.class);
+            JsonObject fieldInfo = properties.getAsJsonObject(fieldName);
+            DocParameter docParameter = GsonUtils.getInstance().fromJson(fieldInfo, DocParameter.class);
             docParameter.setName(fieldName);
             docParameter.setRequired(
-                !CollectionUtils.isEmpty(requiredProperties) && requiredProperties.contains(fieldName));
+                    !(requiredProperties == null || requiredProperties.isEmpty()) && requiredProperties.contains(fieldInfo));
             if (Objects.nonNull(extProperties)) {
-                JSONObject prop = extProperties.getJSONObject(fieldName);
+                JsonObject prop = extProperties.getAsJsonObject(fieldName);
                 if (Objects.nonNull(prop)) {
-                    String maxLength = prop.getString("maxLength");
+                    String maxLength = prop.get("maxLength").getAsString();
                     docParameter.setMaxLength(Objects.isNull(maxLength) ? "-" : maxLength);
-                    String required = prop.getString("required");
+                    String required = prop.get("required").getAsString();
                     if (Objects.nonNull(required)) {
                         docParameter.setRequired(Boolean.parseBoolean(required));
                     }
@@ -284,22 +286,22 @@ public class SwaggerDocParser implements DocParser {
      * @param docInfo docInfo
      * @return RefInfo
      */
-    protected RefInfo getResponseRefInfo(final JSONObject docInfo) {
-        return Optional.ofNullable(docInfo.getJSONObject("responses"))
-            .flatMap(jsonObject -> Optional.ofNullable(jsonObject.getJSONObject("200")))
-            .flatMap(jsonObject -> Optional.ofNullable(jsonObject.getJSONObject("schema")))
+    protected RefInfo getResponseRefInfo(final JsonObject docInfo) {
+        return Optional.ofNullable(docInfo.getAsJsonObject("responses"))
+            .flatMap(jsonObject -> Optional.ofNullable(jsonObject.getAsJsonObject("200")))
+            .flatMap(jsonObject -> Optional.ofNullable(jsonObject.getAsJsonObject("schema")))
             .map(this::getRefInfo)
             .orElse(null);
     }
 
-    private RefInfo getRefInfo(final JSONObject jsonObject) {
+    private RefInfo getRefInfo(final JsonObject jsonObject) {
         String ref;
-        boolean isArray = "array".equals(jsonObject.getString("type"));
+        boolean isArray = "array".equals(jsonObject.get("type").getAsString());
         if (isArray) {
-            ref = jsonObject.getJSONObject("items").getString("$ref");
+            ref = jsonObject.getAsJsonObject("items").get("$ref").getAsString();
         } else {
             // #/definitions/xxx
-            ref = jsonObject.getString("$ref");
+            ref = jsonObject.get("$ref").getAsString();
         }
         if (Objects.isNull(ref)) {
             return null;