Fix OptionalToObjectConverter applicability check#36913
Open
junhyeong9812 wants to merge 1 commit into
Open
Conversation
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>
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 an
Optionaland delegates the contained value to theConversionService.Problem
matches()derived the contained type withTypeDescriptor.getElementTypeDescriptor():TypeDescriptor.getElementTypeDescriptor()only resolves element types for arrays, streams andcollections, so for an
Optionalit returnsnull.ConversionUtils.canConvertElements()thentreats a
nullsource 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, mirroringthe sibling
ObjectToOptionalConverter. A rawOptional(or otherwise unresolved element type)stays permissive, since the element type is unknown. A regression test is added to
DefaultConversionServiceTests.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 that
canConvert/matchesis a predicate callers rely on to decide whether toattempt a conversion or take a different path. Returning an incorrect
truedefeats that — itturns what should be a clean negative answer into a deferred exception. Restoring an accurate
matches()lets callers get the correct answer up front. Impact is bounded, since it onlyaffects
Optionalused as a conversion source whose element type is not convertible to thetarget.