mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2554 Records should not treat collections as alternative target accessors
This commit is contained in:
parent
2c23b935db
commit
b59a23965a
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
@ -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) {
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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" );
|
||||
}
|
||||
}
|
||||
|
@ -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 ) {
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user