Skip to content

Fix NPE in EntPayRequest#storeMap when brandId is null#3909

Merged
binarywang merged 2 commits intodevelopfrom
copilot/fix-null-pointer-in-storemap
Mar 7, 2026
Merged

Fix NPE in EntPayRequest#storeMap when brandId is null#3909
binarywang merged 2 commits intodevelopfrom
copilot/fix-null-pointer-in-storemap

Conversation

Copy link
Contributor

Copilot AI commented Mar 7, 2026

EntPayRequest.storeMap() unconditionally called brandId.toString(), but brand_id is an optional field — causing NPE whenever the request was serialized (e.g. via Jackson) without brandId set.

Changes

  • EntPayRequest.java: Guard brand_id map insertion with a null check; the key is omitted entirely when brandId is null

    // Before
    map.put("brand_id", brandId.toString());
    
    // After
    if (brandId != null) {
      map.put("brand_id", brandId.toString());
    }
  • EntPayRequestTest.java: Add tests covering brandId = null (key absent, no NPE) and brandId set (correct value present)

Original prompt

This section details on the original issue you should resolve

<issue_title>com.github.binarywang.wxpay.bean.entpay.EntPayRequest#storeMap中brandId空指针异常</issue_title>
<issue_description>### 简要描述
com.github.binarywang.wxpay.bean.entpay.EntPayRequest#storeMap
brandId非必填但在storeMap中brandId.toString()是否会容易出现空指针异常

模块版本情况

  • WxJava 模块名: 微信支付
  • WxJava 版本号: v4.8.0

详细描述

在使用sdk企业付款到零钱时,为了方便重试或者为了记录日志,会在每次调用api后保存请求参数与响应参数,使用jackson以json字符串入库但现在因为brandId的问题无法转为json字符串,会报空指针异常

Image

日志

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.github.binarywang.wxpay.bean.entpay.EntPayRequest["signParams"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:392)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:351)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:316)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:782)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4568)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3821)
at com.wikemi.huodongbao.framework.common.util.json.JsonUtils.toJsonString(JsonUtils.java:50)
at com.wikemi.huodongbao.app.wx.WxPayTest.main(WxPayTest.java:260)
Caused by: java.lang.NullPointerException
at com.github.binarywang.wxpay.bean.entpay.EntPayRequest.storeMap(EntPayRequest.java:252)
at com.github.binarywang.wxpay.bean.request.BaseWxPayRequest.getSignParams(BaseWxPayRequest.java:329)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:689)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:774)
</issue_description>

Comments on the Issue (you are @copilot in this section)

Custom agent used: 全部用中文
需要用中文,包括PR标题和分析总结过程


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix NullPointerException in storeMap for brandId Fix NPE in EntPayRequest#storeMap when brandId is null Mar 7, 2026
@binarywang binarywang marked this pull request as ready for review March 7, 2026 07:51
Copilot AI review requested due to automatic review settings March 7, 2026 07:51
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

该 PR 修复了微信支付“企业付款到零钱”请求对象 EntPayRequest 在序列化/签名参数组装过程中因 brandId(可选字段)为空而触发的空指针异常问题,提升了 SDK 在可选参数未设置时的健壮性(对应 issue #3908 的场景:Jackson 序列化触发 getSignParams())。

Changes:

  • EntPayRequest#storeMap 中为 brandId 增加空值判断,为空时不再写入 brand_id,避免 brandId.toString() NPE
  • 新增单测覆盖 brandId = null(不包含 brand_id 且不抛异常)与 brandId 非空(包含正确键值)两种情况

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/bean/entpay/EntPayRequest.java storeMap 中对可选字段 brandId 做 null guard,避免 NPE 并在为空时省略 brand_id
weixin-java-pay/src/test/java/com/github/binarywang/wxpay/bean/entpay/EntPayRequestTest.java 补充 getSignParams()brandId 为空/非空场景下的回归测试

@augmentcode
Copy link

augmentcode bot commented Mar 7, 2026

🤖 Augment PR Summary

Summary: 修复企业付款请求对象在序列化/取签名参数时因 brandId 为空导致的 NPE。

Changes:

  • EntPayRequest#storeMap:仅在 brandId != null 时写入 brand_id 参数,避免对空对象调用 toString()
  • 新增单测覆盖 brandId 为空/非空两种场景,验证 getSignParams() 行为(不抛异常且参数符合预期)

Technical Notes: brand_id 为可选字段;为空时省略该键与签名/生成 XML 的过滤逻辑一致,避免 Jackson 作为 Bean 属性访问 signParams 时触发异常。

🤖 Was this summary useful? React with 👍 or 👎

Copy link

@augmentcode augmentcode bot left a comment

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.

@binarywang binarywang merged commit e2120d6 into develop Mar 7, 2026
5 checks passed
@binarywang binarywang deleted the copilot/fix-null-pointer-in-storemap branch March 7, 2026 07:59
@binarywang binarywang added this to the 4.8.2 milestone Mar 7, 2026
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.

com.github.binarywang.wxpay.bean.entpay.EntPayRequest#storeMap中brandId空指针异常

3 participants