#4111 Fix Optional<Map>/Optional<Collection> source properties calling Optional#get() unguarded - #4115
Open
seonwooj0810 wants to merge 1 commit into
Conversation
…es calling Optional#get() unguarded Map and Collection source properties wrapped in Optional went through CollectionAssignmentBuilder's bare SetterWrapperForCollectionsAndMaps, which evaluates the assignment (including the trailing Optional#get()) unconditionally, unlike scalar properties which are guarded by an isPresent() check. An empty Optional therefore threw NoSuchElementException instead of leaving the target property unset. setterWrapperNeedsSourceNullCheck now requests a null-check wrapper for Optional-typed sources, and handleLocalVarNullCheck guards the local var assignment with isPresent() when the source is an Optional, mirroring the existing handleSourceReferenceNullCheck behavior for scalars.
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.
Fixes #4111
Root cause
For a source property typed
Optional<T>, scalar properties are guarded with anisPresent()check before the generated code callsOptional#get()(viahandleSourceReferenceNullCheckinCommonMacros.ftl).Optional<Map<K, V>>/Optional<Collection<E>>source properties don't go through that macro though —CollectionAssignmentBuilder.setterWrapperNeedsSourceNullCheck()only requested a null-check wrapper when there's a source presence checker,NullValueCheckStrategy.ALWAYS, or aDIRECTassignment, so an Optional-wrapped collection/map fell through to the bareSetterWrapperForCollectionsAndMaps, which calls the assignment (source.getAttributes().get()) unconditionally. Mapping a source whoseOptionalis empty throwsNoSuchElementExceptioninstead of leaving the target property unset, exactly as described in the issue.Fix
CollectionAssignmentBuilder.setterWrapperNeedsSourceNullCheck()now also returnstruewhen the source type is anOptional, so theSetterWrapperForCollectionsAndMapsWithNullCheckwrapper (and itshandleLocalVarNullCheckmacro) is used instead of the bare setter wrapper.handleLocalVarNullCheckinCommonMacros.ftlgained an<#elseif sourceType.optionalType>branch that guards the local-var assignment withOptional#isPresent()before callingOptional#get(), mirroring the existing scalar behavior inhandleSourceReferenceNullCheck.Tests
Added
processor/src/test/java/org/mapstruct/ap/test/bugs/_4111/with a mapper that hasOptional<String>,Optional<Map<String, String>>andOptional<List<String>>source properties mapped to plain target properties:emptyOptionalMapAndCollectionShouldNotThrow— reproduces the reported bug: fails withNoSuchElementExceptionbefore the fix, target properties are leftnullafter the fix.presentOptionalMapAndCollectionShouldBeMapped— confirms present Optionals still map through correctly.Verification done:
gh pr list --searchandclosedByPullRequestsReferences/linkedBranchesGraphQL query — both empty)..java+.ftltemplate), not docs/config.main(211b2be) by running the new test against the pre-fix code — both new-test iterations failed withNoSuchElementExceptionat the exact reported call site; after the fix,./mvnw -pl processor -am test -Dtest=Issue4111Testpasses (4/4, JDK + Eclipse compiler).optional.*andcollection.*test packages (OptionalSimpleTest,OptionalDifferentTypesTest,OptionalNestedTest,OptionalNullCheckAlwaysTest,CollectionMappingTest,MapMappingTest,AdderTest, etc. — ~24 test classes) after the fix: all green, no regressions../mvnw -pl processor checkstyle:checkpasses.