#2596 Record components should have the highest priority for read accessors

This commit is contained in:
Filip Hrisafov 2021-10-16 18:50:04 +02:00
parent 935c03e822
commit 564455ee45
5 changed files with 111 additions and 7 deletions

View File

@ -0,0 +1,13 @@
/*
* 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 record MemberDto(Boolean isActive, Boolean premium) {
}

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;
/**
* @author Filip Hrisafov
*/
public class MemberEntity {
private Boolean isActive;
private Boolean premium;
public Boolean getIsActive() {
return isActive;
}
public void setIsActive(Boolean active) {
isActive = active;
}
public Boolean getPremium() {
return premium;
}
public void setPremium(Boolean premium) {
this.premium = premium;
}
}

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 org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper(unmappedSourcePolicy = ReportingPolicy.ERROR)
public interface MemberMapper {
MemberMapper INSTANCE = Mappers.getMapper( MemberMapper.class );
MemberEntity fromRecord(MemberDto record);
MemberDto toRecord(MemberEntity entity);
}

View File

@ -61,4 +61,26 @@ public class RecordsTest {
assertThat( carDto.wheelPositions() ) assertThat( carDto.wheelPositions() )
.containsExactly( "left" ); .containsExactly( "left" );
} }
@Test
public void shouldMapMemberRecord() {
MemberEntity member = MemberMapper.INSTANCE.fromRecord( new MemberDto( true, false ) );
assertThat( member ).isNotNull();
assertThat( member.getIsActive() ).isTrue();
assertThat( member.getPremium() ).isFalse();
}
@Test
public void shouldMapIntoMemberRecord() {
MemberEntity entity = new MemberEntity();
entity.setIsActive( false );
entity.setPremium( true );
MemberDto value = MemberMapper.INSTANCE.toRecord( entity );
assertThat( value ).isNotNull();
assertThat( value.isActive() ).isEqualTo( false );
assertThat( value.premium() ).isEqualTo( true );
}
} }

View File

@ -576,14 +576,33 @@ public class Type extends ModelElement implements Comparable<Type> {
*/ */
public Map<String, Accessor> getPropertyReadAccessors() { public Map<String, Accessor> getPropertyReadAccessors() {
if ( readAccessors == null ) { if ( readAccessors == null ) {
List<Accessor> getterList = filters.getterMethodsIn( getAllMethods() );
Map<String, Accessor> modifiableGetters = new LinkedHashMap<>(); Map<String, Accessor> modifiableGetters = new LinkedHashMap<>();
Map<String, Accessor> recordAccessors = filters.recordAccessorsIn( getRecordComponents() );
modifiableGetters.putAll( recordAccessors );
List<Accessor> getterList = filters.getterMethodsIn( getAllMethods() );
for ( Accessor getter : getterList ) { for ( Accessor getter : getterList ) {
String simpleName = getter.getSimpleName();
if ( recordAccessors.containsKey( simpleName ) ) {
// If there is already a record accessor that contains the simple name
// then it means that the getter is actually a record component.
// In that case we need to ignore it.
// e.g. record component named isActive.
// The DefaultAccessorNamingStrategy will return active as property name,
// but the property name is isActive, since it is a record
continue;
}
String propertyName = getPropertyName( getter ); String propertyName = getPropertyName( getter );
if ( recordAccessors.containsKey( propertyName ) ) {
// If there is already a record accessor, the property needs to be ignored
continue;
}
if ( modifiableGetters.containsKey( propertyName ) ) { if ( modifiableGetters.containsKey( propertyName ) ) {
// In the DefaultAccessorNamingStrategy, this can only be the case for Booleans: isFoo() and // In the DefaultAccessorNamingStrategy, this can only be the case for Booleans: isFoo() and
// getFoo(); The latter is preferred. // getFoo(); The latter is preferred.
if ( !getter.getSimpleName().startsWith( "is" ) ) { if ( !simpleName.startsWith( "is" ) ) {
modifiableGetters.put( propertyName, getter ); modifiableGetters.put( propertyName, getter );
} }
@ -593,11 +612,6 @@ public class Type extends ModelElement implements Comparable<Type> {
} }
} }
Map<String, Accessor> recordAccessors = filters.recordAccessorsIn( getRecordComponents() );
for ( Map.Entry<String, Accessor> recordEntry : recordAccessors.entrySet() ) {
modifiableGetters.putIfAbsent( recordEntry.getKey(), recordEntry.getValue() );
}
List<Accessor> fieldsList = filters.fieldsIn( getAllFields() ); List<Accessor> fieldsList = filters.fieldsIn( getAllFields() );
for ( Accessor field : fieldsList ) { for ( Accessor field : fieldsList ) {
String propertyName = getPropertyName( field ); String propertyName = getPropertyName( field );