Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
No issues found. LGTM! ✅ — gpt-5.5 via Qwen Code /review
wenshao
left a comment
There was a problem hiding this comment.
Reviewed. Suggestions are inline.
— qwen3.8-max-preview via Qwen Code /review
| if (object != null && fieldValues != null) { | ||
| for (Map.Entry<Long, Object> entry : fieldValues.entrySet()) { | ||
| FieldReader fieldReader = defaultReader.getFieldReader(entry.getKey()); |
There was a problem hiding this comment.
[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.
| 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
| if (object == null && seeAlsoDefault != null && seeAlsoDefault != Void.class) { | ||
| ObjectReader defaultReader = jsonReader.getContext().getObjectReader(seeAlsoDefault); |
There was a problem hiding this comment.
[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.
| 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
Problem
When deserializing JSON without
@typefield into an interface annotated with@JSONType(seeAlso = {Dog.class}, seeAlsoDefault = Dog.class), aNullPointerExceptionis thrown:Reproduction:
Root Cause
ObjectReaderSeeAlso.createInstance()returns null for interfaces (no creator supplier). When JSON has no@typefield:LinkedHashMap(since object is null)fieldReader.accept(null, value)The
seeAlsoDefaultclass is never consulted as a fallback.Fix
After parsing completes, if
objectis still null andseeAlsoDefaultis configured:ObjectReaderfor theseeAlsoDefaultclassThis correctly falls back to the default type when
@typeis absent, matching the documented behavior ofseeAlsoDefault.Tests
The existing test file
Issue3540.java(3 test cases) already covers this scenario:exceptUseSeeAlsoDefault— JSON without@type→ should use default classexceptUseSeeAlsoDefault2— JSON with empty@type→ should use default classuseTypeForSeeAlso— JSON with explicit@type→ works correctlyAll 3 tests pass. Additionally,
SeeAlsoTest,SeeAlsoTest3, andSeeAlsoTest5pass with no regressions.Impact
Only affects deserialization with
seeAlsoDefaulton interfaces/abstract classes when@typeis missing. No impact on normal deserialization paths.Fixes #3540