You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by li...@apache.org on 2022/06/09 01:53:02 UTC

[rocketmq] branch develop updated: Let name server generate valid JSON response when process topic route queries (#4432)

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

lizhanhui pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new 73b9ac82b Let name server generate valid JSON response when process topic route queries (#4432)
73b9ac82b is described below

commit 73b9ac82bcd14b2a40ba31888a96e62d06d42d92
Author: Zhanhui Li <li...@gmail.com>
AuthorDate: Thu Jun 9 09:52:57 2022 +0800

    Let name server generate valid JSON response when process topic route queries (#4432)
    
    * Let name server generate valid JSON response when process topic route queries
---
 .../namesrv/processor/DefaultRequestProcessor.java |  5 ++-
 remoting/pom.xml                                   |  7 ++++
 .../remoting/protocol/RemotingSerializable.java    | 13 +++++++
 .../protocol/RemotingSerializableTest.java         | 44 +++++++++++++++++++++-
 4 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/namesrv/src/main/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessor.java b/namesrv/src/main/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessor.java
index 6df8c65bf..48a034296 100644
--- a/namesrv/src/main/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessor.java
+++ b/namesrv/src/main/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessor.java
@@ -16,6 +16,7 @@
  */
 package org.apache.rocketmq.namesrv.processor;
 
+import com.alibaba.fastjson.serializer.SerializerFeature;
 import io.netty.channel.ChannelHandlerContext;
 import java.io.UnsupportedEncodingException;
 import java.util.Properties;
@@ -362,7 +363,9 @@ public class DefaultRequestProcessor extends AsyncNettyRequestProcessor implemen
                 topicRouteData.setOrderTopicConf(orderTopicConf);
             }
 
-            byte[] content = topicRouteData.encode();
+            byte[] content = topicRouteData.encode(SerializerFeature.BrowserCompatible,
+                    SerializerFeature.QuoteFieldNames, SerializerFeature.SkipTransientField,
+                    SerializerFeature.MapSortField);
             response.setBody(content);
             response.setCode(ResponseCode.SUCCESS);
             response.setRemark(null);
diff --git a/remoting/pom.xml b/remoting/pom.xml
index 32320e6d0..1474e04dd 100644
--- a/remoting/pom.xml
+++ b/remoting/pom.xml
@@ -40,5 +40,12 @@
             <groupId>${project.groupId}</groupId>
             <artifactId>rocketmq-logging</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
+            <version>2.9.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java b/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java
index 4a32f65ff..182b5e7d0 100644
--- a/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java
+++ b/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java
@@ -17,6 +17,8 @@
 package org.apache.rocketmq.remoting.protocol;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 
@@ -52,6 +54,17 @@ public abstract class RemotingSerializable {
         return null;
     }
 
+    /**
+     * Allow call-site to apply specific features according to their requirements.
+     *
+     * @param features Features to apply
+     * @return serialized data.
+     */
+    public byte[] encode(SerializerFeature...features) {
+        final String json = JSON.toJSONString(this, features);
+        return json.getBytes(CHARSET_UTF8);
+    }
+
     public String toJson() {
         return toJson(false);
     }
diff --git a/remoting/src/test/java/org/apache/rocketmq/remoting/protocol/RemotingSerializableTest.java b/remoting/src/test/java/org/apache/rocketmq/remoting/protocol/RemotingSerializableTest.java
index 3e8b7a90a..b70e23ace 100644
--- a/remoting/src/test/java/org/apache/rocketmq/remoting/protocol/RemotingSerializableTest.java
+++ b/remoting/src/test/java/org/apache/rocketmq/remoting/protocol/RemotingSerializableTest.java
@@ -16,9 +16,19 @@
  */
 package org.apache.rocketmq.remoting.protocol;
 
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.google.gson.TypeAdapter;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
-import org.junit.Test;
+import java.util.Map;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
@@ -80,6 +90,38 @@ public class RemotingSerializableTest {
             "}");
     }
 
+    @Test
+    public void testEncode() {
+         class Foo extends RemotingSerializable {
+            Map<Long, String> map = new HashMap<>();
+
+            Foo() {
+             map.put(0L, "Test");
+            }
+
+             public Map<Long, String> getMap() {
+                 return map;
+             }
+         }
+         Foo foo = new Foo();
+         String invalid = new String(foo.encode(), Charset.defaultCharset());
+         String valid = new String(foo.encode(SerializerFeature.BrowserCompatible, SerializerFeature.QuoteFieldNames,
+                 SerializerFeature.MapSortField), Charset.defaultCharset());
+
+         Gson gson = new Gson();
+         final TypeAdapter<JsonElement> strictAdapter = gson.getAdapter(JsonElement.class);
+         try {
+             strictAdapter.fromJson(invalid);
+             Assert.fail("Should have thrown");
+         } catch (IOException ignore) {
+         }
+
+        try {
+            strictAdapter.fromJson(valid);
+        } catch (IOException ignore) {
+            Assert.fail("Should not throw");
+        }
+    }
 }
 
 class Sample {