Skip to content

fix: seeAlsoDefault NPE when @type is missing - #4037

Open
daguimu wants to merge 1 commit into
alibaba:mainfrom
daguimu:fix/seeAlsoDefault-npe-issue3540
Open

daguimu wants to merge 1 commit into
alibaba:mainfrom
daguimu:fix/seeAlsoDefault-npe-issue3540

Conversation

@daguimu

@daguimu daguimu commented Mar 25, 2026

Copy link
Copy Markdown

Problem

When deserializing JSON without @type field into an interface annotated with @JSONType(seeAlso = {Dog.class}, seeAlsoDefault = Dog.class), a NullPointerException is thrown:

NullPointerException
  at FieldReaderStringMethod.accept(FieldReaderStringMethod.java:88)
  at ObjectReaderSeeAlso.readObject(ObjectReaderSeeAlso.java:356)

Reproduction:

@JSONType(seeAlso = Dog.class, seeAlsoDefault = Dog.class)
interface IAnimal { ... }

// Throws NPE - no @type in JSON
JSON.parseObject("{\"name\":\"tom\",\"age\":18}", IAnimal.class);

Root Cause

ObjectReaderSeeAlso.createInstance() returns null for interfaces (no creator supplier). When JSON has no @type field:

  1. Field values are buffered in a LinkedHashMap (since object is null)
  2. After the loop, the code tries to apply buffered values to the still-null object
  3. NPE at fieldReader.accept(null, value)

The seeAlsoDefault class is never consulted as a fallback.

Fix

After parsing completes, if object is still null and seeAlsoDefault is configured:

  1. Get the ObjectReader for the seeAlsoDefault class
  2. Create an instance using that reader
  3. Apply buffered field values using the default class's field readers

This correctly falls back to the default type when @type is absent, matching the documented behavior of seeAlsoDefault.

Tests

The existing test file Issue3540.java (3 test cases) already covers this scenario:

  • exceptUseSeeAlsoDefault — JSON without @type → should use default class
  • exceptUseSeeAlsoDefault2 — JSON with empty @type → should use default class
  • useTypeForSeeAlso — JSON with explicit @type → works correctly

All 3 tests pass. Additionally, SeeAlsoTest, SeeAlsoTest3, and SeeAlsoTest5 pass with no regressions.

Impact

Only affects deserialization with seeAlsoDefault on interfaces/abstract classes when @type is missing. No impact on normal deserialization paths.

Fixes #3540

When deserializing JSON without @type field into an interface annotated
with @jsontype(seeAlsoDefault = X.class), ObjectReaderSeeAlso throws
NullPointerException because:
1. createInstance() returns null for interfaces (no creator)
2. Field values are buffered but object remains null
3. Applying buffered values to null object causes NPE

Add fallback: after parsing, if object is still null and seeAlsoDefault
is set, create instance via the default class reader and apply buffered
field values using its field readers.

Fixes alibaba#3540

@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! ✅ — gpt-5.5 via Qwen Code /review

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

— qwen3.8-max-preview via Qwen Code /review

Comment on lines +357 to +359
if (object != null && fieldValues != null) {
for (Map.Entry<Long, Object> entry : fieldValues.entrySet()) {
FieldReader fieldReader = defaultReader.getFieldReader(entry.getKey());

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] The field-value application loop is unreachable dead code. When seeAlsoDefault is set, the autoType branch condition at line ~248 is always true (the constructor hardcodes SupportAutoType.mask into features), so every field enters the type-discrimination branch and the method returns early from within the loop. fieldValues is therefore always null when this block is reached — the inner loop never executes. — Concrete cost: a developer reading this code would believe field values are applied during the seeAlsoDefault fallback, but they never are. The dead code adds misleading complexity for future maintainers.

Suggested change
if (object != null && fieldValues != null) {
for (Map.Entry<Long, Object> entry : fieldValues.entrySet()) {
FieldReader fieldReader = defaultReader.getFieldReader(entry.getKey());
if (object != null && fieldValues != null) {
for (Map.Entry<Long, Object> entry : fieldValues.entrySet()) {
FieldReader fieldReader = defaultReader.getFieldReader(entry.getKey());
if (fieldReader != null) {
fieldReader.accept(object, entry.getValue());
}
}
fieldValues = null;
}

Consider removing this dead loop entirely (keeping only the createInstance call), or adding a comment explaining the precondition under which fieldValues would be non-null here.

— qwen3.8-max-preview via Qwen Code /review

Comment on lines +353 to +354
if (object == null && seeAlsoDefault != null && seeAlsoDefault != Void.class) {
ObjectReader defaultReader = jsonReader.getContext().getObjectReader(seeAlsoDefault);

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] The createInstance fallback — the only live code in this new block — has zero test coverage. Because SupportAutoType is hardcoded in the reader's features, the autoType branch diverts every field and returns early when seeAlsoDefault is set. This block is only reachable when the JSON object is empty ({}), and no existing test parses {} against a seeAlsoDefault-annotated type. Mutation: delete the entire 16-line block and all tests still pass green. — Failure scenario: a regression removing this fallback would cause JSON.parseObject("{}", IAnimal.class) to silently return null instead of a default instance, with no test to catch it.

Suggested change
if (object == null && seeAlsoDefault != null && seeAlsoDefault != Void.class) {
ObjectReader defaultReader = jsonReader.getContext().getObjectReader(seeAlsoDefault);
if (object == null && seeAlsoDefault != null && seeAlsoDefault != Void.class) {
ObjectReader defaultReader = jsonReader.getContext().getObjectReader(seeAlsoDefault);

Suggested test:

@Test
public void exceptUseSeeAlsoDefaultEmptyObject() {
    IAnimal animal = JSON.parseObject("{}", IAnimal.class);
    assertEquals(Dog.class, animal.getClass());
}

— qwen3.8-max-preview via Qwen Code /review

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] seeAlsoDefault can't used for default

2 participants