Skip to content

Add null check for textJsonElement in WxCpUserGsonAdapter#3632

Merged
binarywang merged 3 commits intobinarywang:developfrom
zhfkt:fix-npe-in-extra-attrs
Jul 4, 2025
Merged

Add null check for textJsonElement in WxCpUserGsonAdapter#3632
binarywang merged 3 commits intobinarywang:developfrom
zhfkt:fix-npe-in-extra-attrs

Conversation

@zhfkt
Copy link
Contributor

@zhfkt zhfkt commented Jul 2, 2025

For some Enterprise Wechat endpoints, attrJsonElement.getAsJsonObject().get("text"); return null if there is no field text in attrJsonElement. We need to check null value before getAsJsonObject.

@binarywang binarywang requested a review from Copilot July 3, 2025 01:56

This comment was marked as outdated.

…CpUserGsonAdapter.java

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@binarywang binarywang requested a review from Copilot July 4, 2025 03:30
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a null check around the text JSON element in buildExtraAttrs to prevent a potential NPE when attrJsonElement has no "text" field.

  • Introduced textJsonElement local variable and guard clauses.
  • Wrapped attr.setTextValue(...) in an if that verifies the element is non-null and an object.
  • Removed the previous direct call to getAsJsonObject().get("text").
Comments suppressed due to low confidence (1)

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

  • Add a unit test for the case where attrJsonElement has no "text" field to verify that buildExtraAttrs handles missing text correctly.
          JsonElement textJsonElement = attrJsonElement.getAsJsonObject().get("text");

…CpUserGsonAdapter.java

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@binarywang binarywang merged commit 84c69d4 into binarywang:develop Jul 4, 2025
@binarywang binarywang added this to the 4.7.7 milestone Jul 4, 2025
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.

3 participants