Repository navigation
fix(java): report custom codec read failures as ForyJsonException - #4228
Open
pavel-ptashyts wants to merge 1 commit into
Open
pavel-ptashyts wants to merge 1 commit into
pavel-ptashyts wants to merge 1 commit into
Conversation
An unchecked exception thrown by a custom codec while fromJson reads a value escaped the root operation with its own type, unlike failures of other application code invoked during a read (any-setters, creators, validators), which are already reported as ForyJsonException. Callers that handle malformed input by catching ForyException then saw a raw NumberFormatException or a third-party parser exception. Wrap non-Fory runtime exceptions once around the root value read on the Latin1, UTF-16 and UTF-8 paths, keeping the original exception as the cause and naming the JSON position. ForyException subclasses and Errors propagate unchanged; codec, property and generated read paths get no per-value exception handling. Write paths and the readUtf8/writeUtf8 token operations keep propagating application exceptions unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pavel-ptashyts
requested review from
PragmaTwice,
chaokunyang and
theweipeng
as code owners
November 17, 2026 14:37
Contributor
Author
|
AI review artifact: Fory-guided reviewer (final round)
|
Contributor
Author
|
AI review artifact: independent general reviewer (final round)
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why?
An unchecked exception thrown by a custom codec while
ForyJson.fromJsonreads a value reachesthe caller with its own type, for example a raw
NumberFormatExceptionor an exception from athird-party parser the codec delegates to. Other application code that Fory JSON invokes during a
read (
@JsonAnySetter,@JsonCreator,@JsonValidator) is already reported asForyJsonExceptionwith the original cause, and built-in scalar codecs convert JDK parser failuresthe same way. Callers that turn malformed input into a client error by catching
ForyExceptiontherefore miss codec failures and answer with a server error instead. Details in #4227.
What does this PR do?
ForyJson(Latin1, UTF-16 and UTF-8,each for
ClassandTypeRefroots) put onetry/catcharound the root value read. ARuntimeExceptionthat is not aForyExceptionbecomesForyJsonException("Cannot read JSON value at JSON position N: <cause>", cause). EveryfromJsonoverload goes through these helpers, including
InputStream, byte ranges andJsonStreamDecoderframes.ForyExceptionsubclasses (including a codec's ownForyJsonException) andErrors propagate as before. Root type resolution, argument validation and the root result caststay outside the
try. Write paths and the token-levelreadUtf8(..., Function)/writeUtf8(Consumer)operations keep their documented "unchecked exceptions propagate unchanged"contract, because there the operation is the caller's own code.
only change on the success path is an exception-table entry in each root helper. The failure
message is built in a separate cold static method.
ForyJsonExceptionJavadoc (it previously said user exceptions "may retain their owntype") and
docs/json/custom-codecs.md.JsonCodecAnnotationTest#codecFailure, in both codegen modes: a throwingAbstractJsonValueCodecas the root value and as a property, throughString(ASCII Latin1,non-ASCII Latin1 and UTF-16),
byte[], byte range,InputStreamandTypeRefroots; theproperty case asserts the generated codec is used when codegen is on. It also checks that a
codec's own
ForyJsonExceptionand anErrorpropagate as the same instance. The test fails onapache/mainwith the rawNumberFormatException.Kept by decision:
toJsonfailures come from the application's own values, not fromuntrusted input, so they do not affect how callers classify malformed requests. Happy to extend
the same rule to writes if maintainers prefer symmetry.
would need per-property work on the hot path. The JSON position is available for free.
Related issues
fromJsonunwrapped #4227AI Contribution Checklist
yesyes, I included a completed AI Contribution Checklist in this PR description and the requiredAI Usage Disclosure.yes, my PR description includes the requiredai_reviewsummary and screenshot evidence or equivalent persisted links of the final clean AI review results from both fresh reviewers described inAI_POLICY.md, the Fory-guided reviewer and the independent general reviewer, on the current PR diff or current HEAD after the latest code changes.Completed checklist from
AI_POLICY.mdsection 9:yesAI Usage Disclosureblock below.AGENTS.mdand.agents/ci-and-pr.md, and one independent general reviewer in a separate clean-context review session that was not pointed to.agents/ci-and-pr.mdor any copied Fory-specific review checklist.Verification
JDK 21.0.2, Windows 11 x86_64, base
apache/mainc8bb610:java/:mvn -pl fory-json spotless:check checkstyle:check: clean.java/:mvn -pl fory-json test: 1335 tests, 0 failures exceptJsonGuavaOptionalDependencyTest.buildWithoutGuava, which could not start its child JVM on thetest machine (
os::commit_memory ... paging file is too small); rerun alone once memory was available: 2 tests (both codegen modes), 0 failures.JsonCodecAnnotationTest#codecFailurefails onapache/main(rawNumberFormatException) andpasses with the change, in both codegen modes.
docs/json/custom-codecs.md: prettier clean.Does this PR introduce any user-facing change?
fromJsonnow reports uncheckedcustom-codec failures as
ForyJsonExceptionwith the original exception as the cause.