fastjson2 icon indicating copy to clipboard operation
fastjson2 copied to clipboard

fix: JSONB shared reference in non-List collection elements unresolvable on deserialization

Open yibiner opened this issue 6 months ago • 1 comments

What this PR does / why we need it?

反序列化时,非列表集合元素中的 JSONB 共享引用无法解析。

Summary of your change

  • ObjectWriterImplCollection.java — 对非 List 集合(如 HashSet)的元素禁用 ReferenceDetection,改为内联写入,避免生成 $.foo 这类以 Set 为根的路径在反序列化时无法解析。
  • SharedReferenceInSetTest.java — 新增回归测试。

Please indicate you've done the following:

  • [x] Made sure tests are passing and test coverage is added if needed.
  • [x] Made sure commit message follow the rule of Conventional Commits specification.
  • [ ] Considered the docs impact and opened a new docs issue or PR with docs changes if needed.

yibiner avatar May 12 '26 16:05 yibiner

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar May 12 '26 16:05 CLAassistant

已修复Review 问题。 https://github.com/alibaba/fastjson2/pull/7641/changes/d425e5b5a3fdaa18e8eeffabae2f4c2376310b03

问题: 非 List 集合(如 HashSet)开启 ReferenceDetection 时,元素内共享引用会输出不可解析的 $ref(如 "$.codes"),反序列化后字段为 null;write() 文本路径也有同样问题。

修复: 在 ObjectWriterImplCollection 的 writeJSONB() / write() 中,写每个元素前用 JSONWriter.saveReferences() / restoreReferences() 回滚 sibling 注册的 refs,强制内联共享对象;不关闭 ReferenceDetection,单元素循环引用仍可正常 $ref 终止。

文件变更: JSONWriter:新增 saveReferences() / restoreReferences() ObjectWriterImplCollection:两条序列化路径同步修复 测试:SharedReferenceInSetTest(18 用例)+ JSONWriterTest(2 个 API 单测)

yibiner avatar Jul 01 '26 16:07 yibiner

已根据 Code Review 更新修复方案: https://github.com/alibaba/fastjson2/pull/7641/commits/66bcc706d6336b418dc904e0ee46e11a5f73e338

  1. 将测试拆分为文本 JSON 与 JSONB 两套对称用例,各 42 个。
  2. 将引用处理从「元素级引用表回滚」改为「基于路径稳定性(unstableIndex)判断」:
    • Set 相关非循环共享引用改为内联;
    • 循环引用仍写 $ref,避免递归溢出。
  3. 兼容性说明:
    • LinkedHashSet / TreeSet:共享引用由 $ref 改为内联,值保真,不再保证共享对象身份;
    • 有序非 Set(如 ArrayDeque)文本路径:补齐元素级引用检测,共享对象可写 $ref 并保持身份。

yibiner avatar Jul 26 '26 18:07 yibiner

https://github.com/alibaba/fastjson2/pull/7641/commits/1a0107c661ff10a1f44949f1f11f5ccb953af86a 主要变更

1. refRestores 语义强化,修复嵌套循环下的错误回填

  • 由 IdentityHashMap<Object, Path> 改为 IdentityHashMap<Object, Path[]>,同时记录 [规范路径, 内联帧路径]。 popPath0 仅在「当前帧恰为登记的内联帧」(restore[1] == this.path)时才回填规范路径。此前嵌套循环中同一对象可能被更内层的帧提前 pop,导致规范路径被错误还原。

2. 拆分 Path.setElement 与 unstableIndex 两个维度

  • setElement:元素属于任意 Set(延迟解析可能改变哈希/顺序)。 unstableIndex:元素属于无序 Set,其索引路径不可作为 $ref。 内联判定改为 isUnderSetElement(this.path) || isUnderUnstable(previous);当 previous 位于无序 Set(不稳定)而当前路径稳定时,提升当前稳定路径为规范路径,使后续引用仍能写出可解析 $ref 并保持共享身份。

3. nextIndexPath 缓存修正

  • Path.ROOT 不再缓存 child0/child1(ROOT 跨 writer 共享,缓存集合标志会产生竞态)。 子节点缓存命中校验纳入 setElement,避免同一 index 下 List 与 Set 复用到错误的 Path。

4. writeReference 的 ".." 相对路径修正(文本 + JSONB 两路径同步)

  • 相等的 ".." 可能指向不同父节点,不再压缩为 #-1,避免误复用上一目标。

5. ObjectWriterImplCollection

  • LinkedHashSet / SortedSet 视为有序、元素路径可引用(unstableIndex=false),与修复前 JSONB 分支逻辑保持一致;仅无序 Set(如 HashSet)标记 unstableIndex。

yibiner avatar Jul 27 '26 17:07 yibiner

关闭本 PR。

原始问题已在 #7701 以不同方案修复。继续推进本 PR 会扩大改动范围、增加 Review 成本。

将关闭本 PR;其中仍独立存在的其他问题,后续拆分最小问题单独提修复。

yibiner avatar Aug 02 '26 13:08 yibiner