-
-
Notifications
You must be signed in to change notification settings - Fork 920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Presence check method used only once when multiple source parameters are provided #3601
Comments
@thunderhook have you tried this with 1.6.0.Beta2? I think that if you change the |
Yeah i tested this on main. Thanks for the hint, I totally forgot about the new feature. Test is green, however it is is then not picked up when the property mapping takes place: // GENERATED
class Issue3601MapperImpl implements Issue3601Mapper {
@Override
public Target map(Source source, List<String> sourceIds) {
if ( source == null && !isNotEmpty( sourceIds ) ) {
return null;
}
Target target = new Target();
if ( source != null ) {
if ( stringCondition( source.uuid ) ) {
target.currentId = source.uuid;
}
}
// if ( isNotEmpty( sourceIds ) ) { ... } is missing here
List<String> list = sourceIds;
if ( list != null ) {
target.targetIds = new ArrayList<String>( list );
}
return target;
}
} I adapated the test case above and added a mapping with an empty list and now it fails again. |
Good catch @thunderhook. This is indeed a bug in the way we are rechecking the presence of the source parameters. Shall we rename this issue to tackle that bug? |
@filiphr Feel free to rename it as you like, I found no better way to describe it. 👍 Thanks in advance! |
This is trickier than I thought. The This would work with something like: @Mapper
public interface Issue3601Mapper {
Issue3601Mapper INSTANCE = Mappers.getMapper( Issue3601Mapper.class );
@Mapping(target = "currentId", source = "source.uuid")
@Mapping(target = "targetIds", source = "sourceIds", conditionQualifiedByName = "isNotEmpty")
Target map(Source source, List<String> sourceIds);
@Condition
@Named( "isNotEmpty" )
default boolean isNotEmpty(List<String> elements) {
return elements != null && !elements.isEmpty();
}
@SourceParameterCondition
default boolean isNotEmptyParameter(List<String> elements) {
return elements != null && !elements.isEmpty();
}
class Source {
public final String uuid;
public Source(String uuid) {
this.uuid = uuid;
}
}
class Target {
public String currentId;
public List<String> targetIds;
}
} but I'm not sure how to fix it without that. Using |
…ce parameter This is a breaking change, with this change whenever a source parameter is used as a source for a target property the condition has to apply to source parameters and not properties
I actually found a solution for this. Have a look at PR #3620 |
@filiphr Thanks for your work on this! I think there are still several problems when it comes to multiple parameters. Let's try to summarise the recent changes about
I created a branch with some tests and my expectations what should be generated (see 4af76ec).
I tried those tests on your branch and more failures occur. For me it seems that those problems arise only when multiple are present. Could you copy the tests from my branch and let me know what you think? Thanks in advance! |
Expected behavior
Feel free to change the title...
Given the following mapper having a mapping method with two parameters:
The
isNotEmpty
presence check method should only be applied when mappingsourceIds
totargetIds
but notsource.uuid
to
currentId
.Actual behavior
We didn't even find a workaround for this and implemented the method ourself.
I tried to find the culprit but I'm not experienced enough to fix this, but it is because it is picked up with the PresenceCheckMethodResolver here:
mapstruct/processor/src/main/java/org/mapstruct/ap/internal/model/PresenceCheckMethodResolver.java
Lines 125 to 128 in 0a2a0aa
Steps to reproduce the problem
Here is a test mapper and a corresponding unit test:
MapStruct Version
1.5.5, 1.6.0.Beta1
The text was updated successfully, but these errors were encountered: