Repository navigation
#3704 Fix NPE when forging a mapping to an Optional target with a wildcard type argument - #4139
Open
sharanggupta wants to merge 1 commit into
Open
sharanggupta wants to merge 1 commit into
sharanggupta wants to merge 1 commit into
Conversation
… with a wildcard type argument A target setter such as `setDate(Optional<? extends Nested>)` made `MappingBuilderContext#canGenerateAutoSubMappingFor` dereference the `TypeElement` of the wildcard `Optional` base type, which is `null`, and the processor crashed with an internal `NullPointerException`. Resolve the wildcard bound before forging (as is already done for `Map` targets) so that an `Optional<Nested>` sub-mapping method is generated and assigned to the wildcard setter, and make the exclusion check null-safe.
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 #3704
Problem — With a target setter like
setDate(Optional<? extends Nested>)(common for Immutables-generated builders) and a sourceOptional<SourceNested>, the processor crashes onmainwithInternal error in the mapping processor: java.lang.NullPointerExceptioninDefaultMappingExclusionProvider.isExcluded, called fromMappingBuilderContext#canGenerateAutoSubMappingFor: theOptionalbase type is a wildcard whoseTypeElementisnull. On 1.6.x the same code reported "Can't map property … Optional<? extends …>" instead of generating a mapping.Regression — The NPE site was introduced with the
Optionalsupport from #3971 (#674, 2026-01-30) and ships in 1.7.0.Beta1 and Beta2; on 1.4.2 and 1.6.0 the same mapper produced the "Can't map property" diagnostic instead of crashing. It would be good to have this in 1.7.0.RC1.Fix —
PropertyMapping#forgeMapping(SourceRHS)now forges the sub-mapping againsttargetType.withoutBounds()forOptionaltargets (the same approachforgeMapMappingalready uses forMap<? extends K, ? extends V>), so a methodOptional<Nested> nestedOptionalToNestedOptional(Optional<SourceNested>)is generated and assigned to the wildcard setter.canGenerateAutoSubMappingForis also made null-safe for theOptionalbranch, mirroring the existing non-Optionalbranch.Tests —
Issue3704Test(bugs/_3704): present/emptyOptionalmapped through a wildcard setter with fixture comparison, plus the exact scenario from the issue thread (overloadedsetDate+ generic<T> Optional<T> wrapOptional(T))../mvnw -pl processor verifyis green (3649 tests, checkstyle, license, forbidden-apis). Reverting the two production changes makesIssue3704Testfail 6/6 (javac and Eclipse JDT) with the reported NPE, so the test is load-bearing.Design note — The wildcard bound is resolved in
forgeMapping, consistent with howforgeMapMappinghandlesMap<? extends K, ? extends V>. If you would rather have the check closer to theOptionalhandling (Type#getOptionalBaseType()/canGenerateAutoSubMappingFor), I am happy to move it.