mapstruct/NEXT_RELEASE_CHANGELOG.md
Filip Hrisafov 66f4288842
#3601 Always use SourceParameterCondition when checking source parameter
This is a breaking change, with this change whenever a source parameter is used as a source for a target property the condition has to apply to source parameters and not properties
2024-07-20 13:53:39 +02:00

2.1 KiB

Features

Enhancements

  • Breaking change:g (#3574) - This reverts #2560, because we've decided that @BeanMapping(ignoreByDefault = true) should only be applied to target properties and not to source properties. Source properties are ignored anyway, the BeanMapping#unmappedSourcePolicy should be used to control what should happen with unmapped source policy

Bugs

  • Breaking change: Presence check method used only once when multiple source parameters are provided (#3601)

Documentation

Build

Breaking changes

Presence checks for source parameters

In 1.6, support for presence checks on source parameters has been added. This means that even if you want to map a source parameter directly to some target property the new @SourceParameterCondition or @Condition(appliesTo = ConditionStrategy.SOURCE_PARAMETERS) should be used.

e.g.

If we had the following in 1.5:

@Mapper
public interface OrderMapper {

    @Mapping(source = "dto", target = "customer", conditionQualifiedByName = "mapCustomerFromOrder")
    Order map(OrderDTO dto);

    @Condition
    @Named("mapCustomerFromOrder")
    default boolean mapCustomerFromOrder(OrderDTO dto) {
        return dto != null && dto.getCustomerName() != null;
    }

}

Them MapStruct would generate

public class OrderMapperImpl implements OrderMapper {

    @Override
    public Order map(OrderDTO dto) {
        if ( dto == null ) {
            return null;
        }

        Order order = new Order();

        if ( mapCustomerFromOrder( dto ) ) {
            order.setCustomer( orderDtoToCustomer( orderDTO ) );
        }

        return order;
    }
}

In order for the same to be generated in 1.6, the mapper needs to look like this:

@Mapper
public interface OrderMapper {

    @Mapping(source = "dto", target = "customer", conditionQualifiedByName = "mapCustomerFromOrder")
    Order map(OrderDTO dto);

    @SourceParameterCondition
    @Named("mapCustomerFromOrder")
    default boolean mapCustomerFromOrder(OrderDTO dto) {
        return dto != null && dto.getCustomerName() != null;
    }

}