Repository navigation
#3896 Use custom int to enum mapping method for nested source property - #4146
Open
henriquejsza wants to merge 1 commit into
Open
henriquejsza wants to merge 1 commit into
henriquejsza wants to merge 1 commit into
Conversation
…e property When a custom mapping method with a primitive parameter (e.g. `GroupState toGroupState(int)`) was used for a nested source property, the value was extracted through a null-safe helper method returning the boxed type (`Integer`). Since mapstruct#2239 the method matcher rejects a boxed source for a primitive parameter, so the custom method was skipped and the built-in ordinal based `int -> enum` conversion was used instead, producing wrong results (or an `ArrayIndexOutOfBoundsException`). The method matcher now allows a boxed source type to match a primitive parameter as a fall-back, applied only when no strict match exists so an exact (e.g. `Integer`) method is still preferred. The generated invocation is guarded with a null check before unboxing to avoid a `NullPointerException`, matching the 1.5 behaviour. Signed-off-by: henriquejsza <henriquejsza@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.
Fixes #3896
When a custom mapping method with a primitive parameter (for example
GroupState toGroupState(int)) is used for a nested source property, the value isextracted through a null-safe helper method that returns the boxed type (
Integer).Since #2239 the method matcher rejects a boxed source for a primitive parameter, so
the custom method was skipped and the built-in ordinal based
int -> enumconversion(
GroupState.values()[state]) was used instead. That produces wrong results, or anArrayIndexOutOfBoundsExceptionwhen the int value is not a valid ordinal.This matches the behaviour filiphr described in the thread: the method matcher should
pick a primitive-parameter method even when the source is an object such as
Integer,and then do the null check like 1.5 did.
Changes:
MethodMatchercan now match a boxed source type against a primitive parameter. Thisis a fall-back used only when no strict match exists, so an exact method (for example
one taking
Integer) is still preferred (keeps Autoboxing not considered when looking for a matching method #205 working).TypeSelectorruns a strict pass first and only retries with the relaxed matchingwhen the strict pass finds nothing.
source no longer causes a
NullPointerException(already asserted by the existingNullCheckTestNull-check missing for auto-unboxing when using property mapping methods #231 case).Added
Issue3896Testin the bugs package reproducing the nestedint -> enummapping.