#2554 Records should not treat collections as alternative target accessors

This commit is contained in:
Filip Hrisafov 2021-08-29 11:24:34 +02:00
parent 2c23b935db
commit b59a23965a
6 changed files with 113 additions and 0 deletions

View File

@ -0,0 +1,24 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.records;
import java.util.List;
/**
* @author Filip Hrisafov
*/
public class Car {
private List<WheelPosition> wheelPositions;
public List<WheelPosition> getWheelPositions() {
return wheelPositions;
}
public void setWheelPositions(List<WheelPosition> wheelPositions) {
this.wheelPositions = wheelPositions;
}
}

View File

@ -0,0 +1,31 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.records;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper(unmappedTargetPolicy = ReportingPolicy.ERROR)
public interface CarAndWheelMapper {
CarAndWheelMapper INSTANCE = Mappers.getMapper( CarAndWheelMapper.class );
default String stringFromWheelPosition(WheelPosition source) {
return source == null ? null : source.getPosition();
}
default WheelPosition wheelPositionFromString(String source) {
return source == null ? null : new WheelPosition(source);
}
CarDto carDtoFromCar(Car source);
Car carFromCarDto(CarDto source);
}

View File

@ -0,0 +1,15 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.records;
import java.util.List;
/**
* @author Filip Hrisafov
*/
public record CarDto(List<String> wheelPositions) {
}

View File

@ -0,0 +1,22 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.itest.records;
/**
* @author Filip Hrisafov
*/
public class WheelPosition {
private final String position;
public WheelPosition(String position) {
this.position = position;
}
public String getPosition() {
return position;
}
}

View File

@ -5,6 +5,8 @@
*/
package org.mapstruct.itest.records;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
@ -47,4 +49,16 @@ public class RecordsTest {
assertThat( value ).isNotNull();
assertThat( value.value() ).isEqualTo( "Kermit" );
}
@Test
public void shouldMapIntoRecordWithList() {
Car car = new Car();
car.setWheelPositions( Arrays.asList( new WheelPosition( "left" ) ) );
CarDto carDto = CarAndWheelMapper.INSTANCE.carDtoFromCar(car);
assertThat( carDto ).isNotNull();
assertThat( carDto.wheelPositions() )
.containsExactly( "left" );
}
}

View File

@ -874,6 +874,13 @@ public class Type extends ModelElement implements Comparable<Type> {
* @return an unmodifiable list of alternative target accessors.
*/
private List<Accessor> getAlternativeTargetAccessors() {
if ( alternativeTargetAccessors != null ) {
return alternativeTargetAccessors;
}
if ( isRecord() ) {
alternativeTargetAccessors = Collections.emptyList();
}
if ( alternativeTargetAccessors == null ) {