How to ignore a source property? #3212
-
|
I must be missing something obvious. We have the @mapping(ignore=true, target="targetField"). But no @mapping(ignore=true, source="sourceField"). I use ReportingPolicy.ERROR setting, because I want to catch everything. Then I get errors: @Mapper(unmappedSourcePolicy=ReportingPolicy.ERROR, unmappedTargetPolicy=ReportingPolicy.ERROR)
interface IgnorePropertyProblemMapping {
public static class ModelA{
public String modelA_1;
public String modelA_Ignore;
}
public class ModelB{
public String modelB_1;
public String modelB_Ignore;
}
@Mapping(source = "modelA_1", target = "modelB_1")
@Mapping(ignore=true, target = "modelB_Ignore")
// @Mapping(source = "modelA_Ignore", target="nothingToMapTo")
ModelB aToB(ModelA a); //This is an error: Unmapped property modelA_Ignore
@InheritInverseConfiguration(name="aToB")
@Mapping(ignore=true, target = "modelA_Ignore")
ModelA bToA(ModelB a); //This is an error: Unmapped property modelB_Ignore
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
A source property can be ignored by using |
Beta Was this translation helpful? Give feedback.
-
|
Thank you. One step forward. Sadly, more steps to go, lol. This solution does not extend through the @InheritConfiguration. @Mapper(unmappedSourcePolicy=ReportingPolicy.ERROR, unmappedTargetPolicy=ReportingPolicy.ERROR)
interface IgnorePropertyProblemMapping {
public static class ModelA{
public String modelA_1;
public String modelA_Ignore;
}
public class ModelB{
public String modelB_1;
public String modelB_Ignore;
}
@Mapping(source = "modelA_1", target = "modelB_1")
@Mapping(ignore=true, target = "modelB_Ignore")
@BeanMapping(ignoreUnmappedSourceProperties = {"modelA_Ignore"})
ModelB aToB(ModelA a); //NO LONGER AN ERROR
@InheritInverseConfiguration(name="aToB")
@BeanMapping(ignoreUnmappedSourceProperties = {"modelB_Ignore"})
@Mapping(ignore=true, target = "modelA_Ignore")
ModelA bToA(ModelB a);//NO LONGER AN ERROR
public static class ModelA2 extends ModelA{}
public class ModelB2 extends ModelB{}
@InheritConfiguration(name ="aToB")
ModelB2 aToB2(ModelA2 a); //COMPILE ERROR. Does not inherit the ignoreUnmappedSourceProperties.
@InheritConfiguration(name="bToA")
ModelA2 bToA2(ModelB2 a); //COMPILE ERROR. Does not inherit the ignoreUnmappedSourceProperties.
} |
Beta Was this translation helpful? Give feedback.
-
|
Followup on the code fix. I confirm that the reported issue is now working. Unfortunately, ignoreUnmappedSourceProperties does not accumulate in the inheritance. If the inheriting code declares another ignoreUnmappedSourceProperties, it overrides the values, rather than adding to them. Is this intentional, an error, or optional? See code sample below: @Mapper(unmappedSourcePolicy = ReportingPolicy.ERROR, unmappedTargetPolicy = ReportingPolicy.ERROR)
interface IgnorePropertyProblemMapping {
public static class ModelASuper {
public String superA;
public String superAIgnore;
}
public class ModelBSuper {
public String superB;
public String superBIgnore;
}
@Mapping(source = "superA", target = "superB")
@Mapping(ignore = true, target = "superBIgnore")
@BeanMapping(ignoreUnmappedSourceProperties = { "superAIgnore" })
ModelBSuper aToBSuper(ModelASuper a); // everything mapped
@InheritInverseConfiguration(name = "aToBSuper")
@BeanMapping(ignoreUnmappedSourceProperties = { "superBIgnore" })
@Mapping(ignore = true, target = "superAIgnore")
ModelASuper bToASuper(ModelBSuper a); // everything mapped
public static class ModelA2 extends ModelASuper {
public String subA;
public String subAIgnore;
}
public class ModelB2 extends ModelBSuper {
public String subB;
public String subBIgnore;
}
@Mapping(source = "subA", target = "subB")
@InheritConfiguration(name = "aToBSuper")
@BeanMapping(ignoreUnmappedSourceProperties = { "subAIgnore" })
ModelB2 aToBSub(ModelA2 a); // COMPILE ERROR. Does not inherit @BeanMapping(ignoreUnmappedSourceProperties = { "superAIgnore" }) from aToBSuper
@InheritInverseConfiguration(name = "aToBSub")
@InheritConfiguration(name = "bToASuper")
@BeanMapping(ignoreUnmappedSourceProperties = { "subBIgnore" })
ModelA2 bToASub(ModelB2 a); // COMPILE ERROR. Does not inherit @BeanMapping(ignoreUnmappedSourceProperties = { "superBIgnore" }) from bToASuper
} |
Beta Was this translation helpful? Give feedback.
A source property can be ignored by using
@BeanMapping(ignoreUnmappedSourceProperties = "modelA_Ignore")and@BeanMapping(ignoreUnmappedSourceProperties = "modelB_Ignore")