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

crazyhzm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 0b27565  [Dubbo-4218] Fix NPE when the TagRouterRule addresses config 
is null (#4218) (#4236)
0b27565 is described below

commit 0b275651aa461dd241b77eaead7fd48fb656de47
Author: web <[email protected]>
AuthorDate: Mon Jun 3 16:24:58 2019 +0800

    [Dubbo-4218] Fix NPE when the TagRouterRule addresses config is null 
(#4218) (#4236)
    
    * Fix NPE when the TagRouterRule addresses config is null (#4218)
    
    * fix import
    
    * add more test
---
 .../cluster/router/tag/model/TagRouterRule.java    |  8 +++-
 .../dubbo/rpc/cluster/router/TagRouterTest.java    | 45 ++++++++++++++++++++++
 2 files changed, 51 insertions(+), 2 deletions(-)

diff --git 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/model/TagRouterRule.java
 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/model/TagRouterRule.java
index 827518b..154a161 100644
--- 
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/model/TagRouterRule.java
+++ 
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/tag/model/TagRouterRule.java
@@ -16,6 +16,7 @@
  */
 package org.apache.dubbo.rpc.cluster.router.tag.model;
 
+import org.apache.dubbo.common.utils.CollectionUtils;
 import org.apache.dubbo.rpc.cluster.router.AbstractRouterRule;
 
 import java.util.ArrayList;
@@ -50,7 +51,7 @@ public class TagRouterRule extends AbstractRouterRule {
             return;
         }
 
-        tags.forEach(tag -> {
+        tags.stream().filter(tag -> 
CollectionUtils.isNotEmpty(tag.getAddresses())).forEach(tag -> {
             tagnameToAddresses.put(tag.getName(), tag.getAddresses());
             tag.getAddresses().forEach(addr -> {
                 List<String> tagNames = 
addressToTagnames.computeIfAbsent(addr, k -> new ArrayList<>());
@@ -60,7 +61,10 @@ public class TagRouterRule extends AbstractRouterRule {
     }
 
     public List<String> getAddresses() {
-        return tags.stream().flatMap(tag -> 
tag.getAddresses().stream()).collect(Collectors.toList());
+        return tags.stream()
+                .filter(tag -> CollectionUtils.isNotEmpty(tag.getAddresses()))
+                .flatMap(tag -> tag.getAddresses().stream())
+                .collect(Collectors.toList());
     }
 
     public List<String> getTagNames() {
diff --git 
a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/TagRouterTest.java
 
b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/TagRouterTest.java
index 98cdaf4..829208d 100644
--- 
a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/TagRouterTest.java
+++ 
b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/router/TagRouterTest.java
@@ -19,6 +19,8 @@ package org.apache.dubbo.rpc.cluster.router;
 import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.CuratorFrameworkFactory;
 import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.apache.dubbo.rpc.cluster.router.tag.model.TagRouterRule;
+import org.apache.dubbo.rpc.cluster.router.tag.model.TagRuleParser;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
@@ -63,4 +65,47 @@ public class TagRouterTest {
     private void setData(String path, String data) throws Exception {
         client.setData().forPath(path, data.getBytes());
     }
+
+    /**
+     * TagRouterRule parse test when the tags addresses is null
+     *
+     * <pre>
+     *     ~ -> null
+     *     null -> null
+     * </pre>
+     */
+    @Test
+    public void tagRouterRuleParseTest(){
+        String tagRouterRuleConfig = "---\n" +
+                "force: false\n" +
+                "runtime: true\n" +
+                "enabled: false\n" +
+                "priority: 1\n" +
+                "key: demo-provider\n" +
+                "tags:\n" +
+                "  - name: tag1\n" +
+                "    addresses: null\n" +
+                "  - name: tag2\n" +
+                "    addresses: [\"30.5.120.37:20880\"]\n" +
+                "  - name: tag3\n" +
+                "    addresses: []\n" +
+                "  - name: tag4\n" +
+                "    addresses: ~\n" +
+                "...";
+
+        TagRouterRule tagRouterRule = TagRuleParser.parse(tagRouterRuleConfig);
+
+        // assert tags
+        assert tagRouterRule.getTagNames().contains("tag1");
+        assert tagRouterRule.getTagNames().contains("tag2");
+        assert tagRouterRule.getTagNames().contains("tag3");
+        assert tagRouterRule.getTagNames().contains("tag4");
+        // assert addresses
+        assert tagRouterRule.getAddresses().contains("30.5.120.37:20880");
+        assert tagRouterRule.getTagnameToAddresses().get("tag1")==null;
+        assert tagRouterRule.getTagnameToAddresses().get("tag2").size()==1;
+        assert tagRouterRule.getTagnameToAddresses().get("tag3")==null;
+        assert tagRouterRule.getTagnameToAddresses().get("tag4")==null;
+        assert tagRouterRule.getAddresses().size()==1;
+    }
 }

Reply via email to