#2478 Make sure that forged methods do not generate duplicate method parameters

This commit is contained in:
Filip Hrisafov 2021-06-12 10:41:39 +02:00
parent 7f38efad4d
commit 934a47323a
3 changed files with 130 additions and 1 deletions

View File

@ -10,6 +10,7 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.lang.model.element.ExecutableElement;
import org.mapstruct.ap.internal.model.beanmapping.MappingReferences;
@ -125,7 +126,16 @@ public class ForgedMethod implements Method {
boolean forgedNameBased) {
// establish name
String sourceParamSafeName = Strings.getSafeVariableName( sourceType.getName() );
String sourceParamSafeName;
if ( additionalParameters.isEmpty() ) {
sourceParamSafeName = Strings.getSafeVariableName( sourceType.getName() );
}
else {
sourceParamSafeName = Strings.getSafeVariableName(
sourceType.getName(),
additionalParameters.stream().map( Parameter::getName ).collect( Collectors.toList() )
);
}
// establish parameters
this.parameters = new ArrayList<>( 1 + additionalParameters.size() );

View File

@ -0,0 +1,86 @@
/*
* 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._2478;
import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper
public interface Issue2478Mapper {
Issue2478Mapper INSTANCE = Mappers.getMapper( Issue2478Mapper.class );
ProductEntity productFromDto(Product dto, @Context Shop shop);
class Product {
protected final String name;
protected final Shop shop;
public Product(String name, Shop shop) {
this.name = name;
this.shop = shop;
}
public String getName() {
return name;
}
public Shop getShop() {
return shop;
}
}
class Shop {
protected final String name;
public Shop(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class ProductEntity {
protected String name;
protected ShopEntity shop;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ShopEntity getShop() {
return shop;
}
public void setShop(ShopEntity shop) {
this.shop = shop;
}
}
class ShopEntity {
protected String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}

View File

@ -0,0 +1,33 @@
/*
* 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._2478;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Filip Hrisafov
*/
@IssueKey("2478")
@WithClasses({
Issue2478Mapper.class
})
class Issue2478Test {
@ProcessorTest
void shouldGenerateCodeThatCompiles() {
Issue2478Mapper.Product dto = new Issue2478Mapper.Product( "Test", new Issue2478Mapper.Shop( "Shopify" ) );
Issue2478Mapper.ProductEntity entity = Issue2478Mapper.INSTANCE.productFromDto( dto, dto.getShop() );
assertThat( entity ).isNotNull();
assertThat( entity.getName() ).isEqualTo( "Test" );
assertThat( entity.getShop() ).isNotNull();
assertThat( entity.getShop().getName() ).isEqualTo( "Shopify" );
}
}