Skip to content

fix: StringToAny to Boolean rejects TRUE/True/T/Y spellings, for issue #7842 - #7848

Open
WLLTB wants to merge 1 commit into
alibaba:mainfrom
WLLTB:fix-7842-string-to-boolean
Open

fix: StringToAny to Boolean rejects TRUE/True/T/Y spellings, for issue #7842#7848
WLLTB wants to merge 1 commit into
alibaba:mainfrom
WLLTB:fix-7842-string-to-boolean

Conversation

@WLLTB

@WLLTB WLLTB commented Sep 3, 2026

Copy link
Copy Markdown

Fixes #7842

What this PR does / why we need it?

TypeUtils.castToJavaBean("TRUE", Boolean.class) returns false. All String-to-Boolean conversions go through StringToAny, whose boolean branch only accepted lowercase "true", while ToBoolean (used for every other source type) accepts TRUE/True/T/Y/YES and friends — so the String source type could never reach the richer logic.

Summary of your change

  • StringToAny: boolean branch now accepts "true"/"T"/"Y" case-insensitively (matching ToBoolean's single-letter set and fastjson1's castToBoolean spellings); unrecognized strings keep the existing lenient FALSE fallback instead of throwing, since this converter is shared by JSONObject/JSONArray/JSONPath/list conversion paths.
  • Added regression test core/src/test/java/com/alibaba/fastjson2/issues_7800/Issue7842.java.

Note: "1" is deliberately left as falseJSONObjectTest.test_invoke pins that behavior, and ToBoolean doesn't accept "1" either. Happy to flip it if maintainers prefer fastjson1's castToBoolean semantics there.

Verified: core 7992 passed (standard + -Dfastjson2.creator=reflect), fastjson1-compatible 1355 passed, root validate clean.

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. (No docs impact.)

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]TypeUtils.castToJavaBean对Boolean支持的问题

1 participant