mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2109 Generate correct array assignment for constructor parameter mapping
This commit is contained in:
parent
d87d75a7a8
commit
da37d40152
@ -30,31 +30,26 @@
|
||||
<#list sourceParametersExcludingPrimitives as sourceParam>
|
||||
<#if (constructorPropertyMappingsByParameter(sourceParam)?size > 0)>
|
||||
<#list constructorPropertyMappingsByParameter(sourceParam) as propertyMapping>
|
||||
<@includeModel object=propertyMapping.targetType /> ${propertyMapping.targetWriteAccessorName};
|
||||
<@includeModel object=propertyMapping.targetType /> ${propertyMapping.targetWriteAccessorName} = ${propertyMapping.targetType.null};
|
||||
</#list>
|
||||
if ( ${sourceParam.name} != null ) {
|
||||
<#list constructorPropertyMappingsByParameter(sourceParam) as propertyMapping>
|
||||
<@includeModel object=propertyMapping existingInstanceMapping=existingInstanceMapping defaultValueAssignment=propertyMapping.defaultValueAssignment/>
|
||||
</#list>
|
||||
}
|
||||
else {
|
||||
<#list constructorPropertyMappingsByParameter(sourceParam) as propertyMapping>
|
||||
${propertyMapping.targetWriteAccessorName} = ${propertyMapping.targetType.null};
|
||||
</#list>
|
||||
}
|
||||
</#if>
|
||||
</#list>
|
||||
<#list sourcePrimitiveParameters as sourceParam>
|
||||
<#if (constructorPropertyMappingsByParameter(sourceParam)?size > 0)>
|
||||
<#list constructorPropertyMappingsByParameter(sourceParam) as propertyMapping>
|
||||
<@includeModel object=propertyMapping.targetType /> ${propertyMapping.targetWriteAccessorName};
|
||||
<@includeModel object=propertyMapping.targetType /> ${propertyMapping.targetWriteAccessorName} = ${propertyMapping.targetType.null};
|
||||
<@includeModel object=propertyMapping existingInstanceMapping=existingInstanceMapping defaultValueAssignment=propertyMapping.defaultValueAssignment/>
|
||||
</#list>
|
||||
</#if>
|
||||
</#list>
|
||||
<#else>
|
||||
<#list constructorPropertyMappingsByParameter(sourceParameters[0]) as propertyMapping>
|
||||
<@includeModel object=propertyMapping.targetType /> ${propertyMapping.targetWriteAccessorName};
|
||||
<@includeModel object=propertyMapping.targetType /> ${propertyMapping.targetWriteAccessorName} = ${propertyMapping.targetType.null};
|
||||
</#list>
|
||||
<#if mapNullToDefault>if ( ${sourceParameters[0].name} != null ) {</#if>
|
||||
<#list constructorPropertyMappingsByParameter(sourceParameters[0]) as propertyMapping>
|
||||
@ -62,11 +57,6 @@
|
||||
</#list>
|
||||
<#if mapNullToDefault>
|
||||
}
|
||||
else {
|
||||
<#list constructorPropertyMappingsByParameter(sourceParameters[0]) as propertyMapping>
|
||||
${propertyMapping.targetWriteAccessorName} = ${propertyMapping.targetType.null};
|
||||
</#list>
|
||||
}
|
||||
</#if>
|
||||
</#if>
|
||||
<#list constructorConstantMappings as constantMapping>
|
||||
|
@ -18,10 +18,6 @@
|
||||
<@lib.handleLocalVarNullCheck needs_explicit_local_var=directAssignment>
|
||||
<#if ext.targetBeanName?has_content>${ext.targetBeanName}.</#if>${ext.targetWriteAccessorName}<@lib.handleWrite><#if directAssignment><@wrapLocalVarInCollectionInitializer/><#else><@lib.handleWithAssignmentOrNullCheckVar/></#if></@lib.handleWrite>;
|
||||
</@lib.handleLocalVarNullCheck>
|
||||
<#if !ext.defaultValueAssignment?? && !sourcePresenceCheckerReference?? && !ext.targetBeanName?has_content>else {<#-- the opposite (defaultValueAssignment) case is handeld inside lib.handleLocalVarNullCheck -->
|
||||
${ext.targetWriteAccessorName}<@lib.handleWrite>null</@lib.handleWrite>;
|
||||
}
|
||||
</#if>
|
||||
</#macro>
|
||||
<#--
|
||||
wraps the local variable in a collection initializer (new collection, or EnumSet.copyOf)
|
||||
|
@ -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.ap.test.bugs._2109;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
@Mapper
|
||||
public interface Issue2109Mapper {
|
||||
|
||||
Issue2109Mapper INSTANCE = Mappers.getMapper( Issue2109Mapper.class );
|
||||
|
||||
Target map(Source source);
|
||||
|
||||
@Mapping(target = "data", defaultExpression = "java(new byte[0])")
|
||||
Target mapWithEmptyData(Source source);
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.bugs._2109;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
@IssueKey("2109")
|
||||
@RunWith(AnnotationProcessorTestRunner.class)
|
||||
@WithClasses({
|
||||
Issue2109Mapper.class,
|
||||
Source.class,
|
||||
Target.class,
|
||||
})
|
||||
public class Issue2109Test {
|
||||
|
||||
@Test
|
||||
public void shouldCorrectlyMapArrayInConstructorMapping() {
|
||||
Target target = Issue2109Mapper.INSTANCE.map( new Source( 100L, new byte[] { 100, 120, 40, 40 } ) );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getId() ).isEqualTo( 100L );
|
||||
assertThat( target.getData() ).containsExactly( 100, 120, 40, 40 );
|
||||
|
||||
target = Issue2109Mapper.INSTANCE.map( new Source( 50L, null ) );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getId() ).isEqualTo( 50L );
|
||||
assertThat( target.getData() ).isNull();
|
||||
|
||||
target = Issue2109Mapper.INSTANCE.mapWithEmptyData( new Source( 100L, new byte[] { 100, 120, 40, 40 } ) );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getId() ).isEqualTo( 100L );
|
||||
assertThat( target.getData() ).containsExactly( 100, 120, 40, 40 );
|
||||
|
||||
target = Issue2109Mapper.INSTANCE.mapWithEmptyData( new Source( 50L, null ) );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getId() ).isEqualTo( 50L );
|
||||
assertThat( target.getData() ).isEmpty();
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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.bugs._2109;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
public class Source {
|
||||
|
||||
private final Long id;
|
||||
private final byte[] data;
|
||||
|
||||
public Source(Long id, byte[] data) {
|
||||
this.id = id;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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.bugs._2109;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
public class Target {
|
||||
|
||||
private final Long id;
|
||||
private final byte[] data;
|
||||
|
||||
public Target(Long id, byte[] data) {
|
||||
this.id = id;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
@ -26,36 +26,24 @@ public class ArtistToChartEntryImpl implements ArtistToChartEntry {
|
||||
return null;
|
||||
}
|
||||
|
||||
String chartName;
|
||||
String chartName = null;
|
||||
if ( chart != null ) {
|
||||
chartName = chart.getName();
|
||||
}
|
||||
else {
|
||||
chartName = null;
|
||||
}
|
||||
String songTitle;
|
||||
String artistName;
|
||||
String recordedAt;
|
||||
String city;
|
||||
String songTitle = null;
|
||||
String artistName = null;
|
||||
String recordedAt = null;
|
||||
String city = null;
|
||||
if ( song != null ) {
|
||||
songTitle = song.getTitle();
|
||||
artistName = songArtistName( song );
|
||||
recordedAt = songArtistLabelStudioName( song );
|
||||
city = songArtistLabelStudioCity( song );
|
||||
}
|
||||
else {
|
||||
songTitle = null;
|
||||
artistName = null;
|
||||
recordedAt = null;
|
||||
city = null;
|
||||
}
|
||||
int position1;
|
||||
int position1 = 0;
|
||||
if ( position != null ) {
|
||||
position1 = position;
|
||||
}
|
||||
else {
|
||||
position1 = 0;
|
||||
}
|
||||
|
||||
ChartEntry chartEntry = new ChartEntry( chartName, songTitle, artistName, recordedAt, city, position1 );
|
||||
|
||||
@ -68,10 +56,10 @@ public class ArtistToChartEntryImpl implements ArtistToChartEntry {
|
||||
return null;
|
||||
}
|
||||
|
||||
String songTitle;
|
||||
String artistName;
|
||||
String recordedAt;
|
||||
String city;
|
||||
String songTitle = null;
|
||||
String artistName = null;
|
||||
String recordedAt = null;
|
||||
String city = null;
|
||||
|
||||
songTitle = song.getTitle();
|
||||
artistName = songArtistName( song );
|
||||
@ -92,7 +80,7 @@ public class ArtistToChartEntryImpl implements ArtistToChartEntry {
|
||||
return null;
|
||||
}
|
||||
|
||||
String chartName;
|
||||
String chartName = null;
|
||||
|
||||
chartName = name.getName();
|
||||
|
||||
|
@ -27,8 +27,8 @@ public class ChartEntryToArtistImpl extends ChartEntryToArtist {
|
||||
return null;
|
||||
}
|
||||
|
||||
Song song;
|
||||
String name;
|
||||
Song song = null;
|
||||
String name = null;
|
||||
|
||||
song = chartEntryToSong( chartEntry );
|
||||
name = chartEntry.getChartName();
|
||||
@ -46,12 +46,12 @@ public class ChartEntryToArtistImpl extends ChartEntryToArtist {
|
||||
return null;
|
||||
}
|
||||
|
||||
String chartName;
|
||||
String songTitle;
|
||||
String artistName;
|
||||
String recordedAt;
|
||||
String city;
|
||||
int position;
|
||||
String chartName = null;
|
||||
String songTitle = null;
|
||||
String artistName = null;
|
||||
String recordedAt = null;
|
||||
String city = null;
|
||||
int position = 0;
|
||||
|
||||
chartName = chart.getName();
|
||||
songTitle = chartSongTitle( chart );
|
||||
@ -70,8 +70,8 @@ public class ChartEntryToArtistImpl extends ChartEntryToArtist {
|
||||
return null;
|
||||
}
|
||||
|
||||
String name;
|
||||
String city;
|
||||
String name = null;
|
||||
String city = null;
|
||||
|
||||
name = chartEntry.getRecordedAt();
|
||||
city = chartEntry.getCity();
|
||||
@ -86,7 +86,7 @@ public class ChartEntryToArtistImpl extends ChartEntryToArtist {
|
||||
return null;
|
||||
}
|
||||
|
||||
Studio studio;
|
||||
Studio studio = null;
|
||||
|
||||
studio = chartEntryToStudio( chartEntry );
|
||||
|
||||
@ -102,8 +102,8 @@ public class ChartEntryToArtistImpl extends ChartEntryToArtist {
|
||||
return null;
|
||||
}
|
||||
|
||||
Label label;
|
||||
String name;
|
||||
Label label = null;
|
||||
String name = null;
|
||||
|
||||
label = chartEntryToLabel( chartEntry );
|
||||
name = chartEntry.getArtistName();
|
||||
@ -118,9 +118,9 @@ public class ChartEntryToArtistImpl extends ChartEntryToArtist {
|
||||
return null;
|
||||
}
|
||||
|
||||
Artist artist;
|
||||
String title;
|
||||
List<Integer> positions;
|
||||
Artist artist = null;
|
||||
String title = null;
|
||||
List<Integer> positions = null;
|
||||
|
||||
artist = chartEntryToArtist( chartEntry );
|
||||
title = chartEntry.getSongTitle();
|
||||
|
Loading…
x
Reference in New Issue
Block a user