Skip to content

Method already defined when mapping a recursive data structure into a list of the same structure #3532

@codeStev

Description

@codeStev

Expected behavior

I've got the following data structures I would like to map:

class Source {

    private Node node;

    // getters/setters

}

class Target {

    private List<NodeDto> nodeDtoList;

    // getters/setters

}

The recursive structures "Node" and "NodeDto" look like this :

class Node {

    //properties

    List<Node> childNodes;

    // getters/setters

}

class NodeDto {

    //properties

    List<NodeDto> childNodes;

    // getters/setters

}

The map method looks as follows:

@Mapping( target = "nodeList", source = "node.childNodes" )
abstract Target map( Source source);

The reason for mapping it like this is to omit the root element of the tree since it is not wanted in this case.

MapStruct generates the methods with the following signature correctly :

NodeDto nodeToNodeDto( Node node )

List<NodeDto> nodeListToNodeDtoList( List<Node> )

I would expect MapStruct to use the already generated method to map the list and not to generate the same method twice.

Actual behavior

MapStruct generates the method List<NodeDto> nodeListToNodeDtoList( List<Node> ) twice, provoking a compile error in the generated implementation.
If I don't specify the source the root element will get mapped, which is not wanted behaviour.

The Exception Message looks like this :

error: method nodeListToNodeDtoList(List<Node>) is already defined in class TargetMapperImpl

Steps to reproduce the problem

My configuration looks as follows:

@MapperConfig(
        unmappedTargetPolicy = ReportingPolicy.ERROR,
        unmappedSourcePolicy = ReportingPolicy.IGNORE,
        componentModel = MappingConstants.ComponentModel.SPRING,
        injectionStrategy = InjectionStrategy.CONSTRUCTOR,
        nullValueIterableMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface CentralMapperConfig {

}

The problem should be reproducable with the snippets described above. I omitted the getters/setters and the Mapping class to keep it more readable.

MapStruct Version

MapStruct 1.5.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions