Skip to content

Cannot map a collection with named mapping method #2250

@MaciejDobrowolski

Description

@MaciejDobrowolski

After upgrading to mapstruct 1.4.1 we are having some problems with mapping collections using named mapping methods. I suspect this is a bug b/c it worked fine for mapstruct 1.2.0.

The full example can be found here: https://gist.github.com/MaciejDobrowolski/6a9b9bedfd873e8ffd822301ab333be4

I am getting the following errors:

Demo.java:36: error: Qualifier error. No method found annotated with @Named#value: [ fromBase ]. See https://mapstruct.org/faq/#qualifier for more info.
House toEntity(HouseDto houseDto);
^
Demo.java:36: error: Can't map property "List dogs" to "List dogs". Consider to declare/implement a mapping method: "List map(List value)".
House toEntity(HouseDto houseDto);
^

+short in-ticket summary:

Having the following model:

class BaseEntity {
    private Long id;
}

class Dog extends BaseEntity {
    private String name;
}

class House extends BaseEntity {
    private List<Dog> dogs;
}

class BaseDto {
    private Long id;
}

class HouseDto extends BaseDto {
    private List<BaseDto> dogs;
}

And the following mapper:

@Named("fromBase")
public <T> T fromBase(BaseDto baseDto, @TargetType Class<T> entityClazz) {
    return Optional.ofNullable(baseDto)
            .map(BaseDto::getId)
             // somehow create instance of T from id
            .orElse(null);
}

The above mapper is not used by the one below:

@Mapping(target = "dogs", qualifiedByName = "fromBase")
House toEntity(HouseDto houseDto);

If I only change dogs to be a single dog (without List), it works as expected.

update: I have just removed generics and @TargetType from fromBase mapping method and it still returns the same error

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions