Replies: 1 comment
|
The combination is supported, but the current qualifier semantics also apply to the default value.
Your converter only covers the first path. Add a second overload with the same qualifier: public class DataConverter {
@Named("quantityConverter")
public String quantityConverter(DataWrapper value) {
if (value == null || value.getCount() == null) {
return null;
}
return value.getCount().toString();
}
@Named("quantityConverter")
public String quantityConverter(String value) {
return value;
}
}The mapper can remain unchanged: @Mapping(
target = "quantity",
source = "dataWrapper",
qualifiedByName = "quantityConverter",
defaultValue = "1"
)
TargetObject convert(SourceObject source);I reproduced this with MapStruct 1.6.3. It generates the two intended branches: if (source.getDataWrapper() != null) {
target.setQuantity(dataConverter.quantityConverter(source.getDataWrapper()));
}
else {
target.setQuantity(dataConverter.quantityConverter("1"));
}So this is not an unsupported |
Uh oh!
There was an error while loading. Please reload this page.
Title: Compilation fails when combining source, qualifiedByName, and defaultValue in @mapping (Regression from 1.1.0.Final to 1.6.2)
Description
When using the
@Mappingannotation with all three attributes (source,qualifiedByName, anddefaultValue) together, MapStruct 1.6.2 fails to compile with a qualifier error. This combination worked correctly in MapStruct 1.1.0.Final.Steps to Reproduce
Expected Behavior
The code should compile successfully. The generated mapper should:
This was the behavior in MapStruct 1.1.0.Final.
Actual Behavior
Compilation fails with:
Minimal Reproducible Example
Workarounds
Workaround 1: Remove defaultValue
Workaround 2: Handle default value in the converter method
Environment
Generated Code (MapStruct 1.1.0.Final - Working)
Questions
Additional Context
This issue was discovered during a JDK 8 to JDK 17 migration where MapStruct was upgraded from 1.1.0.Final to 1.6.2. The code that previously compiled successfully now fails.
This is a common use case where:
A complete reproducible example is available in the attached MapStructIssueDemo.java file.
All reactions