mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#3848: Mark String to number as lossy conversion
This commit is contained in:
parent
05f27e96e2
commit
42c87d1da9
@ -474,6 +474,7 @@ public class NativeTypes {
|
||||
tmp3.put( Double.class.getName(), 6 );
|
||||
tmp3.put( BigInteger.class.getName(), 50 );
|
||||
tmp3.put( BigDecimal.class.getName(), 51 );
|
||||
tmp3.put( String.class.getName(), 51 );
|
||||
NARROWING_LUT = Collections.unmodifiableMap( tmp3 );
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,7 @@ public class CutleryInventoryDto {
|
||||
private short numberOfKnifes;
|
||||
private int numberOfForks;
|
||||
private byte numberOfSpoons;
|
||||
private int drawerId;
|
||||
|
||||
private float approximateKnifeLength;
|
||||
|
||||
@ -44,4 +45,12 @@ public class CutleryInventoryDto {
|
||||
public void setApproximateKnifeLength(float approximateKnifeLength) {
|
||||
this.approximateKnifeLength = approximateKnifeLength;
|
||||
}
|
||||
|
||||
public int getDrawerId() {
|
||||
return drawerId;
|
||||
}
|
||||
|
||||
public void setDrawerId(int drawerId) {
|
||||
this.drawerId = drawerId;
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ public class CutleryInventoryEntity {
|
||||
private int numberOfKnifes;
|
||||
private Long numberOfForks;
|
||||
private short numberOfSpoons;
|
||||
private String drawerId;
|
||||
|
||||
private double approximateKnifeLength;
|
||||
|
||||
@ -44,4 +45,12 @@ public class CutleryInventoryEntity {
|
||||
public void setApproximateKnifeLength(double approximateKnifeLength) {
|
||||
this.approximateKnifeLength = approximateKnifeLength;
|
||||
}
|
||||
|
||||
public String getDrawerId() {
|
||||
return drawerId;
|
||||
}
|
||||
|
||||
public void setDrawerId(String drawerId) {
|
||||
this.drawerId = drawerId;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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.conversion.lossy;
|
||||
|
||||
import org.mapstruct.BeanMapping;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
@Mapper( typeConversionPolicy = ReportingPolicy.ERROR )
|
||||
public interface ErroneousKitchenDrawerMapper6 {
|
||||
|
||||
@BeanMapping( ignoreByDefault = true )
|
||||
@Mapping( target = "drawerId", source = "drawerId" )
|
||||
RegularKitchenDrawerEntity map( OversizedKitchenDrawerDto dto );
|
||||
|
||||
}
|
@ -40,12 +40,14 @@ public class LossyConversionTest {
|
||||
dto.setNumberOfKnifes( (short) 7 );
|
||||
dto.setNumberOfSpoons( (byte) 3 );
|
||||
dto.setApproximateKnifeLength( 3.7f );
|
||||
dto.setDrawerId( 1 );
|
||||
|
||||
CutleryInventoryEntity entity = CutleryInventoryMapper.INSTANCE.map( dto );
|
||||
assertThat( entity.getNumberOfForks() ).isEqualTo( 5L );
|
||||
assertThat( entity.getNumberOfKnifes() ).isEqualTo( 7 );
|
||||
assertThat( entity.getNumberOfSpoons() ).isEqualTo( (short) 3 );
|
||||
assertThat( entity.getApproximateKnifeLength() ).isCloseTo( 3.7d, withinPercentage( 0.0001d ) );
|
||||
assertThat( entity.getDrawerId() ).isEqualTo( "1" );
|
||||
}
|
||||
|
||||
@ProcessorTest
|
||||
@ -74,6 +76,19 @@ public class LossyConversionTest {
|
||||
public void testConversionFromBigIntegerToInteger() {
|
||||
}
|
||||
|
||||
@ProcessorTest
|
||||
@WithClasses(ErroneousKitchenDrawerMapper6.class)
|
||||
@ExpectedCompilationOutcome(value = CompilationResult.FAILED,
|
||||
diagnostics = {
|
||||
@Diagnostic(type = ErroneousKitchenDrawerMapper6.class,
|
||||
kind = javax.tools.Diagnostic.Kind.ERROR,
|
||||
line = 17,
|
||||
message = "Can't map property \"String drawerId\". It has a possibly lossy conversion from "
|
||||
+ "String to int.")
|
||||
})
|
||||
public void testConversionFromStringToInt() {
|
||||
}
|
||||
|
||||
@ProcessorTest
|
||||
@WithClasses(ErroneousKitchenDrawerMapper3.class)
|
||||
@ExpectedCompilationOutcome(value = CompilationResult.FAILED,
|
||||
|
@ -21,6 +21,7 @@ public class OversizedKitchenDrawerDto {
|
||||
private Double depth;
|
||||
private BigDecimal length;
|
||||
private double height;
|
||||
private String drawerId;
|
||||
|
||||
public long getNumberOfForks() {
|
||||
return numberOfForks;
|
||||
@ -70,4 +71,11 @@ public class OversizedKitchenDrawerDto {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public String getDrawerId() {
|
||||
return drawerId;
|
||||
}
|
||||
|
||||
public void setDrawerId(String drawerId) {
|
||||
this.drawerId = drawerId;
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ public class RegularKitchenDrawerEntity {
|
||||
private float depth;
|
||||
private Float length;
|
||||
private VerySpecialNumber height;
|
||||
private int drawerId;
|
||||
|
||||
public int getNumberOfForks() {
|
||||
return numberOfForks;
|
||||
@ -66,4 +67,11 @@ public class RegularKitchenDrawerEntity {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public int getDrawerId() {
|
||||
return drawerId;
|
||||
}
|
||||
|
||||
public void setDrawerId(int drawerId) {
|
||||
this.drawerId = drawerId;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user