#2715: Updated documentation to reflect impact of conditions on update mappers. (#2740)

* #2715: added an example with an update mapper for Conditional behavior.
This commit is contained in:
Zegveld 2022-02-06 20:05:42 +01:00 committed by GitHub
parent 7bb85d05c0
commit 9b434f80f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -354,6 +354,56 @@ public class CarMapperImpl implements CarMapper {
----
====
When using this in combination with an update mapping method it will replace the `null-check` there, for example:
.Update mapper using custom condition check method
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
@Mapper
public interface CarMapper {
CarDto carToCarDto(Car car, @MappingTarget CarDto carDto);
@Condition
default boolean isNotEmpty(String value) {
return value != null && !value.isEmpty();
}
}
----
====
The generated update mapper will look like:
.Custom condition check in generated implementation
====
[source, java, linenums]
[subs="verbatim,attributes"]
----
// GENERATED CODE
public class CarMapperImpl implements CarMapper {
@Override
public CarDto carToCarDto(Car car, CarDto carDto) {
if ( car == null ) {
return carDto;
}
if ( isNotEmpty( car.getOwner() ) ) {
carDto.setOwner( car.getOwner() );
} else {
carDto.setOwner( null );
}
// Mapping of other properties
return carDto;
}
}
----
====
[IMPORTANT]
====
If there is a custom `@Condition` method applicable for the property it will have a precedence over a presence check method in the bean itself.