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

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 24, 2025

Problem

When updating WeChat Work (企业微信) users, the directLeader field has specific API behavior:

  • null value: Field is ignored (no change to user's direct leaders)
  • Empty array []: Resets direct leaders (clears all direct leaders)
  • Non-empty array: Sets specific direct leaders

However, the current JSON serialization omits empty arrays entirely, preventing the API from resetting user direct leaders.

Root Cause

The WxCpUserGsonAdapter.addArrayProperty() method only includes arrays in JSON if array != null && array.length > 0. This causes empty directLeader arrays to be completely omitted from the serialized JSON.

Solution

Modified the serialization logic for the directLeader field to include empty arrays:

Before:

addArrayProperty(o, DIRECT_LEADER, user.getDirectLeader());

After:

// 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);
}

Behavior Changes

Input Before After WeChat Work API Result
new String[]{} {} (no field) {"direct_leader":[]} ✅ Resets leaders
null {} (no field) {} (no field) ✅ No change
new String[]{"a","b"} {"direct_leader":["a","b"]} {"direct_leader":["a","b"]} ✅ Sets leaders

Testing

Added comprehensive test coverage in WxCpUserGsonAdapterTest.testDirectLeaderEmptyArraySerialization() that verifies all three scenarios produce the correct JSON output.

Fixes #3628.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] 企业微信更新用户接口,directLeader 传入new String[]{} 空数组,会被toJson忽略掉导致传值失败 Fix directLeader empty array serialization for WeChat Work user update API Jul 24, 2025
@Copilot Copilot AI requested a review from binarywang July 24, 2025 07:57
Copilot finished work on behalf of binarywang July 24, 2025 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

企业微信更新用户接口,directLeader 传入new String[]{} 空数组,会被toJson忽略掉导致传值失败
2 participants