0

In the beginning I would like to warn that I removed getters and setters in order to make code concise.

I guess code will describe more precisely that many words

//Mapper is obviously as separate class
@Mapper
public interface TestMapper {
    @Mapping(source = "x.field1", target="inner.field1")
    @Mapping(source = "y.field2", target="inner.field2")
    Target map(Source source);
}

class Source {
    private X x;
    private Y y;
}

class X {
    private String field1;
}

class Y {
    private String field2;
}

class Target {
    private Inner inner;
}

class Inner {
    private String field1;
    private String field2;
}

public class Main {
    public static void main(String[] args) {
        Source source = new Source();
        final X x = new X();
        x.setField1("field1");
        Y y = new Y();
        y.setField2("field2");
        source.setX(x);
        source.setY(y);

        final TestMapper mapper = Mappers.getMapper(TestMapper.class);
        Target target = mapper.map(source);
        System.out.println(source.getX().getField1().equals(target.getInner().getField1()));
        System.out.println(source.getY().getField2().equals(target.getInner().getField2()));
    }
}

Such code generate implementation like this:

public class TestMapperImpl implements TestMapper {

    @Override
    public Target map(Source source) {
        if ( source == null ) {
            return null;
        }

        Target target = new Target();

        target.setInner( xToInner( source.getX() ) );
        target.setInner( yToInner( source.getY() ) );

        return target;
    }
    (...)

This obviously causes overriding value of Inner. Which is not what I've expected. Is there a way to make it work? Or is it some bug in mapstruct?

1 Answer 1

3

This is a known bug in MapStruct that was fixed with 1.3.1.Final release.

The relevant issue is mapstruct/mapstruct#1828.

In case you still have the same problem after you upgrade to 1.3.1.Final then please raise a new issue in the MapStruct bug tracker

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.