Visitar URL original
#3896 Use custom int to enum mapping method for nested source property by henriquejsza · Pull Request #4146 · mapstruct/mapstruct · GitHub
Skip to content

#3896 Use custom int to enum mapping method for nested source property - #4146

Open
henriquejsza wants to merge 1 commit into
mapstruct:mainfrom
henriquejsza:3896-int-enum-nested-method
Open

henriquejsza wants to merge 1 commit into
mapstruct:mainfrom
henriquejsza:3896-int-enum-nested-method

Conversation

@henriquejsza

Copy link
Copy Markdown

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 is
extracted 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 -> enum conversion
(GroupState.values()[state]) was used instead. That produces wrong results, or an
ArrayIndexOutOfBoundsException when 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:

Added Issue3896Test in the bugs package reproducing the nested int -> enum mapping.

…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

custom int -> enum mapper not used for nested property

1 participant