Skip to content

Fix Optional target not using static builder factory method#4058

Open
jmwbRyDWLeNsvtzrihGoY wants to merge 1 commit into
mapstruct:mainfrom
jmwbRyDWLeNsvtzrihGoY:main
Open

Fix Optional target not using static builder factory method#4058
jmwbRyDWLeNsvtzrihGoY wants to merge 1 commit into
mapstruct:mainfrom
jmwbRyDWLeNsvtzrihGoY:main

Conversation

@jmwbRyDWLeNsvtzrihGoY
Copy link
Copy Markdown

When the return type of a mapping method is Optional and T uses a
Lombok @builder, MapStruct was generating new T.TBuilder() instead of
T.builder(). This caused a compilation error when the mapper and T are
in different packages, because the generated builder constructor is
package-private.

Root cause: ObjectFactoryMethodResolver.getBuilderFactoryMethod(Method, BuilderType)
was passing method.getReturnType() (i.e. Optional) to the overload
that checks assignability. Since T is not assignable to Optional,
the check failed and returned null, causing the fallback to use the
constructor directly.

Fix: Unwrap the Optional type before passing to the overload.

Fixes #4046

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.

Optional target does not use Lombok builder correctly

1 participant