Mapstruct ignore field, warning unmapped target properties

708 Views Asked by At

I am using mapstruct in Java and I need to get rid of warning: Warning: Unmapped target properties: "t3". I have the following structure:

class A {
    String t1;
    String t2;
}

class B {
    String t1;
    String t2;
    String t3;
}

I am mapping with mapstruct from B to A class.

B toB(A a);

I would like to ignore just one field (t3), but @Mapping required target property, so I cannot use @Mapping(source = "t3", ignore = true). Is there any solution for that?

TRYING: I was trying to use @Mapping and use unmappedTargetPolicy = ReportingPolicy.IGNORE, but I want to ignore only specific field.

EXPECT: I would like to ignore just one field (t3), but @Mapping required target property, so I cannot use @Mapping(source = "t3", ignore = true). Is there any solution for that?

2

There are 2 best solutions below

4
Gabriela83 On

You can use @Mapping(target = "t3", ignore = true) even if it belongs to the "source" not the "target" of the mapping.

0
atish.s On

I made unmapped policies (for source and target) to throw errors and ignore the field t3 for both mappings.

It worked fine with my testing.

import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;


@Mapper(unmappedTargetPolicy = ReportingPolicy.ERROR, unmappedSourcePolicy = ReportingPolicy.ERROR)
public interface ABMapper {

    ABMapper mapper = Mappers.getMapper(ABMapper.class);

    @Mapping(source = "t1", target = "t1")
    @Mapping(source = "t2", target = "t2")
    @Mapping(target = "t3", ignore = true)
    B toB(A a);

    @Mapping(source = "t1", target = "t1")
    @Mapping(source = "t2", target = "t2")
    @BeanMapping(ignoreUnmappedSourceProperties = { "t3" })
    A toA(B b);
}

Source: https://github.com/mapstruct/mapstruct/issues/1718