mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2666 Presence Check should be applied to source parameters when used in @Mapping
This commit is contained in:
parent
00df0bc3d0
commit
5de813c16f
@ -558,11 +558,17 @@ public class PropertyMapping extends ModelElement {
|
|||||||
|
|
||||||
// parameter reference
|
// parameter reference
|
||||||
if ( propertyEntry == null ) {
|
if ( propertyEntry == null ) {
|
||||||
return new SourceRHS( sourceParam.getName(),
|
SourceRHS sourceRHS = new SourceRHS(
|
||||||
sourceParam.getType(),
|
sourceParam.getName(),
|
||||||
existingVariableNames,
|
sourceParam.getType(),
|
||||||
sourceReference.toString()
|
existingVariableNames,
|
||||||
|
sourceReference.toString()
|
||||||
);
|
);
|
||||||
|
sourceRHS.setSourcePresenceCheckerReference( getSourcePresenceCheckerRef(
|
||||||
|
sourceReference,
|
||||||
|
sourceRHS
|
||||||
|
) );
|
||||||
|
return sourceRHS;
|
||||||
}
|
}
|
||||||
// simple property
|
// simple property
|
||||||
else if ( !sourceReference.isNested() ) {
|
else if ( !sourceReference.isNested() ) {
|
||||||
|
@ -74,6 +74,43 @@ public class ConditionalExpressionTest {
|
|||||||
assertThat( target.getValue() ).isEqualTo( 0 );
|
assertThat( target.getValue() ).isEqualTo( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
@WithClasses({
|
||||||
|
ConditionalWithSourceToTargetExpressionMapper.class
|
||||||
|
})
|
||||||
|
@IssueKey("2666")
|
||||||
|
public void conditionalExpressionForSourceToTarget() {
|
||||||
|
ConditionalWithSourceToTargetExpressionMapper mapper = ConditionalWithSourceToTargetExpressionMapper.INSTANCE;
|
||||||
|
|
||||||
|
ConditionalWithSourceToTargetExpressionMapper.OrderDTO orderDto =
|
||||||
|
new ConditionalWithSourceToTargetExpressionMapper.OrderDTO();
|
||||||
|
|
||||||
|
ConditionalWithSourceToTargetExpressionMapper.Order order = mapper.convertToOrder( orderDto );
|
||||||
|
assertThat( order ).isNotNull();
|
||||||
|
assertThat( order.getCustomer() ).isNull();
|
||||||
|
|
||||||
|
orderDto = new ConditionalWithSourceToTargetExpressionMapper.OrderDTO();
|
||||||
|
orderDto.setCustomerName( "Tester" );
|
||||||
|
order = mapper.convertToOrder( orderDto );
|
||||||
|
|
||||||
|
assertThat( order ).isNotNull();
|
||||||
|
assertThat( order.getCustomer() ).isNotNull();
|
||||||
|
assertThat( order.getCustomer().getName() ).isEqualTo( "Tester" );
|
||||||
|
assertThat( order.getCustomer().getAddress() ).isNull();
|
||||||
|
|
||||||
|
orderDto = new ConditionalWithSourceToTargetExpressionMapper.OrderDTO();
|
||||||
|
orderDto.setLine1( "Line 1" );
|
||||||
|
order = mapper.convertToOrder( orderDto );
|
||||||
|
|
||||||
|
assertThat( order ).isNotNull();
|
||||||
|
assertThat( order.getCustomer() ).isNotNull();
|
||||||
|
assertThat( order.getCustomer().getName() ).isNull();
|
||||||
|
assertThat( order.getCustomer().getAddress() ).isNotNull();
|
||||||
|
assertThat( order.getCustomer().getAddress().getLine1() ).isEqualTo( "Line 1" );
|
||||||
|
assertThat( order.getCustomer().getAddress().getLine2() ).isNull();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@ProcessorTest
|
@ProcessorTest
|
||||||
@ExpectedCompilationOutcome(
|
@ExpectedCompilationOutcome(
|
||||||
value = CompilationResult.FAILED,
|
value = CompilationResult.FAILED,
|
||||||
|
@ -0,0 +1,132 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.conditional.expression;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper(imports = ConditionalWithSourceToTargetExpressionMapper.Util.class)
|
||||||
|
public interface ConditionalWithSourceToTargetExpressionMapper {
|
||||||
|
|
||||||
|
ConditionalWithSourceToTargetExpressionMapper INSTANCE =
|
||||||
|
Mappers.getMapper( ConditionalWithSourceToTargetExpressionMapper.class );
|
||||||
|
|
||||||
|
@Mapping(source = "orderDTO", target = "customer",
|
||||||
|
conditionExpression = "java(Util.mapCustomerFromOrder( orderDTO ))")
|
||||||
|
Order convertToOrder(OrderDTO orderDTO);
|
||||||
|
|
||||||
|
@Mapping(source = "customerName", target = "name")
|
||||||
|
@Mapping(source = "orderDTO", target = "address",
|
||||||
|
conditionExpression = "java(Util.mapAddressFromOrder( orderDTO ))")
|
||||||
|
Customer convertToCustomer(OrderDTO orderDTO);
|
||||||
|
|
||||||
|
Address convertToAddress(OrderDTO orderDTO);
|
||||||
|
|
||||||
|
class OrderDTO {
|
||||||
|
|
||||||
|
private String customerName;
|
||||||
|
private String line1;
|
||||||
|
private String line2;
|
||||||
|
|
||||||
|
public String getCustomerName() {
|
||||||
|
return customerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomerName(String customerName) {
|
||||||
|
this.customerName = customerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLine1() {
|
||||||
|
return line1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLine1(String line1) {
|
||||||
|
this.line1 = line1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLine2() {
|
||||||
|
return line2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLine2(String line2) {
|
||||||
|
this.line2 = line2;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Order {
|
||||||
|
|
||||||
|
private Customer customer;
|
||||||
|
|
||||||
|
public Customer getCustomer() {
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomer(Customer customer) {
|
||||||
|
this.customer = customer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Customer {
|
||||||
|
private String name;
|
||||||
|
private Address address;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Address getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(Address address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Address {
|
||||||
|
|
||||||
|
private String line1;
|
||||||
|
private String line2;
|
||||||
|
|
||||||
|
public String getLine1() {
|
||||||
|
return line1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLine1(String line1) {
|
||||||
|
this.line1 = line1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLine2() {
|
||||||
|
return line2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLine2(String line2) {
|
||||||
|
this.line2 = line2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Util {
|
||||||
|
|
||||||
|
static boolean mapCustomerFromOrder(OrderDTO orderDTO) {
|
||||||
|
return orderDTO != null && ( orderDTO.getCustomerName() != null || mapAddressFromOrder( orderDTO ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
static boolean mapAddressFromOrder(OrderDTO orderDTO) {
|
||||||
|
return orderDTO != null && ( orderDTO.getLine1() != null || orderDTO.getLine2() != null );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,132 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.conditional.qualifier;
|
||||||
|
|
||||||
|
import org.mapstruct.Condition;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.Named;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface ConditionalMethodWithSourceToTargetMapper {
|
||||||
|
|
||||||
|
ConditionalMethodWithSourceToTargetMapper INSTANCE =
|
||||||
|
Mappers.getMapper( ConditionalMethodWithSourceToTargetMapper.class );
|
||||||
|
|
||||||
|
@Mapping(source = "orderDTO", target = "customer", conditionQualifiedByName = "mapCustomerFromOrder")
|
||||||
|
Order convertToOrder(OrderDTO orderDTO);
|
||||||
|
|
||||||
|
@Mapping(source = "customerName", target = "name")
|
||||||
|
@Mapping(source = "orderDTO", target = "address", conditionQualifiedByName = "mapAddressFromOrder")
|
||||||
|
Customer convertToCustomer(OrderDTO orderDTO);
|
||||||
|
|
||||||
|
Address convertToAddress(OrderDTO orderDTO);
|
||||||
|
|
||||||
|
@Condition
|
||||||
|
@Named("mapCustomerFromOrder")
|
||||||
|
default boolean mapCustomerFromOrder(OrderDTO orderDTO) {
|
||||||
|
return orderDTO != null && ( orderDTO.getCustomerName() != null || mapAddressFromOrder( orderDTO ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Condition
|
||||||
|
@Named("mapAddressFromOrder")
|
||||||
|
default boolean mapAddressFromOrder(OrderDTO orderDTO) {
|
||||||
|
return orderDTO != null && ( orderDTO.getLine1() != null || orderDTO.getLine2() != null );
|
||||||
|
}
|
||||||
|
|
||||||
|
class OrderDTO {
|
||||||
|
|
||||||
|
private String customerName;
|
||||||
|
private String line1;
|
||||||
|
private String line2;
|
||||||
|
|
||||||
|
public String getCustomerName() {
|
||||||
|
return customerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomerName(String customerName) {
|
||||||
|
this.customerName = customerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLine1() {
|
||||||
|
return line1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLine1(String line1) {
|
||||||
|
this.line1 = line1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLine2() {
|
||||||
|
return line2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLine2(String line2) {
|
||||||
|
this.line2 = line2;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Order {
|
||||||
|
|
||||||
|
private Customer customer;
|
||||||
|
|
||||||
|
public Customer getCustomer() {
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomer(Customer customer) {
|
||||||
|
this.customer = customer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Customer {
|
||||||
|
private String name;
|
||||||
|
private Address address;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Address getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(Address address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Address {
|
||||||
|
|
||||||
|
private String line1;
|
||||||
|
private String line2;
|
||||||
|
|
||||||
|
public String getLine1() {
|
||||||
|
return line1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLine1(String line1) {
|
||||||
|
this.line1 = line1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLine2() {
|
||||||
|
return line2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLine2(String line2) {
|
||||||
|
this.line2 = line2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -86,4 +86,41 @@ public class ConditionalQualifierTest {
|
|||||||
assertThat( employee.getNin() ).isNull();
|
assertThat( employee.getNin() ).isNull();
|
||||||
assertThat( employee.getSsid() ).isNull();
|
assertThat( employee.getSsid() ).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
@WithClasses({
|
||||||
|
ConditionalMethodWithSourceToTargetMapper.class
|
||||||
|
})
|
||||||
|
@IssueKey("2666")
|
||||||
|
public void conditionalQualifiersForSourceToTarget() {
|
||||||
|
ConditionalMethodWithSourceToTargetMapper mapper = ConditionalMethodWithSourceToTargetMapper.INSTANCE;
|
||||||
|
|
||||||
|
ConditionalMethodWithSourceToTargetMapper.OrderDTO orderDto =
|
||||||
|
new ConditionalMethodWithSourceToTargetMapper.OrderDTO();
|
||||||
|
|
||||||
|
ConditionalMethodWithSourceToTargetMapper.Order order = mapper.convertToOrder( orderDto );
|
||||||
|
assertThat( order ).isNotNull();
|
||||||
|
assertThat( order.getCustomer() ).isNull();
|
||||||
|
|
||||||
|
orderDto = new ConditionalMethodWithSourceToTargetMapper.OrderDTO();
|
||||||
|
orderDto.setCustomerName( "Tester" );
|
||||||
|
order = mapper.convertToOrder( orderDto );
|
||||||
|
|
||||||
|
assertThat( order ).isNotNull();
|
||||||
|
assertThat( order.getCustomer() ).isNotNull();
|
||||||
|
assertThat( order.getCustomer().getName() ).isEqualTo( "Tester" );
|
||||||
|
assertThat( order.getCustomer().getAddress() ).isNull();
|
||||||
|
|
||||||
|
orderDto = new ConditionalMethodWithSourceToTargetMapper.OrderDTO();
|
||||||
|
orderDto.setLine1( "Line 1" );
|
||||||
|
order = mapper.convertToOrder( orderDto );
|
||||||
|
|
||||||
|
assertThat( order ).isNotNull();
|
||||||
|
assertThat( order.getCustomer() ).isNotNull();
|
||||||
|
assertThat( order.getCustomer().getName() ).isNull();
|
||||||
|
assertThat( order.getCustomer().getAddress() ).isNotNull();
|
||||||
|
assertThat( order.getCustomer().getAddress().getLine1() ).isEqualTo( "Line 1" );
|
||||||
|
assertThat( order.getCustomer().getAddress().getLine2() ).isNull();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user