-
Hi, I'm currently experimenting mapstruct and I don't understand a reported error at compile time. I have an interface GrantingResponseMapper annotated with @Mapper(unmappedTargetPolicy = ReportingPolicy.ERROR) Here is the class: @Mapper(unmappedTargetPolicy = ReportingPolicy.ERROR)
public interface GrantingResponseMapper {
GrantingResponseMapper INSTANCE = Mappers.getMapper(GrantingResponseMapper.class);
@Mapping(target = "decisionId", ignore = true)
GrantingResponseVo mapFrom(GrantingDecisionResponse response);
@AfterMapping
default void mapSecondaryTelemDecisions(GrantingDecisionResponse source, @MappingTarget GrantingResponseVo.GrantingResponseVoBuilder target) {
List<TelemDecision> telemDecisionsSecondaryList = source.getTelemDecisionsSecondaryList();
if (CollectionUtils.isNotEmpty(telemDecisionsSecondaryList)) {
if (!telemDecisionsSecondaryList.isEmpty()) {
target.secondaryTelemDecision1(telemDecisionsSecondaryList.get(0));
}
if (telemDecisionsSecondaryList.size() > 1) {
target.secondaryTelemDecision2(telemDecisionsSecondaryList.get(1));
}
if (telemDecisionsSecondaryList.size() > 2) {
target.secondaryTelemDecision3(telemDecisionsSecondaryList.get(2));
}
if (telemDecisionsSecondaryList.size() > 3) {
target.secondaryTelemDecision4(telemDecisionsSecondaryList.get(3));
}
}
}
} If I compile the class an error is reported that fields secondaryTelemDecision1, secondaryTelemDecision2, secondaryTelemDecision3, and secondaryTelemDecision4 are not mapped. If I remove the unmappedTargetPolicy the code compiles correctly, and I can see that AfterMapping method is called:
Maybe the mapping is not correctly done on my side. If this is the case, what would be the correct mapping to implement to prevent from having a compilation error? Thanks for your help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The reason why you are getting the error is due to the fact that MapStruct is not able to map those 4 fields. MapStruct has no way of knowing what is being done in the If you want to use I don't know how your objects are created, but in your case I would not expose setters for the public class GrantingResponseVo {
public TelemDecision getSecondaryTelemDecision1() {
return telemDecisionsSecondaryList != null && !telemDecisionsSecondaryList.isEmpty() ? telemDecisionsSecondaryList.get(0) : null;
}
} |
Beta Was this translation helpful? Give feedback.
The reason why you are getting the error is due to the fact that MapStruct is not able to map those 4 fields. MapStruct has no way of knowing what is being done in the
@AfterMapping
.If you want to use
ReportingPolicy.ERROR
as the unmapped target policy then you should ignore explicitly those 4 methods via@Maping(target = "secondaryTelemDecision1", ignore = true)
I don't know how your objects are created, but in your case I would not expose setters for the
secondaryTelemDecision1
and the rest. I would instead do