Skip to content

(@Condition + @Named) not working with @Mapping#expression+conditionQualifiedByName #3405

@Maze-fr

Description

@Maze-fr

Expected behavior

I create a @Condition method with a @Named.
I annotate a mapping method with @Mapping using an #expression, and the #conditionQualifiedByName from my @Named.

The generated code should condition the mapping of my field with the named condition.

Actual behavior

The generated code doesn't condition the mapping of my field.
However, this line of mapping appears at the end, instead of the beginning, and after a blank line, showing that "something tried to happen".

Steps to reproduce the problem

Here is a simplified version of what I have.
I use Lombok to generate private / getters / setters for fields.

Having this POJO as source (with private fields, getters, setters) :

public class Source {
    String fieldA;
    String fieldB;
}

Having these POJOs as targets (with private fields, getters, setters) :

public class TargetA {
    String fieldA;
    TargetB objectA;
}
public class TargetB {
    String fieldB;
}

Having that mapping :

@Mapper
public interface MyMapper {
    @Mapping(target = "objectA", expression = "java(mapToTargetB(source))", conditionQualifiedByName = "condition")
    TargetA mapToTargetA(Source source);

    TargetB mapToTargetB(Source source);

    @Condition
    @Named("condition")
    default boolean isValid(Source source) {
        return source.getFieldA() != null;
    }
}

MapStruct Version

MapStruct 1.5.5.Final

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