Skip to content

Return null from GsonHelper array getters for a non-array property#4056

Open
vasiliy-mikhailov wants to merge 1 commit into
binarywang:developfrom
vasiliy-mikhailov:fix/gsonhelper-non-array-graceful
Open

Return null from GsonHelper array getters for a non-array property#4056
vasiliy-mikhailov wants to merge 1 commit into
binarywang:developfrom
vasiliy-mikhailov:fix/gsonhelper-non-array-graceful

Conversation

@vasiliy-mikhailov

Copy link
Copy Markdown

GsonHelper.getIntArray, getStringArray and getLongArray call gson's JsonObject.getAsJsonArray(memberName), which casts the member to JsonArray. If the property exists but is not a JSON array (e.g. a string or number), this throws an unchecked ClassCastException instead of returning gracefully.

This passes the raw element via o.get(string) to the existing null-safe getAsJsonArray(JsonElement) helper, and makes that helper treat a non-array element the same as null — so the three methods return null for a non-array property, matching how they already handle a missing/null property. The other caller of the helper (WxOpenAuthorizationInfoGsonAdapter) is unaffected for valid arrays and becomes null-safe for malformed input.

Adds regression tests for the three methods that fail before the change (CCE) and pass after.

…ray property

These methods call gson JsonObject.getAsJsonArray(memberName), which casts the member to JsonArray and throws ClassCastException when the property exists but is not an array (e.g. a string or number). Pass the raw element to the null-safe getAsJsonArray(JsonElement) helper and make that helper treat a non-array element like null, so the methods return null for a non-array property just as they already do for a missing one. Adds regression tests.
@binarywang

Copy link
Copy Markdown
Owner

@codex auggie review

@augmentcode

augmentcode Bot commented Jun 25, 2026

Copy link
Copy Markdown
🤖 Augment PR Summary

Summary: Makes GsonHelper’s array getters return null (instead of throwing) when a JSON member exists but is not an array.

Changes:

  • Switch getIntArray/getStringArray/getLongArray to read via JsonObject.get() and the shared getAsJsonArray(JsonElement) helper.
  • Harden getAsJsonArray to return null for null/JsonNull/non-array elements to avoid unchecked ClassCastException.
  • Add regression tests ensuring the three getters are null-safe for non-array properties.
Notes: Improves robustness against malformed input while keeping behavior unchanged for valid arrays.

🤖 Was this summary useful? React with 👍 or 👎

@augmentcode augmentcode Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review completed. No suggestions at this time.

Comment augment review to trigger a new review at any time.

@chatgpt-codex-connector

Copy link
Copy Markdown

Review 结论

未发现需要阻塞合并的问题。该 PR 将 getIntArraygetStringArraygetLongArray 从直接调用 JsonObject#getAsJsonArray(String) 改为先取 JsonElement 再走统一的 getAsJsonArray(JsonElement),避免非数组字段触发 ClassCastException;同时 getAsJsonArray 对非数组元素返回 null,与缺失/空字段的处理语义一致。相关实现集中在 GsonHelper 的数组 getter 和 helper 方法中。 weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/GsonHelper.javaL110-L153

新增的回归用例覆盖了三个数组 getter 遇到非数组属性时返回 null 的行为,能有效防止该问题回归。 weixin-java-common/src/test/java/me/chanjar/weixin/common/util/json/GsonHelperTest.javaL140-L163

重点检查项

Testing

  • git diff --find-renames 1697535faeedb3509d202d9f6e6ccbaa29323de5...780000b0a3e777a1c806a19c054a2092779e1d4c -- weixin-java-common/src/main/java/me/chanjar/weixin/common/util/json/GsonHelper.java weixin-java-common/src/test/java/me/chanjar/weixin/common/util/json/GsonHelperTest.java
  • rg "getAsJsonArray\\(" -n weixin-java-common/src/main/java weixin-java-common/src/test/java | head -50
  • ⚠️ mvn -pl weixin-java-common -Dtest=me.chanjar.weixin.common.util.json.GsonHelperTest test(环境访问 Maven Central 返回 403,导致依赖/插件无法解析,测试未能实际执行)

View task →

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.

2 participants