Skip to content

fix: support autoType when keyType is String for Map typed reader - #7964

Open
liango2 wants to merge 2 commits into
alibaba:mainfrom
liango2:fix-mapstring-autotype
Open

liango2 wants to merge 2 commits into
alibaba:mainfrom
liango2:fix-mapstring-autotype

Conversation

@liango2

@liango2 liango2 commented Nov 19, 2026

Copy link
Copy Markdown

What this PR does / why we need it?

When deserializing a Map with keyType = String, the inner @type field was ignored even if SupportAutoType feature was enabled or ContextAutoTypeBeforeHandler was provided.
The keyType == String.class branch missed the auto-type condition check which already existed in the else branch. This change aligns the condition so auto-type works consistently for String-key typed maps.

Summary of your change

Add the missing auto-type check condition in ObjectReaderImplMapTyped for the keyType == String.class path:
|| jsonReader.getContext().getContextAutoTypeBeforeHandler() != null

Add a test case to verify auto-type deserialization works for typed Map with String key.

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 Nov 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@liango2
liango2 force-pushed the fix-mapstring-autotype branch from 00e6e50 to 5cdb1fd Compare November 19, 2026 11:35
@liango2

liango2 commented Nov 25, 2026 •

Copy link
Copy Markdown
Author

Added a second commit, 5656dbc99. Two changes.

1. Use the helper the project already has. The first commit wrote the check by hand: isEnabled(SupportAutoType) || getContextAutoTypeBeforeHandler() != null. JSONReader already has isSupportAutoTypeOrHandler(long features) for exactly this, and ObjectReaderAdapter calls it. Using it keeps the rule in one place.

There is also a small semantic reason. The old line tested contextFeatures, which is the context features OR the features argument passed in. isEnabled reads only the context features, so the hand-written version describes a slightly narrower rule than before. I looked for a way to hit that gap and could not find one a user can reach: @JSONField has no features attribute in this version, so there is no per-field switch. So I am not calling it a bug, just a place where the shorter form does not say the same thing as the old code.

2. Cover the feature path. The first commit only tested the autoTypeFilter route. Auto type also turns on through the SupportAutoType feature alone, so there is now a second test for that.

Evidence the fix does something.

  • On 460d2e4b4 (upstream main) with the new test: JSONException: expect ':', but 44, offset 21. The reader treats the inner @type as a normal key and then chokes on the comma.
  • On 5656dbc99: both cases parse, varsMap is a LinkedHashMap.
  • com.alibaba.fastjson2.reader.*Test: 396 tests, 0 failures. Checkstyle clean.

One thing outside my control: the CI run on this PR is at action_required, so no job has run yet. A maintainer needs to approve the workflow before the matrix means anything.

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.

2 participants