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

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


The following commit(s) were added to refs/heads/3.2 by this push:
     new 25548c345b Fix NPE if name not match in PojoUtils (#12404)
25548c345b is described below

commit 25548c345bdfc1dcf222b68c2057cd472e2b74a0
Author: Albumen Kevin <[email protected]>
AuthorDate: Fri May 26 15:32:54 2023 +0800

    Fix NPE if name not match in PojoUtils (#12404)
---
 .../org/apache/dubbo/common/utils/PojoUtils.java   |  7 +-
 .../apache/dubbo/common/utils/PojoUtilsTest.java   | 96 +++++++++++++++++-----
 2 files changed, 83 insertions(+), 20 deletions(-)

diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java 
b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java
index 30b454ebe8..8dcea2fcef 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/PojoUtils.java
@@ -50,6 +50,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.Properties;
 import java.util.TreeMap;
 import java.util.WeakHashMap;
@@ -539,7 +540,11 @@ public class PojoUtils {
                                 if (!method.isAccessible()) {
                                     method.setAccessible(true);
                                 }
-                                Type containType = 
mapGeneric.get(field.getGenericType().getTypeName());
+                                Type containType = Optional.ofNullable(field)
+                                    .map(Field::getGenericType)
+                                    .map(Type::getTypeName)
+                                    .map(mapGeneric::get)
+                                    .orElse(null);
                                 if (containType != null) {
                                     //is generic
                                     if (containType instanceof 
ParameterizedType) {
diff --git 
a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java 
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java
index 4c67a41d99..2066142c11 100644
--- 
a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java
+++ 
b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/PojoUtilsTest.java
@@ -16,6 +16,25 @@
  */
 package org.apache.dubbo.common.utils;
 
+import org.apache.dubbo.common.model.Person;
+import org.apache.dubbo.common.model.SerializablePerson;
+import org.apache.dubbo.common.model.User;
+import org.apache.dubbo.common.model.person.Ageneric;
+import org.apache.dubbo.common.model.person.Bgeneric;
+import org.apache.dubbo.common.model.person.BigPerson;
+import org.apache.dubbo.common.model.person.Cgeneric;
+import org.apache.dubbo.common.model.person.Dgeneric;
+import org.apache.dubbo.common.model.person.FullAddress;
+import org.apache.dubbo.common.model.person.PersonInfo;
+import org.apache.dubbo.common.model.person.PersonMap;
+import org.apache.dubbo.common.model.person.PersonStatus;
+import org.apache.dubbo.common.model.person.Phone;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
 import java.io.Serializable;
 import java.lang.reflect.Method;
 import java.lang.reflect.Type;
@@ -32,27 +51,9 @@ import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.UUID;
 
-import com.alibaba.fastjson.JSON;
-import org.apache.dubbo.common.model.Person;
-import org.apache.dubbo.common.model.SerializablePerson;
-import org.apache.dubbo.common.model.User;
-import org.apache.dubbo.common.model.person.Bgeneric;
-import org.apache.dubbo.common.model.person.BigPerson;
-import org.apache.dubbo.common.model.person.Cgeneric;
-import org.apache.dubbo.common.model.person.Dgeneric;
-import org.apache.dubbo.common.model.person.FullAddress;
-import org.apache.dubbo.common.model.person.Ageneric;
-import org.apache.dubbo.common.model.person.PersonInfo;
-import org.apache.dubbo.common.model.person.PersonMap;
-import org.apache.dubbo.common.model.person.PersonStatus;
-import org.apache.dubbo.common.model.person.Phone;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
-
-import com.alibaba.fastjson.JSONObject;
-
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.equalTo;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
@@ -998,6 +999,63 @@ class PojoUtilsTest {
         }
     }
 
+    @Test
+    void testNameNotMatch() {
+        NameNotMatch origin = new NameNotMatch();
+        origin.setNameA("test123");
+        origin.setNameB("test234");
+
+        Object generalized = PojoUtils.generalize(origin);
+
+        Assertions.assertInstanceOf(Map.class, generalized);
+        Assertions.assertEquals("test123", ((Map)generalized).get("nameA"));
+        Assertions.assertEquals("test234", ((Map)generalized).get("nameB"));
+
+        NameNotMatch target1 = (NameNotMatch) 
PojoUtils.realize(PojoUtils.generalize(origin), NameNotMatch.class, 
NameNotMatch.class);
+        Assertions.assertEquals(origin, target1);
+
+        Map<String, String> map = new HashMap<>();
+        map.put("nameA", "test123");
+        map.put("nameB", "test234");
+
+        NameNotMatch target2 = (NameNotMatch) PojoUtils.realize(map, 
NameNotMatch.class, NameNotMatch.class);
+        Assertions.assertEquals(origin, target2);
+    }
+
+    class NameNotMatch implements Serializable {
+        private String NameA;
+        private String NameAbsent;
+
+        public void setNameA(String nameA) {
+            this.NameA = nameA;
+        }
+
+        public String getNameA() {
+            return NameA;
+        }
+
+        public void setNameB(String nameB) {
+            this.NameAbsent = nameB;
+        }
+
+        public String getNameB() {
+            return NameAbsent;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) return true;
+            if (o == null || getClass() != o.getClass()) return false;
+            NameNotMatch that = (NameNotMatch) o;
+            return Objects.equals(NameA, that.NameA) && 
Objects.equals(NameAbsent, that.NameAbsent);
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(NameA, NameAbsent);
+        }
+    }
+
     public enum Day {
         SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
     }

Reply via email to