How can I map properties conditionally with MapStruct 1.2?
You can't really do that with MapStruct. The @ValueMapping
annotation is for mapping of Enum
(s).
In order to achieve what you are looking for you would need to do that in @BeforeMapping
or @AfterMapping
.
For example you can do something like:
@Mapper
public interface JiraKpmMapper {
@BeforeMapping
default void beforeMapping(@MappingTarget MyTarget target, MySource source) {
if (source.getPropY().equals("ABC") {
target.setPropX("V01.123.456.AB");
}
}
@Mapping(target = "propX", ignore = true) // This is now mapped in beforeMapping
MyTarget source2Target(final MySource mySource);
}
Your custom mapper then should have an @AfterMapping
. Where you would convert the propX
into your class. You can even do this as part of the @BeforeMapping
I wrote and directly create your class (or invoke the method that does the conversion from String
into a class)
you can do something like this
@Mapping(target = "myTarget.propX",expression="java(mySource.getPropA().equals(\"Abc\")?"\"V01.123.456.AB\":\"\")")