问题描述:

Mapstruct can't find generic type of an attribute. Let's take an example to clarify what I'm trying to do.

Considering the following dtos :

class ListForm<T> {

private Collection<T> adds;

private Collection<T> changes;

private Collection<T> deletes;

}

class Person {

private String name;

}

class PersonDto {

private String name;

}

I try to implement the following mapper:

@Mapper

public interface OccupantMapper {

ListForm<Person> test(ListForm<PersonDto> person);

Collection<Person> toPersons (Collection<PersonDto> persons);

}

But here is a part of what mapstruct generates :

ListForm<Person> listForm= new ListForm<Person>();

if ( occ.getAjouts() != null ) {

if ( listForm.getAjouts() != null ) {

// problem here, mapstruct can't find the type of the attribute

Collection<T> targetCollection = person.getAdds();

if ( targetCollection != null ) {

listForm.getAjouts().addAll( targetCollection );

}

}

}

As you can see on the code below, mapstruct can't find the type of the target collection. And it doesn't to convert the list of PersonDto to a list of Person. Here is what mapstruct should generates.

Collection<Occupant> targetCollection = toPersons(person.getAdds());

Can you tell me if it's a bug? If there a fix? Or should I do it differently?

Thks,

网友答案:

Can you build MapStruct from the latest master (https://github.com/mapstruct/mapstruct) yourself and see whether this fixes the problem for you? I fixed a related bug last week which I think was essentially the same issue as yours.

You just need to run "mvn clean install" and refer to the SNAPSHOT version of the dependency in your project.

网友答案:

It has been corrected in the MapStruct 1.1.0.Beta1 released

相关阅读:
Top