Skip to content

fix: honor reference detection defaults in compatible JSON instances - #7933

Open
hutiefang76 wants to merge 1 commit into
alibaba:mainfrom
hutiefang76:frank/fastjson7932-default-features-20261026
Open

hutiefang76 wants to merge 1 commit into
alibaba:mainfrom
hutiefang76:frank/fastjson7932-default-features-20261026

Conversation

@hutiefang76

Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

Fixes #7932. Enabling DisableCircularReferenceDetect in the compatibility layer's global defaults expands shared values through the static API, while the instance APIs still force reference detection.

Summary of your change

Honor that flag in JSON.toJSONString() and JSONObject.toString() while retaining the existing core writer provider, default reference behavior and subclass overrides. The change is limited to this option.

The seven regression cases cover objects, arrays, shared references, custom subclasses, an overridden toJSONString(), and an unrelated global-option control. The same frozen tests have five assertion failures before the patch and pass afterward in standard and reflect modes.

Validation on JDK 21: the core, extension and compatibility reactor completed with 9,773 test records in each mode, zero failures/errors; reflect mode has three existing ASM-only skips. Checkstyle passed. The changed production methods have bytecode sizes of 33 and 5 bytes.

The complete fork CI matrix passed: all 14 original OS/JDK test combinations completed the standard and reflect suites, and the coverage job passed. A separate validation-only workflow pinned every checkout to this PR source, 82fe8927aaf3cf67c503d30764798d62b999dd37; that workflow is not included in this PR. The macOS JDK 8 and 21 logs were also checked directly: each executed all seven Issue7932 cases in both modes, with zero failures, errors or skips.

Implementation and tests were assisted by DeepSeek; Codex coordinated reproduction, verification and review.

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follows Conventional Commits.
  • Considered the documentation impact; this fixes existing behavior and introduces no new API or option.
中文说明

开启兼容层全局 DisableCircularReferenceDetect 后,静态 API 会展开共享值,但实例入口仍强制生成引用。本补丁让相关实例入口遵守这一选项,同时保留原序列化器、默认引用检测及自定义子类行为;不扩大到其他全局选项。

同一份 7 项回归修复前有 5 项失败,修复后普通和 reflect 模式均通过。相关三个模块各模式共 9,773 项测试,零失败、零错误;reflect 模式有 3 项已有 ASM 专属跳过,Checkstyle 通过。原有 14 个操作系统/JDK 测试组合均已完成普通及 reflect 模式,coverage 也通过:fork CI。独立验证分支固定检出与本 PR 相同的源码;其工作流不进入本补丁。另已直接核验 macOS JDK 8 和 21 日志,7 项新回归在两种模式均实际执行,零失败、零错误、零跳过。代码和测试由 DeepSeek 辅助,Codex 完成复现、验证和审查协调。

Keep instance serialization on its existing core writer provider while honoring DisableCircularReferenceDetect. Preserve JSONObject subclass overrides and the default reference behavior.

Fixes alibaba#7932.

public String toJSONString() {
return com.alibaba.fastjson2.JSON.toJSONString(this, JSONWriter.Feature.ReferenceDetection);
if ((DEFAULT_GENERATE_FEATURE & SerializerFeature.DisableCircularReferenceDetect.mask) == 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

是不是直接调用jSON.tojosnstring(this)更好

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If you mean com.alibaba.fastjson.JSON.toJSONString(this), I checked it against the current implementation: the calls are not equivalent.

The compatibility provider uses Fastjson1xWriterModule. For a JSONObject or JSONArray subclass, that writer calls JSONAware.toJSONString() again. In a standalone JDK 21 probe, giving either subclass the suggested delegating method produces StackOverflowError in both standard and reflect modes. The exact base classes work, which is why the simple object/array examples do not expose it.

The static compatibility call also changes the output for an overridden toJSONString(), QuoteFieldNames, WriteMapNullValue and a custom compatibility writer. The current implementation already delegates to com.alibaba.fastjson2.JSON.toJSONString(...); it selects the overload for DisableCircularReferenceDetect while preserving the existing provider and other behavior. I have kept that narrower change here.

中文

如果指的是兼容层 com.alibaba.fastjson.JSON.toJSONString(this),实测并不等价。它的 writer 会对 JSONObject/JSONArray 子类回调 JSONAware.toJSONString();给子类换成这个委托实现后,独立 JDK 21 探针在普通和 reflect 模式都出现栈溢出。直接使用两个基类则正常,因此简单示例看不出这个差异。

静态兼容入口还会改变自定义 toJSONString()、QuoteFieldNames、WriteMapNullValue 和兼容层自定义 writer 的输出。当前实现已经调用 core 的 com.alibaba.fastjson2.JSON.toJSONString(...),只是按 DisableCircularReferenceDetect 选择重载,保留原 provider 和其他行为,所以这里先保留这个较小的修复。

This branch has not been deployed

No deployments
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.

[BUG]fastjsonv2兼容层不同序列化api行为不一致(disablerefdetect)

2 participants