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
This commit is contained in:
Zhanhui Li
2022-06-09 09:52:57 +08:00
committed by GitHub
parent aacaf91539
commit 73b9ac82bc
4 changed files with 68 additions and 3 deletions
@@ -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);
+7
View File
@@ -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>
@@ -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);
}
@@ -16,10 +16,20 @@
*/
package org.apache.rocketmq.remoting.protocol;
import java.util.Arrays;
import java.util.List;
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 java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
public class RemotingSerializableTest {
@@ -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 {