Skip to content

Commit 73b9ac8

Browse files
authored
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
1 parent aacaf91 commit 73b9ac8

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

namesrv/src/main/java/org/apache/rocketmq/namesrv/processor/DefaultRequestProcessor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.rocketmq.namesrv.processor;
1818

19+
import com.alibaba.fastjson.serializer.SerializerFeature;
1920
import io.netty.channel.ChannelHandlerContext;
2021
import java.io.UnsupportedEncodingException;
2122
import java.util.Properties;
@@ -362,7 +363,9 @@ public RemotingCommand getRouteInfoByTopic(ChannelHandlerContext ctx,
362363
topicRouteData.setOrderTopicConf(orderTopicConf);
363364
}
364365

365-
byte[] content = topicRouteData.encode();
366+
byte[] content = topicRouteData.encode(SerializerFeature.BrowserCompatible,
367+
SerializerFeature.QuoteFieldNames, SerializerFeature.SkipTransientField,
368+
SerializerFeature.MapSortField);
366369
response.setBody(content);
367370
response.setCode(ResponseCode.SUCCESS);
368371
response.setRemark(null);

remoting/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,12 @@
4040
<groupId>${project.groupId}</groupId>
4141
<artifactId>rocketmq-logging</artifactId>
4242
</dependency>
43+
44+
<dependency>
45+
<groupId>com.google.code.gson</groupId>
46+
<artifactId>gson</artifactId>
47+
<version>2.9.0</version>
48+
<scope>test</scope>
49+
</dependency>
4350
</dependencies>
4451
</project>

remoting/src/main/java/org/apache/rocketmq/remoting/protocol/RemotingSerializable.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.apache.rocketmq.remoting.protocol;
1818

1919
import com.alibaba.fastjson.JSON;
20+
import com.alibaba.fastjson.serializer.SerializerFeature;
21+
2022
import java.nio.charset.Charset;
2123
import java.nio.charset.StandardCharsets;
2224

@@ -52,6 +54,17 @@ public byte[] encode() {
5254
return null;
5355
}
5456

57+
/**
58+
* Allow call-site to apply specific features according to their requirements.
59+
*
60+
* @param features Features to apply
61+
* @return serialized data.
62+
*/
63+
public byte[] encode(SerializerFeature...features) {
64+
final String json = JSON.toJSONString(this, features);
65+
return json.getBytes(CHARSET_UTF8);
66+
}
67+
5568
public String toJson() {
5669
return toJson(false);
5770
}

remoting/src/test/java/org/apache/rocketmq/remoting/protocol/RemotingSerializableTest.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,19 @@
1616
*/
1717
package org.apache.rocketmq.remoting.protocol;
1818

19+
import com.alibaba.fastjson.serializer.SerializerFeature;
20+
import com.google.gson.Gson;
21+
import com.google.gson.JsonElement;
22+
import com.google.gson.TypeAdapter;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
import java.io.IOException;
27+
import java.nio.charset.Charset;
1928
import java.util.Arrays;
29+
import java.util.HashMap;
2030
import java.util.List;
21-
import org.junit.Test;
31+
import java.util.Map;
2232

2333
import static org.assertj.core.api.Assertions.assertThat;
2434

@@ -80,6 +90,38 @@ public void setStringList(List<String> stringList) {
8090
"}");
8191
}
8292

93+
@Test
94+
public void testEncode() {
95+
class Foo extends RemotingSerializable {
96+
Map<Long, String> map = new HashMap<>();
97+
98+
Foo() {
99+
map.put(0L, "Test");
100+
}
101+
102+
public Map<Long, String> getMap() {
103+
return map;
104+
}
105+
}
106+
Foo foo = new Foo();
107+
String invalid = new String(foo.encode(), Charset.defaultCharset());
108+
String valid = new String(foo.encode(SerializerFeature.BrowserCompatible, SerializerFeature.QuoteFieldNames,
109+
SerializerFeature.MapSortField), Charset.defaultCharset());
110+
111+
Gson gson = new Gson();
112+
final TypeAdapter<JsonElement> strictAdapter = gson.getAdapter(JsonElement.class);
113+
try {
114+
strictAdapter.fromJson(invalid);
115+
Assert.fail("Should have thrown");
116+
} catch (IOException ignore) {
117+
}
118+
119+
try {
120+
strictAdapter.fromJson(valid);
121+
} catch (IOException ignore) {
122+
Assert.fail("Should not throw");
123+
}
124+
}
83125
}
84126

85127
class Sample {

0 commit comments

Comments
 (0)