我正在尝试使用Mapstruct映射和Lombok Builder创建补丁方法。但在生成的代码中缺少build()
调用
删除空值的Mapper:
@Mapper(
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE
)
public interface CartMapper {
CartMapper INSTANCE = Mappers.getMapper(CartMapper.class);
Cart cartRequestToCart(CartRequest cartRequest);
Cart patch(CartRequest cartRequest, @MappingTarget Cart cart);
}
嵌套类具有与Cart相同的lombok注释:
@Builder
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@Getter
@Setter
@FieldDefaults(level = AccessLevel.PRIVATE)
public class Cart {
Customer customer;
Owner owner;
Integer price;
String voucher;
}
所有嵌套类都具有与CartRequest相同的lombok注释,并且结构与目标实体结构相同
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class CartRequest {
CustomerRequest customer;
OwnerRequest owner;
Integer price;
String voucher;
}
方法cartRequestToCart
一切正常,在这里调用构建器,也在嵌套方法中调用
@Override
public Cart cartRequestToCart(CartRequest cartRequest) {
if ( cartRequest == null ) {
return null;
}
CartBuilder cart = Cart.builder();
if ( cartRequest.getCustomer() != null ) {
cart.customer( customerRequestToCustomer( cartRequest.getCustomer() ) );
}
if ( cartRequest.getOwner() != null ) {
cart.owner( ownerRequestToOwner( cartRequest.getOwner() ) );
}
if ( cartRequest.getPrice() != null ) {
cart.price( cartRequest.getPrice() );
}
if ( cartRequest.getVoucher() != null ) {
cart.voucher( cartRequest.getVoucher() );
}
return cart.build();
}
对于方法patch
生成这个和这里在一些setters缺少build()
调用
@Override
public Cart patch(CartRequest cartRequest, Cart cart) {
if ( cartRequest == null ) {
return null;
}
if ( cartRequest.getCustomer() != null ) {
if ( cart.getCustomer() == null ) {
cart.setCustomer( Customer.builder() ); // HERE build() missing
}
customerRequestToCustomer1( cartRequest.getCustomer(), cart.getCustomer() );
}
if ( cartRequest.getOwner() != null ) {
if ( cart.getOwner() == null ) {
cart.setOwner( Owner.builder() ); // HERE build() missing
}
ownerRequestToOwner1( cartRequest.getOwner(), cart.getOwner() );
}
if ( cartRequest.getPrice() != null ) {
cart.setPrice( cartRequest.getPrice() );
}
if ( cartRequest.getVoucher() != null ) {
cart.setVoucher( cartRequest.getVoucher() );
}
return cart;
}
一些提示如何解决这个问题?非常感谢。
这是MapStruct 1.3.0.Final中的一个错误。
我建议按照mapstruct/mapstruct#1742进行修复。
目前的解决方案是通过SPI设置NoOpBuilderProvider
来完全禁用构建器。