From 7760bb4a43924608cb998fc87d038c9cf472fcd1 Mon Sep 17 00:00:00 2001 From: carlvine500 Date: Fri, 3 Apr 2020 16:20:08 +0800 Subject: [PATCH] [ISSUE #1463] Skip encode the null value property as string "null" (#1899) * Update MessageDecoder.java * add test case for MessageDecoder --- .../common/message/MessageDecoder.java | 3 +++ .../common/message/MessageDecoderTest.java | 21 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/org/apache/rocketmq/common/message/MessageDecoder.java b/common/src/main/java/org/apache/rocketmq/common/message/MessageDecoder.java index 54d5e6b6..d048dde2 100644 --- a/common/src/main/java/org/apache/rocketmq/common/message/MessageDecoder.java +++ b/common/src/main/java/org/apache/rocketmq/common/message/MessageDecoder.java @@ -414,6 +414,9 @@ public class MessageDecoder { final String name = entry.getKey(); final String value = entry.getValue(); + if (value == null) { + continue; + } sb.append(name); sb.append(NAME_VALUE_SEPARATOR); sb.append(value); diff --git a/common/src/test/java/org/apache/rocketmq/common/message/MessageDecoderTest.java b/common/src/test/java/org/apache/rocketmq/common/message/MessageDecoderTest.java index 82ad2714..fde523db 100644 --- a/common/src/test/java/org/apache/rocketmq/common/message/MessageDecoderTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/message/MessageDecoderTest.java @@ -246,4 +246,23 @@ public class MessageDecoderTest { assertThat("abc").isEqualTo(decodedMsg.getTopic()); } -} + + public void testNullValueProperty() throws Exception { + MessageExt msg = new MessageExt(); + msg.setBody("x".getBytes()); + msg.setTopic("x"); + msg.setBornHost(new InetSocketAddress("127.0.0.1", 9000)); + msg.setStoreHost(new InetSocketAddress("127.0.0.1", 9000)); + String key = "NullValueKey"; + msg.putProperty(key, null); + try { + byte[] encode = MessageDecoder.encode(msg, false); + MessageExt decode = MessageDecoder.decode(ByteBuffer.wrap(encode)); + assertThat(decode.getProperty(key)).isNull(); + } catch (Exception e) { + e.printStackTrace(); + assertThat(Boolean.FALSE).isTrue(); + } + } + +} \ No newline at end of file -- GitLab