Skip to content

Fix directLeader empty array serialization for WeChat Work user update API #3652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,12 @@ public JsonElement serialize(WxCpUser user, Type typeOfSrc, JsonSerializationCon
}
addProperty(o, MAIN_DEPARTMENT, user.getMainDepartment());

addArrayProperty(o, DIRECT_LEADER, user.getDirectLeader());
// Special handling for directLeader: include empty arrays to support WeChat Work API reset functionality
if (user.getDirectLeader() != null) {
JsonArray directLeaderArray = new JsonArray();
Arrays.stream(user.getDirectLeader()).forEach(directLeaderArray::add);
o.add(DIRECT_LEADER, directLeaderArray);
}

if (!user.getExtAttrs().isEmpty()) {
JsonArray attrsJsonArray = new JsonArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,31 @@ public void testSerialize() {
"{\"type\":2,\"name\":\"测试app\"," +
"\"miniprogram\":{\"appid\":\"wx8bd80126147df384\",\"pagepath\":\"/index\",\"title\":\"my miniprogram\"}}]}}");
}

/**
* Test directLeader empty array serialization.
* This test verifies that empty directLeader arrays are included in JSON as "direct_leader":[]
* instead of being omitted, which is required for WeChat Work API to reset user direct leaders.
*/
@Test
public void testDirectLeaderEmptyArraySerialization() {
WxCpUser user = new WxCpUser();
user.setUserId("testuser");
user.setName("Test User");

// Test with empty array - should be serialized as "direct_leader":[]
user.setDirectLeader(new String[]{});
String json = user.toJson();
assertThat(json).contains("\"direct_leader\":[]");

// Test with null - should not include direct_leader field
user.setDirectLeader(null);
json = user.toJson();
assertThat(json).doesNotContain("direct_leader");

// Test with non-empty array - should be serialized normally
user.setDirectLeader(new String[]{"leader1", "leader2"});
json = user.toJson();
assertThat(json).contains("\"direct_leader\":[\"leader1\",\"leader2\"]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public String toString() {
* </pre>
*/
@XStreamAlias("refund_recv_accout")
private String refundRecvAccout;
private String refundRecvAccount;

/**
* <pre>
Expand Down Expand Up @@ -324,7 +324,7 @@ public void loadXML(Document d) {
settlementRefundFee = readXmlInteger(d, "settlement_refund_fee");
refundStatus = readXmlString(d, "refund_status");
successTime = readXmlString(d, "success_time");
refundRecvAccout = readXmlString(d, "refund_recv_accout");
refundRecvAccount = readXmlString(d, "refund_recv_accout");
refundAccount = readXmlString(d, "refund_account");
refundRequestSource = readXmlString(d, "refund_request_source");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void testFromXMLFastMode() throws WxPayException {
refundNotifyResult.loadReqInfo(xmlDecryptedReqInfo);
assertEquals(refundNotifyResult.getReqInfo().getRefundFee().intValue(), 15);
assertEquals(refundNotifyResult.getReqInfo().getRefundStatus(), "SUCCESS");
assertEquals(refundNotifyResult.getReqInfo().getRefundRecvAccout(), "用户零钱");
assertEquals(refundNotifyResult.getReqInfo().getRefundRecvAccount(), "用户零钱");
System.out.println(refundNotifyResult);
} finally {
XmlConfig.fastMode = false;
Expand Down