#3848: Mark String to number as lossy conversion

This commit is contained in:
Şamil Can 2025-05-25 15:22:47 +02:00 committed by GitHub
parent 05f27e96e2
commit 42c87d1da9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 70 additions and 0 deletions

View File

@ -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 );
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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 );
}

View File

@ -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,

View File

@ -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;
}
}

View File

@ -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;
}
}