Skip to content

Commit 8256c46

Browse files
committed
🎨 binarywang#1619 企业微信获取用户信息接口优化,处理type为null情况下可能会导致的空指针问题
1 parent 3b37f52 commit 8256c46

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpUser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static class Attr {
8989
/**
9090
* 属性类型: 0-文本 1-网页
9191
*/
92-
private int type;
92+
private Integer type;
9393
private String name;
9494
private String textValue;
9595
private String webUrl;
@@ -104,7 +104,7 @@ public static class ExternalAttribute {
104104
/**
105105
* 属性类型: 0-本文 1-网页 2-小程序.
106106
*/
107-
private int type;
107+
private Integer type;
108108
/**
109109
* 属性名称: 需要先确保在管理端有创建改属性,否则会忽略.
110110
*/

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpUserGsonAdapter.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ private void buildExtraAttrs(JsonObject o, WxCpUser user) {
9898
.setName(GsonHelper.getString(attrJsonElement.getAsJsonObject(), "name"));
9999
user.getExtAttrs().add(attr);
100100

101+
if (type == null) {
102+
continue;
103+
}
104+
101105
switch (type) {
102106
case 0: {
103107
attr.setTextValue(GsonHelper.getString(attrJsonElement.getAsJsonObject().get("text").getAsJsonObject(), "value"));
@@ -120,6 +124,10 @@ private void buildExternalAttrs(JsonObject o, WxCpUser user) {
120124
final Integer type = GsonHelper.getInteger(element.getAsJsonObject(), "type");
121125
final String name = GsonHelper.getString(element.getAsJsonObject(), "name");
122126

127+
if (type == null) {
128+
continue;
129+
}
130+
123131
switch (type) {
124132
case 0: {
125133
user.getExternalAttrs()
@@ -253,6 +261,12 @@ public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationCon
253261
for (WxCpUser.Attr attr : user.getExtAttrs()) {
254262
JsonObject attrJson = new JsonObject();
255263

264+
attrsJsonArray.add(attrJson);
265+
266+
if (attr.getType() == null) {
267+
continue;
268+
}
269+
256270
switch (attr.getType()) {
257271
case 0: {
258272
JsonObject text = new JsonObject();
@@ -269,7 +283,6 @@ public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationCon
269283
}
270284
default: //ignored
271285
}
272-
attrsJsonArray.add(attrJson);
273286
}
274287
JsonObject attrsJson = new JsonObject();
275288
attrsJson.add("attrs", attrsJsonArray);
@@ -293,6 +306,13 @@ public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationCon
293306
JsonObject attrJson = new JsonObject();
294307
attrJson.addProperty("type", attr.getType());
295308
attrJson.addProperty("name", attr.getName());
309+
310+
attrsJsonArray.add(attrJson);
311+
312+
if (attr.getType() == null) {
313+
continue;
314+
}
315+
296316
switch (attr.getType()) {
297317
case 0: {
298318
JsonObject text = new JsonObject();
@@ -317,7 +337,6 @@ public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationCon
317337
}
318338
default://忽略
319339
}
320-
attrsJsonArray.add(attrJson);
321340
}
322341

323342
attrsJson.add(EXTERNAL_ATTR, attrsJsonArray);

0 commit comments

Comments
 (0)