Skip to content

Commit

Permalink
mapstruct#2596 Record components should have the highest priority for…
Browse files Browse the repository at this point in the history
… read accessors
  • Loading branch information
filiphr committed Oct 16, 2021
1 parent 8b84f5b commit f9e251e
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 7 deletions.
@@ -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) {

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

}
Expand Up @@ -61,4 +61,26 @@ public void shouldMapIntoRecordWithList() {
assertThat( carDto.wheelPositions() )
.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 );
}
}
Expand Up @@ -573,14 +573,33 @@ public Type asRawType() {
*/
public Map<String, Accessor> getPropertyReadAccessors() {
if ( readAccessors == null ) {
List<Accessor> getterList = filters.getterMethodsIn( getAllMethods() );
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 ) {
String simpleName = getter.getSimpleName();
if ( recordAccessors.containsKey( simpleName ) ) {
// If there is already a record accessor that contains the simple
// 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 );

if ( recordAccessors.containsKey( propertyName ) ) {
// If there is already a record accessor, the property needs to be ignored
continue;
}
if ( modifiableGetters.containsKey( propertyName ) ) {
// In the DefaultAccessorNamingStrategy, this can only be the case for Booleans: isFoo() and
// getFoo(); The latter is preferred.
if ( !getter.getSimpleName().startsWith( "is" ) ) {
if ( !simpleName.startsWith( "is" ) ) {
modifiableGetters.put( propertyName, getter );
}

Expand All @@ -590,11 +609,6 @@ public Map<String, Accessor> getPropertyReadAccessors() {
}
}

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() );
for ( Accessor field : fieldsList ) {
String propertyName = getPropertyName( field );
Expand Down

0 comments on commit f9e251e

Please sign in to comment.