mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2170 Make sure that an import is created for constructor mapping defined variables
This commit is contained in:
parent
609824037b
commit
ed16d62a91
@ -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 ) {
|
||||
|
@ -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" );
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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 );
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
@ -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> {
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user