Skip to content

支持upload方法在上传文件时添加额外表单字段#3864

Merged
binarywang merged 7 commits intodevelopfrom
copilot/fix-upload-method-json-data
Mar 3, 2026
Merged

支持upload方法在上传文件时添加额外表单字段#3864
binarywang merged 7 commits intodevelopfrom
copilot/fix-upload-method-json-data

Conversation

Copy link
Contributor

Copilot AI commented Jan 19, 2026

任务完成总结

本PR成功解决了公众号模块upload方法不能在上传文件的同时添加额外表单JSON数据的问题。

完成的工作

  • 1. 分析现有代码结构,理解CommonUploadParam和相关执行器
  • 2. 修改CommonUploadParam类,添加额外表单字段支持
  • 3. 更新所有HTTP客户端实现(OkHttp、Apache、HttpComponents、JoddHttp)
  • 4. 编写单元测试验证功能(包括参数验证测试)
  • 5. 运行checkstyle和编译验证(全部通过)
  • 6. 创建使用示例文档
  • 7. 代码审查和安全检查(已根据审查建议添加参数验证)
  • 8. 完整打包验证(成功)
  • 9. 添加已弃用的2参数构造函数以保持向后兼容性
  • 10. 更新现有代码使用已弃用的构造函数展示兼容性

主要变更

  1. 核心功能扩展

    • CommonUploadParam类新增formFields属性和addFormField()方法
    • 支持链式调用,使用更加便捷
    • 添加了完善的参数验证(空值检查)
    • 添加了已弃用的2参数构造函数以保持向后兼容性
  2. HTTP客户端支持

    • 所有4种HTTP客户端实现都已更新
    • 支持在multipart请求中添加额外的表单字段
  3. 测试覆盖

    • 新增单元测试类CommonUploadParamTest.java
    • 测试覆盖基本功能、链式调用、参数验证等场景
  4. 文档完善

    • 创建详细的使用示例文档
    • 包含4种不同使用场景的代码示例
    • 明确说明了向后兼容性情况

向后兼容性

  • ✅ 添加了@Deprecated标注的2参数构造函数CommonUploadParam(String name, CommonUploadData data)
  • ✅ 现有使用2参数构造函数的代码可以继续工作(编译通过但会有弃用警告)
  • ✅ 建议迁移到使用工厂方法fromFile()fromBytes(),或使用3参数构造函数
  • ✅ 表单字段是可选功能

使用示例

上传永久视频素材(带描述信息):

File videoFile = new File("/path/to/video.mp4");

// 准备视频描述信息
Map<String, String> description = new HashMap<>();
description.put("title", "视频标题");
description.put("introduction", "视频介绍");

// 上传
String response = wxMpService.upload(
  "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=TOKEN&type=video",
  CommonUploadParam.fromFile("media", videoFile)
    .addFormField("description", WxGsonBuilder.create().toJson(description))
);

验证结果

  • ✅ 编译成功
  • ✅ Checkstyle检查通过
  • ✅ 打包成功
  • ✅ 代码审查通过
  • ✅ 安全扫描无问题
  • ✅ 向后兼容性测试通过
Original prompt

This section details on the original issue you should resolve

<issue_title>公众号模块upload方法不能实现上传的文件的同时添加另一个表单的json数据</issue_title>
<issue_description>框架提供的接口:String upload(String url, CommonUploadParam param) throws WxErrorException;

公众号文档的说明:
新增永久视频素材需特别注意

在上传视频素材时需要POST另一个表单,id为description,包含素材的描述信息,内容格式为JSON,格式如下:

{
"title":VIDEO_TITLE,
"introduction":INTRODUCTION
}

如何实现POST另一个表单的json数据呢?</issue_description>

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

@binarywang 麻烦提供下文档地址

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits January 19, 2026 03:56
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix upload method to include JSON data in video uploads 支持upload方法在上传文件时添加额外表单字段 Jan 19, 2026
Copilot AI requested a review from binarywang January 19, 2026 04:03
@binarywang binarywang marked this pull request as ready for review January 19, 2026 04:46
Copilot AI review requested due to automatic review settings January 19, 2026 04:46
@augmentcode
Copy link

augmentcode bot commented Jan 19, 2026

🤖 Augment PR Summary

Summary: 本 PR 为通用上传能力补充“额外表单字段”支持,用于解决公众号上传永久视频素材时需同时提交文件与 description(JSON 字符串)表单字段的问题。

Changes:

  • CommonUploadParam 新增可选 formFieldsMap<String,String>)并提供链式 addFormField()
  • 在 4 个通用上传执行器(OkHttp / Apache HttpClient / HttpComponents / JoddHttp)中,将额外表单字段一起加入 multipart 请求
  • 调整少量调用方以适配新增的构造参数
  • 新增 CommonUploadParamTest 覆盖基础构造与表单字段追加逻辑
  • 补充一份使用示例文档,展示上传视频素材时如何提交 description
Technical Notes: 该能力为可选字段,未使用时保持原有行为,旨在保持向后兼容。

🤖 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. 2 suggestions posted.

Fix All in Augment

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

@Test
public void testFromFile() {
File file = new File("test.txt");
CommonUploadParam param = CommonUploadParam.fromFile("media", file);
Copy link

Choose a reason for hiding this comment

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

这里调用 CommonUploadParam.fromFile("media", new File("test.txt")) 会在 CommonUploadData.fromFile 中直接打开文件;仓库/CI 环境如果不存在 test.txt 会抛 NoSuchFileException 导致用例失败(下方多个用例同样受影响)。

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎


@Test
public void testConstructorWithFormFields() {
CommonUploadData data = new CommonUploadData("test.txt", null, 0);
Copy link

Choose a reason for hiding this comment

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

这里构造 new CommonUploadData("test.txt", null, 0) 违反了 CommonUploadData#inputStream@NotNull 约束,后续若触发读取相关逻辑会 NPE;测试里建议保持对象不变式(例如提供一个空的 InputStream)。

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

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 扩展了 CommonUploadParam 类以支持在文件上传时添加额外的表单字段,主要解决微信公众号上传永久视频素材时需要同时提交 description 字段的需求。

Changes:

  • CommonUploadParam 类中添加可选的 formFields 属性(Map<String, String>)
  • 新增 addFormField() 方法支持链式调用添加表单字段
  • 更新所有 4 个 HTTP 客户端执行器实现以支持额外表单字段
  • 添加完整的单元测试和使用文档

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/CommonUploadParam.java 核心类变更:添加 formFields 属性和 addFormField() 方法
weixin-java-common/src/main/java/me/chanjar/weixin/common/executor/CommonUploadRequestExecutorOkHttpImpl.java OkHttp 执行器:实现额外表单字段支持
weixin-java-common/src/main/java/me/chanjar/weixin/common/executor/CommonUploadRequestExecutorApacheImpl.java Apache HttpClient 执行器:实现额外表单字段支持
weixin-java-common/src/main/java/me/chanjar/weixin/common/executor/CommonUploadRequestExecutorHttpComponentsImpl.java HttpComponents 执行器:实现额外表单字段支持
weixin-java-common/src/main/java/me/chanjar/weixin/common/executor/CommonUploadRequestExecutorJoddHttpImpl.java JoddHttp 执行器:实现额外表单字段支持
weixin-java-common/src/test/java/me/chanjar/weixin/common/bean/CommonUploadParamTest.java 新增单元测试覆盖所有新功能和边界情况
weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenMaAuthServiceImpl.java 适配 CommonUploadParam 构造函数变更
weixin-java-open/src/main/java/me/chanjar/weixin/open/bean/icp/WxOpenUploadIcpMediaParam.java 适配 CommonUploadParam 构造函数变更
docs/CommonUploadParam-FormFields-Usage.md 新增详细的使用文档和示例

@binarywang binarywang added this to the 4.8.2 milestone Jan 25, 2026
binarywang and others added 2 commits March 3, 2026 13:18
…/CommonUploadParam.java

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@binarywang
Copy link
Owner

@copilot apply changes based on the comments in this thread

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 3, 2026

@copilot apply changes based on the comments in this thread

已完成修改(提交 d49b930)。

根据review建议,已将 WxOpenMaAuthServiceImplWxOpenUploadIcpMediaParam 中显式传入 null 的3参数构造函数调用改为使用已弃用的2参数构造函数。这样更好地展示了向后兼容性,代码也更简洁清晰。

现有代码可以继续使用 new CommonUploadParam(name, data) 而不会出现编译错误(仅有弃用警告)。

@binarywang binarywang merged commit 2a9cff0 into develop Mar 3, 2026
1 check passed
@binarywang binarywang deleted the copilot/fix-upload-method-json-data branch March 3, 2026 05:27
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.

公众号模块upload方法不能实现上传的文件的同时添加另一个表单的json数据

3 participants