Fix OptionalToObjectConverter applicability check - #36913
Merged
sbrannen merged 1 commit intoAug 20, 2026
Merged
Conversation
sbrannen
requested changes
Aug 14, 2026
sbrannen
left a comment
Member
There was a problem hiding this comment.
Good catch, and thanks for submitting the PR to fix it! 👍
Can you please add tests that cover the Optional<?> and Optional<? extends Number> use cases in addition to the raw Optional case?
OptionalToObjectConverter applicability check
OptionalToObjectConverter.matches() used TypeDescriptor.getElementTypeDescriptor(), which returns null for an Optional (element types are only resolved for arrays, streams and collections). ConversionUtils.canConvertElements() then treats a null source element type as "maybe" and returns true unconditionally, so ConversionService.canConvert(Optional<X>, target) reported true even when X is not convertible to the target -- a violation of the canConvert contract, since the subsequent conversion fails. Resolve the Optional's element type from its generic and check it against the target, mirroring ObjectToOptionalConverter. A raw or otherwise unresolved element type remains permissive. Signed-off-by: junhyeong9812 <pickjog@gmail.com>
junhyeong9812
force-pushed
the
fix/optional-to-object-converter-matches
branch
from
August 14, 2026 15:14
563515d to
3d8ec1d
Compare
Contributor
Author
|
Thanks for the review! I have added the requested tests, plus two closely related cases:
The wildcard descriptors are built from fixture fields via |
sbrannen
approved these changes
Aug 15, 2026
sbrannen
left a comment
Member
There was a problem hiding this comment.
Thanks for making the requested changes.
Member
|
This has been merged into Thanks |
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.
Overview
OptionalToObjectConverter(registered byDefaultConversionServiceforOptional->Object) unwraps anOptionaland delegates the contained value to theConversionService.Problem
matches()derived the contained type withTypeDescriptor.getElementTypeDescriptor():TypeDescriptor.getElementTypeDescriptor()only resolves element types for arrays, streams and collections, so for anOptionalit returnsnull.ConversionUtils.canConvertElements()then treats anullsource element type as "maybe" and returnstrueunconditionally. As a resultmatches()always matched, andConversionService.canConvert(Optional<X>, target)returnedtrueeven whenXcannot be converted totarget— violating thecanConvertcontract (atrueresult impliesconvertis capable of converting), since the subsequent conversion fails.For example, with the out-of-the-box
DefaultConversionService:Fix
Resolve the
Optional's element type from its generic and check it against the target, mirroring the siblingObjectToOptionalConverter. A rawOptional(or otherwise unresolved element type) stays permissive, since the element type is unknown. A regression test is added toDefaultConversionServiceTests.Note on impact
This is a contract-correctness fix rather than a fix for data corruption: the conversion itself already fails loudly (the subsequent
convertthrows), so nothing is silently mis-converted. The value is thatcanConvert/matchesis a predicate callers rely on to decide whether to attempt a conversion or take a different path. Returning an incorrecttruedefeats that — it turns what should be a clean negative answer into a deferred exception. Restoring an accuratematches()lets callers get the correct answer up front. Impact is bounded, since it only affectsOptionalused as a conversion source whose element type is not convertible to the target.