#2170 Make sure that an import is created for constructor mapping defined variables

This commit is contained in:
Filip Hrisafov 2020-08-01 09:02:29 +02:00
parent 609824037b
commit ed16d62a91
9 changed files with 225 additions and 0 deletions

View File

@ -1488,6 +1488,10 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
for ( PropertyMapping propertyMapping : propertyMappings ) {
types.addAll( propertyMapping.getImportTypes() );
if ( propertyMapping.isConstructorMapping() ) {
// We need to add the target type imports for a constructor mapper since we define its parameters
types.addAll( propertyMapping.getTargetType().getImportTypes() );
}
}
if ( returnTypeToConstruct != null ) {

View File

@ -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._2170;
import java.util.Collections;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.test.bugs._2170.dto.AddressDto;
import org.mapstruct.ap.test.bugs._2170.dto.PersonDto;
import org.mapstruct.ap.test.bugs._2170.entity.Address;
import org.mapstruct.ap.test.bugs._2170.entity.Person;
import org.mapstruct.ap.test.bugs._2170.mapper.AddressMapper;
import org.mapstruct.ap.test.bugs._2170.mapper.EntityMapper;
import org.mapstruct.ap.test.bugs._2170.mapper.PersonMapper;
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("2170")
@RunWith(AnnotationProcessorTestRunner.class)
@WithClasses({
Address.class,
Person.class,
AddressDto.class,
PersonDto.class,
AddressMapper.class,
PersonMapper.class,
EntityMapper.class,
})
public class Issue2170Test {
@Test
public void shouldGenerateCodeThatCompiles() {
AddressDto addressDto = AddressMapper.INSTANCE.toDto( new Address(
"10000",
Collections.singletonList( new Person( "Tester" ) )
) );
assertThat( addressDto ).isNotNull();
assertThat( addressDto.getZipCode() ).isEqualTo( "10000" );
assertThat( addressDto.getPeople() )
.extracting( PersonDto::getName )
.containsExactly( "Tester" );
}
}

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.ap.test.bugs._2170.dto;
import java.util.List;
/**
* @author Filip Hrisafov
*/
public class AddressDto {
private final String zipCode;
private final List<PersonDto> people;
public AddressDto(String zipCode,
List<PersonDto> people) {
this.zipCode = zipCode;
this.people = people;
}
public String getZipCode() {
return zipCode;
}
public List<PersonDto> getPeople() {
return people;
}
}

View File

@ -0,0 +1,23 @@
/*
* 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._2170.dto;
/**
* @author Filip Hrisafov
*/
public
class PersonDto {
private final String name;
public PersonDto(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,29 @@
/*
* 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._2170.entity;
import java.util.List;
/**
* @author Filip Hrisafov
*/
public class Address {
private final String zipCode;
private final List<Person> people;
public Address(String zipCode, List<Person> people) {
this.zipCode = zipCode;
this.people = people;
}
public String getZipCode() {
return zipCode;
}
public List<Person> getPeople() {
return people;
}
}

View File

@ -0,0 +1,21 @@
/*
* 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._2170.entity;
/**
* @author Filip Hrisafov
*/
public class Person {
private final String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,23 @@
/*
* 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._2170.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.ap.test.bugs._2170.dto.AddressDto;
import org.mapstruct.ap.test.bugs._2170.entity.Address;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
//CHECKSTYLE:OFF
@Mapper(uses = { PersonMapper.class })
//CHECKSTYLE:ON
public interface AddressMapper extends EntityMapper<AddressDto, Address> {
AddressMapper INSTANCE = Mappers.getMapper( AddressMapper.class );
}

View File

@ -0,0 +1,21 @@
/*
* 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._2170.mapper;
import java.util.List;
/**
* @author Filip Hrisafov
*/
public interface EntityMapper<D, E> {
E toEntity(D dto);
D toDto(E entity);
List<E> toEntity(List<D> dtoList);
List<D> toDto(List<E> entityList);
}

View File

@ -0,0 +1,18 @@
/*
* 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._2170.mapper;
import org.mapstruct.Mapper;
import org.mapstruct.ap.test.bugs._2170.dto.PersonDto;
import org.mapstruct.ap.test.bugs._2170.entity.Person;
/**
* @author Filip Hrisafov
*/
@Mapper
public interface PersonMapper extends EntityMapper<PersonDto, Person> {
}