Skip to content

fix: ClassCastException in JSON.toJSON() when a generic field's cached initWriter does not match the runtime value class, for issue #7853 - #7854

Merged
wenshao merged 1 commit into
alibaba:mainfrom
wylovelyi:fix-issue7853
Oct 17, 2026
Merged

wenshao merged 1 commit into
alibaba:mainfrom
wylovelyi:fix-issue7853

Conversation

@wylovelyi

Copy link
Copy Markdown
Contributor

What this PR does / why we need it?

Fixes #7853.

When serializing the same generic bean class with different generic arguments via JSON.toJSON(), a ClassCastException is thrown (e.g. Boolean cannot be cast to PagerDataBean), because ObjectWriterAdapter.toJSONObject reuses the writer cached by fieldWriter.getInitWriter() without checking that it matches the runtime value class.

Note that on some JDK versions this bug can even manifest as a JVM crash (EXCEPTION_ACCESS_VIOLATION) instead of a clean ClassCastException, since the mismatched value reaches ASM-generated primitive accessors.

Summary of your change

In ObjectWriterAdapter.toJSONObject (core), the getInitWriter() cached writer is now only reused when objectWriterAdapter.objectClass.isAssignableFrom(fieldValue.getClass()). Otherwise the value is converted generically via JSON.toJSON(fieldValue) — the same behavior the toJSONString path already has via the typeMatch check in FieldWriterObject.getObjectWriter.

  • ObjectWriterAdapter.java: type-match guard before reusing the cached init writer.
  • Issue7853.java (new test): covers JSON.toJSON / JSON.toJSONString for the same generic wrapper serialized first with PagerDataBean<String> and then with Boolean and PagerDataBean<Integer>.

Reproduction before the fix (JDK 17):

// first call caches the PagerDataBean writer on the "result" field
JSON.toJSONString(new ResponseResult<>(new PagerDataBean<>()));
// second call with a different generic argument throws CCE / crashes the JVM
JSON.toJSON(new ResponseResult<>(Boolean.TRUE));

After the fix: {"code":0,"msg":"ok","result":true} — identical to the toJSONString output.

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.

@CLAassistant

CLAassistant commented Sep 4, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

wenshao
wenshao previously requested changes Sep 6, 2026

@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.

Not explored to full depth (tool budget reached): "agent 6c": 未编译 base 侧 ObjectWriterAdapter.java 做改前/改后字节码大小对比(head 侧偏移已证明两侧同为 >325,无跨越,故未继续)。; "agent 6c": 未运行 Issue7853.java 本身(需 JUnit 平台与 mvn ,brief 禁止在共享 worktree 内构建),改用等价探针复现了同一调用序列。; "agent 1d": 无(未因预算上限中止任何检查)。.

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

wylovelyi added a commit to wylovelyi/fastjson2 that referenced this pull request Sep 7, 2026
…alue class, for issue alibaba#7853

Addresses the review comments on alibaba#7854.

R1-1: the previous guard dereferenced ObjectWriterAdapter#objectClass, which is
null for adapters built by ObjectWriterCreator (for example writers registered
via JSON.register), so JSON.toJSON threw a NullPointerException where the merge
base returned a JSONObject. The match decision no longer touches objectClass.

R1-2: reuse now follows the same rule the write path already applies
(FieldWriterObject#getObjectWriter): exact class match, widened only for
writeUsing / Map / List. The previous isAssignableFrom check reused a superclass
writer for a subclass value and silently dropped the subclass-only fields.

R1-3 / R1-4: the check moves into a new FieldWriter#getInitWriter(Class)
overload, shared by both getInitWriter() cache-reuse sites in
ObjectWriterAdapter#toJSONObject (the UNWRAPPED_MASK branch and the main
branch), so they cannot drift apart again. This also covers the reverse
ordering, where the cached writer is a scalar writer and the current value is a
bean: the value is now converted instead of being put as-is, which made the
caller's cast throw ClassCastException.

R1-5: when the cached writer does not match, the writer is resolved from the
runtime class, which keeps the direct conversion for bean values.

R1-8: Issue7853 now covers the Base/Sub ordering (pinning the predicate) and the
Boolean-first ordering. Reverting the type check makes the new tests crash the
forked JVM, so the predicate is now observed rather than assumed.

Fixes alibaba#7853
@wylovelyi

Copy link
Copy Markdown
Contributor Author

Pushed a revised fix in commit 3703bff that addresses all six review findings from the automated review (R1-1 .. R1-5, R1-8):

  • The type-match decision is moved into a new FieldWriter#getInitWriter(Class) overload, overridden by FieldWriterObject with the same typeMatch predicate the write path already uses (exact class equality, widened only for writeUsing / Map / List).
  • Both cache-reuse sites in ObjectWriterAdapter#toJSONObject (UNWRAPPED and main) now share that single overload, so they cannot drift and the reverse cache-poisoning order is also fixed.
  • The guard no longer dereferences objectClass, so adapters with a null objectClass (e.g. registered via JSON.register) no longer throw NPE (R1-1).
  • On a mismatch the writer is resolved from the runtime class and converted directly instead of always going through the generic JSON.toJSON route (R1-5).
  • Issue7853 now pins the predicate in both orderings; reverting the type check makes the new tests crash the forked JVM, proving the predicate is observed (R1-8).

Verified locally: the full core regression suite (7991 tests) passes with 0 failures / 0 errors, and mutation testing (removing the type check) makes the new tests fail / crash the JVM, confirming they actually guard the behavior. CLA is signed. Requesting a re-review. Thanks!

wylovelyi added a commit to wylovelyi/fastjson2 that referenced this pull request Sep 14, 2026
…alue class, for issue alibaba#7853

Addresses the review comments on alibaba#7854.

R1-1: the previous guard dereferenced ObjectWriterAdapter#objectClass, which is
null for adapters built by ObjectWriterCreator (for example writers registered
via JSON.register), so JSON.toJSON threw a NullPointerException where the merge
base returned a JSONObject. The match decision no longer touches objectClass.

R1-2: reuse now follows the same rule the write path already applies
(FieldWriterObject#getObjectWriter): exact class match, widened only for
writeUsing / Map / List. The previous isAssignableFrom check reused a superclass
writer for a subclass value and silently dropped the subclass-only fields.

R1-3 / R1-4: the check moves into a new FieldWriter#getInitWriter(Class)
overload, shared by both getInitWriter() cache-reuse sites in
ObjectWriterAdapter#toJSONObject (the UNWRAPPED_MASK branch and the main
branch), so they cannot drift apart again. This also covers the reverse
ordering, where the cached writer is a scalar writer and the current value is a
bean: the value is now converted instead of being put as-is, which made the
caller's cast throw ClassCastException.

R1-5: when the cached writer does not match, the writer is resolved from the
runtime class, which keeps the direct conversion for bean values.

R1-8: Issue7853 now covers the Base/Sub ordering (pinning the predicate) and the
Boolean-first ordering. Reverting the type check makes the new tests crash the
forked JVM, so the predicate is now observed rather than assumed.

Fixes alibaba#7853
…alue class (issue alibaba#7853)

In ObjectWriterAdapter#toJSONObject, when a field's cached writer was resolved for one
generic argument (e.g. ResponseResult<PagerDataBean>) but the current value has a different
runtime type (ResponseResult<Boolean>), reusing the cached writer reads the value through
foreign field writers and throws ClassCastException (on some JDKs it even crashes the VM).
Re-resolve the writer from the runtime value class so toJSON stays consistent with
toJSONString.

Fixes alibaba#7853

(Rebased onto current main. The ClassCastException class is already handled by the
toJSONObject re-resolution logic on main; this commit adds the regression test in
issues_7800/Issue7853 covering generic-argument cache poisoning, subclass fields, and
scalar-first-then-bean ordering.)
@wylovelyi

Copy link
Copy Markdown
Contributor Author

@wenshao I rebuilt this branch on top of current main and removed the unrelated diff noise — the previous push had accidentally included 25 pom.xml version bumps, a .gitignore change and a module-info.java deletion that did not belong to this fix. The diff is now exactly the 4 files that implement it:

  • ObjectWriterAdapter.java — toJSONObject re-resolves the field writer from the runtime value class instead of reusing a cached writer resolved for a different generic argument, on both the main path and the @JSONField(unwrapped=true) path (R1-3).
  • FieldWriterObject.java — the reuse decision is centralized in a new getInitWriter(Class) overload using the same exact-class typeMatch rule the write path already applies (widened only for writeUsing / Map / List); the over-loose isTypeMatch (plain isAssignableFrom) is gone (R1-2). It never dereferences objectClass, so the null-objectClass adapter no longer throws NPE (R1-1). When the cached writer does not match, the writer is resolved from the runtime class and converted directly (R1-4 / R1-5).
  • FieldWriter.java — base getInitWriter(Class) overload returning getInitWriter().
  • Issue7853.java — covers bean→scalar, scalar→bean (reverse ordering) and the superclass/subclass field-drop case, pinning the predicate in both directions (R1-8).

This addresses every point from the previous review (R1-1 through R1-5 and R1-8). The branch is now clean and passes the issue's reproduction both directions. Could you take another look? Thanks!

@wylovelyi

Copy link
Copy Markdown
Contributor Author

@wenshao 你好,这个 PR 修复 JSON.toJSON() 在泛型字段缓存 initWriter 与运行时类型不匹配时的 ClassCastException(#7853),已含回归测试。准备好 review 了,方便时帮忙看一下,谢谢!

@wylovelyi

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (73fbe59).

The ClassCastException-on-reused-cached-writer class of bug is already handled by the toJSONObject re-resolution logic that landed on main (see the #7714 fix), so this PR's source changes are now redundant with main. The only remaining delta is the regression test issues_7800/Issue7853 covering generic-argument cache poisoning (ResponseResult<PagerDataBean> vs ResponseResult<Boolean>), subclass fields not being dropped, and scalar-first-then-bean ordering.

If the maintainers consider #7853 sufficiently covered by main, this PR can be closed as completed; otherwise the added regression test is ready to merge as-is. CI should now run green since the branch is conflict-free.

@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.

Not explored to full depth (tool budget reached): "agent reverse-audit (round 2)": repo-wide sweep for an existing subclass-cached-first → superclass-value test outside the two suites read in full (Issue7853.java, Issue7714.java — every value-…; "agent reverse-audit (round 1)": resolving R2-1's open question — whether any other committed suite test pins the un-primed JSON.toJSON fallback arm — requires per-test isolation runs (a whol…; "agent 1a": re-running Issue7853 on JDKs other than 8 (only JDK 8 was exercised; with the fix in place the assertions are JDK-independent output strings, so this is corrobo….

— glm-5.3-flash@89f03b58 via Qwen Code /review (v0.25.0)

Comment on lines +91 to +93
Holder baseHolder = new Holder();
baseHolder.value = base;
assertEquals("{\"value\":{\"a\":\"A\"}}", JSON.toJSONString(baseHolder));

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.

[Suggestion] R2-4: the tests pin only the parent-cached-then-subclass direction of the isTypeMatch predicate. No test in this PR or in Issue7714 caches a subclass writer and then writes a superclass value through the same field, so a reversed-receiver mutation of the assignability check (valueClass.isAssignableFrom(initValueClass) instead of initValueClass.isAssignableFrom(valueClass)) passes everything while reintroducing a production crash: with Sub cached first and a Base value written second, both the write path and the tree path reuse Sub's writer for the Base instance, and Sub's b accessor fails on it. An argument-order flip is exactly the class of edit the test's own javadoc warns against, in the direction the current tests cannot see.

Witness (probe on this commit):

HEAD + proposed test:   testsStarted=4 passed=4 failed=0
MUTANT + proposed test: testsStarted=4 passed=3 failed=1
  testSubclassCachedFirstThenSuperclassValue :: java.lang.ClassCastException:
  class ...Issue7853$Base cannot be cast to class ...Issue7853$Sub
Full-suite sweep (8,150 tests x 2 arms): failing-test sets identical,
0 mutation-caused failures — no committed test pins this direction.

Suggested fix — add a case pinning the reverse direction, with a fresh holder class: the field-writer cache binds once per holder class, process-wide, and never resets (FieldWriterObject.java:199-201), so reusing Holder would inherit the binding testSubclassFieldsAreNotDropped left behind — the same isolation pattern ScalarFirstResult already applies:

public static class SubFirstHolder {
    public Object value;
}

@Test
public void testSubclassCachedFirstThenSuperclassValue() {
    SubFirstHolder subFirst = new SubFirstHolder();
    subFirst.value = new Sub();      // binds initValueClass = Sub
    JSON.toJSONString(subFirst);

    SubFirstHolder baseSecond = new SubFirstHolder();
    baseSecond.value = new Base();   // must re-resolve by Base, not reuse Sub's writer
    assertEquals("{\"value\":{\"a\":\"A\"}}", JSON.toJSONString(baseSecond));
    assertEquals("{\"value\":{\"a\":\"A\"}}", JSON.toJSON(baseSecond).toString());
}

The test is green at this commit and reds under the flipped-receiver mutation; the toJSONString assertion is the one that kills the mutant, because on ASM-generated writers the tree path's Unsafe accessors read out-of-class fields as null silently instead of throwing.

— glm-5.3-flash@89f03b58 via Qwen Code /review (v0.25.0)

Comment on lines +84 to +85
* writeUsing / Map / List). A looser isAssignableFrom check would reuse the superclass writer
* for a subclass value and silently drop the subclass-only field.

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.

[Suggestion] R2-5: this javadoc advertises the full rule ("exact class match, widened only for writeUsing / Map / List"), but the tests pin only the exact-match arm — the widening arms of FieldWriterObject.isTypeMatch are unpinned anywhere in the repo, so a regressed arm would ship with every gate green.

Witness (probe on this commit):

writeUsing arm: removing `writeUsing && initValueClass.isAssignableFrom(valueClass)`:
  INTACT toJSONString: {"value":"MASKED"}
  MUTANT toJSONString: {"value":{"a":"A","b":"B"}}  <- custom writer silently bypassed
The five writeUsing fixtures in core tests (Issue1524 x4, SortFieldNamesRegistryTest x1)
each use exactly one value class, so none exercises the widening.

Suggested fixes, two small tests:

(1) pin the writeUsing widening with a toJSONString pair — prime a base-class value, then write a subclass value, assert the custom writer's output. A toJSON assertion cannot pin this arm: toJSON stores a writeUsing field's value raw (pre-existing main behavior).

(2) pin the primitive-boxing arm, which becomes reachable when a field-level custom writer is configured on a primitive-typed property (the creator then pre-binds initValueClass = int.class, making this the only arm that honors the custom writer):

public static class IntMaskWriter implements ObjectWriter {
    public void write(JSONWriter w, Object o, Object name, Type type, long features) {
        w.writeString("MASKED");
    }
}

public static class PrimitiveHolder {
    @JSONField(writeUsing = IntMaskWriter.class)
    public int status = 3;
}

@Test
public void testPrimitiveWriteUsingHonored() {
    assertEquals("{\"status\":\"MASKED\"}", JSON.toJSONString(new PrimitiveHolder()));
}

Both tests go red if the corresponding arm is removed and are green at this commit.

— glm-5.3-flash@89f03b58 via Qwen Code /review (v0.25.0)

@wenshao
wenshao dismissed their stale review October 17, 2026 08:21

Dismissing stale review: the findings were against the production change in ObjectWriterAdapter.java, which is no longer in this PR -- it is now test-only (Issue7853 regression coverage; the production fix landed on main via #7739). CI green on head. Agent-verified against the current diff.

@wenshao
wenshao merged commit 4c6e1c0 into alibaba:main Oct 17, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants