Skip to content

fix: use createReadContext for string reparse in fastjson1-compatible, for issue #7765 - #7767

Merged
wenshao merged 3 commits into
alibaba:mainfrom
yibiner:fix/fastjson1-compat-nested-container-type
Oct 17, 2026
Merged

wenshao merged 3 commits into
alibaba:mainfrom
yibiner:fix/fastjson1-compat-nested-container-type

Conversation

@yibiner

@yibiner yibiner commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

Fixes #7765.
In fastjson1-compatible mode, when a field value is a JSON string and is re-parsed by getJSONArray / getJSONObject / getObject, nested containers could become com.alibaba.fastjson2.JSONObject / JSONArray. Casting them to com.alibaba.fastjson.JSONObject then throws ClassCastException.
Root cause: those paths used bare JSONReader.of(...) and skipped JSON.createReadContext(...), so objectSupplier / arraySupplier were not applied.

Summary of your change

  • Use JSON.createReadContext(JSON.DEFAULT_PARSER_FEATURE) for string reparse in:

    • JSONObject.getJSONObject(String) / getJSONArray(String) / getObject(String, TypeReference)
    • JSONArray.getJSONObject(int) / getJSONArray(int) / getObject(int, Class)
  • Add regression tests in Issue7765.

中文说明

这个 PR 做了什么 / 为什么需要?

修复 #7765。

fastjson1 兼容模式下,字段值是 JSON 字符串时,经 getJSONArray / getJSONObject / getObject 二次解析后,嵌套容器可能变成 com.alibaba.fastjson2.JSONObject / JSONArray,再强转为 com.alibaba.fastjson.JSONObject 会抛 ClassCastException。

根因:这些路径使用了裸的 JSONReader.of(...),未走 JSON.createReadContext(...),因此未注入 objectSupplier / arraySupplier。

变更摘要

上述 6 处二次解析改为使用 JSON.createReadContext(JSON.DEFAULT_PARSER_FEATURE)
新增 Issue7765 回归测试

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • 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.

@wenshao wenshao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed — no blockers. Suggestions are inline.

中文说明

已审查——无阻断问题。 建议见行内评论。

— glm-5.2 via Qwen Code /review (v0.21.10)

@yibiner

yibiner commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review — all three suggestions addressed.

  1. Added JSON.createReadContext(Feature...) and switched the duplicated call sites to JSON.createReadContext() / JSON.createReadContext(features).
  2. Fixed remaining bare JSONReader.of(...) sites:
    • JSONPath.read(String, String, Type)
    • JSONScanner(String)
    • HSFJSONUtils.parseInvocationArguments
    • also DefaultJSONParser(String) / (String, ParserConfig) (same root cause)
  3. Replaced the inert native-array test with a string-reparse cast case, and added regression tests for the new entry points above.
中文说明

感谢 review,三条建议都已处理:

  1. 新增 JSON.createReadContext(Feature...),并把重复调用统一为 JSON.createReadContext() / JSON.createReadContext(features)。
  2. 修复剩余裸 JSONReader.of(...):
    • JSONPath.read(String, String, Type)
    • JSONScanner(String)
    • HSFJSONUtils.parseInvocationArguments
    • 顺带修了同根因的 DefaultJSONParser(String) / (String, ParserConfig)
  3. 无效的原生数组测试已改为字符串二次解析强转用例,并为上述新入口补充了回归测试。

@wenshao wenshao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed. Suggestions are inline. 1 Suggestion-level finding(s) could not be anchored to a changed line and were dropped; nothing further to act on here.

Not explored to full depth (tool budget reached): "本 PR 修复 fastjson1-compatible 的 issue…": did not compile the changed classes for static bytecode-size measurement (javap tier) — the ~6–8-byte growth per modified method was assessed by inspection agai…; "本 PR 修复 fastjson1-compatible 的 issue…": none — I did not run mvn test (worktree shared with concurrent agents), relying on static verification of signatures/overloads instead.; "本 PR 修复 fastjson1-compatible 的 issue…": did not execute mvn test -Dtest=Issue7765 — the worktree is shared with concurrently running review agents, so a Maven build in place risks racing their work;…; "本 PR 修复 fastjson1-compatible 的 issue…": did not compile/run the module tests — the worktree has no pre-built target/classes and building via Maven in a shared worktree is prohibited for this review;…; "本 PR 修复 fastjson1-compatible 的 issue…": no bytecode-size measurement (static tier needs a compile; no pre-built classpath) — not pursued because the per-method structural delta is a single invokestati…, and 4 more.

中文说明

已审查。 建议见行内评论。 1 条建议级发现无法锚定到改动行,已丢弃;此处无需进一步处理。

未探索到全部深度(达到工具调用预算):"本 PR 修复 fastjson1-compatible 的 issue…":did not compile the changed classes for static bytecode-size measurement (javap tier) — the ~6–8-byte growth per modified method was assessed by inspection agai…;"本 PR 修复 fastjson1-compatible 的 issue…":none — I did not run mvn test (worktree shared with concurrent agents), relying on static verification of signatures/overloads instead.;"本 PR 修复 fastjson1-compatible 的 issue…":did not execute mvn test -Dtest=Issue7765 — the worktree is shared with concurrently running review agents, so a Maven build in place risks racing their work;…;"本 PR 修复 fastjson1-compatible 的 issue…":did not compile/run the module tests — the worktree has no pre-built target/classes and building via Maven in a shared worktree is prohibited for this review;…;"本 PR 修复 fastjson1-compatible 的 issue…":no bytecode-size measurement (static tier needs a compile; no pre-built classpath) — not pursued because the per-method structural delta is a single invokestati…,另有 4 条。

— qwen3.8-max via Qwen Code /review (v0.21.11)

@wenshao wenshao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No issues found. LGTM! ✅

中文说明

未发现问题。LGTM!✅

— glm-5.3 via Qwen Code /review (v0.21.11)

@wenshao
wenshao merged commit 3041a0b into alibaba:main Oct 17, 2026
30 checks passed
wylovelyi added a commit to wylovelyi/fastjson2 that referenced this pull request Nov 4, 2026
Resolve the conflict with alibaba#7767, which refactored the same three files:

  * JSON.createReadContext(Feature...) is kept as added upstream;
  * JSONPath.read(String, String, Type, ParserConfig) keeps
    JSON.createReadContext() and only flips SupportAutoType on top of it;
  * DefaultJSONParser builds its reader from JSON.createReadContext() and only
    flips SupportAutoType when the ParserConfig asks for it.

The resulting diff against main is the auto type gating alone.
wylovelyi added a commit to wylovelyi/fastjson2 that referenced this pull request Nov 4, 2026
Resolve the conflict with alibaba#7767, which refactored the same three files:

  * JSON.createReadContext(Feature...) is kept as added upstream;
  * JSONPath.read(String, String, Type, ParserConfig) keeps
    JSON.createReadContext() and only flips SupportAutoType on top of it;
  * DefaultJSONParser builds its reader from JSON.createReadContext() and only
    flips SupportAutoType when the ParserConfig asks for it.

The resulting diff against main is the auto type gating alone.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants