Skip to content

Commit

Permalink
Let name server generate valid JSON response when process topic route…
Browse files Browse the repository at this point in the history
… queries (#4432)

* Let name server generate valid JSON response when process topic route queries
  • Loading branch information
lizhanhui authored Jun 9, 2022
1 parent aacaf91 commit 73b9ac8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -362,7 +363,9 @@ public RemotingCommand getRouteInfoByTopic(ChannelHandlerContext ctx,
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);
Expand Down
7 changes: 7 additions & 0 deletions remoting/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -52,6 +54,17 @@ public byte[] encode() {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -80,6 +90,38 @@ public void setStringList(List<String> stringList) {
"}");
}

@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 {
Expand Down

0 comments on commit 73b9ac8

Please sign in to comment.