Skip to content

Fix Optional target not using static builder factory method - #4058

Merged
filiphr merged 1 commit into
mapstruct:mainfrom
jmwbRyDWLeNsvtzrihGoY:main
Jun 5, 2026
Merged

Fix Optional target not using static builder factory method#4058
filiphr merged 1 commit into
mapstruct:mainfrom
jmwbRyDWLeNsvtzrihGoY:main

Conversation

@jmwbRyDWLeNsvtzrihGoY

Copy link
Copy Markdown
Contributor

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

@filiphr
filiphr merged commit cabe266 into mapstruct:main Jun 5, 2026
9 checks passed
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

2 participants