v1.4.1 Optional<T> mapping
Hi,
today I had to switch to 1.4.1 (from 1.3.1) due to a bug in intellij https://youtrack.jetbrains.com/issue/IDEA-200481 .
I'm now facing another problem with the workaround concerning the way Optional is handled in mapstruct.
This
@Named("unwrap") public <T> T unwrap(Optional<T> optional) { return optional.orElse(null); }
doesn't work anymore...
It gives me this error error: Qualifier error. No method found annotated with @nAmed#value: [ unwrap ]. See https://mapstruct.org/faq/#qualifier for more info.
Is it a regression? or intended? is there another workaround as it seems this feature isn't available.
Linked issue: https://github.com/mapstruct/mapstruct/issues/674
Hey @phmaes ,
Just to be clear that the problem you are seeing with IntelliJ is not a bug in IntelliJ.
Can you please share your entire mapper? Your error seems similar to #2233, which is fixed, but not released yet. You can try with 1.5.0-SNAPSHOT if you can as well.
Hi,
thanks for your reply. Because of an issue with intellij (after last upgrade, project build fails. See link), I had to upgrade mapstruct from 1.3.1 to 1.4.1. And now I have this issue...
Here is a stripped down version of the mapper:
@Mappings({
...
@Mapping(source = "XXX", target = "ZZZ", qualifiedByName = "unwrap"),
...
})
public abstract MyClassTo from(MyClassFrom domain);
@Named("unwrap")
public <T> T unwrap(Optional<T> optional) {
return optional.orElse(null);
}
where XXX -> public Optional<MyClassFrom> getXXX()
Really simple. It perfectly worked in 1.3.1...
I have a very simple case as well:
@Mapping(target="companyId", source="company.id", qualifiedByName = "idToCompany")
PaymentGatewayStrategyDTO paymentGatewayStrategyToDto(PaymentGatewayStrategy paymentGateway);
@Named("idToCompany")
default Company idToCompany(String id) {
Company company = new Company(null, null, null);
company.setId(id);
return company;
}
But I get an error:
java: Qualifier error. No method found annotated with @Named#value: [ idToCompany ]. See https://mapstruct.org/faq/#qualifier for more info.
It used to work with 1.3.1 and does not work with 1.4. As per @filiphr's advice, I checked out the latest master and built the 1.5.0 SNAPSHOT and I still get the same error.
Can't work with the latest IntelliJ because of this error.
I have very simmilar problem too after updating to 1.4.1:
// Use of Reference class was needed here because resultType=GroupBuildRef.class along with unwrapping of Optional
// resulted in NPE in Mapstruct processor
@Mapping(target = "groupBuild", source = "buildSetTask.buildConfigSetRecord", qualifiedBy = Reference.class)
Build fromBuildTask(BuildTask buildTask);
public static <T> T unwrap(Optional<T> optional) {
return (optional != null && optional.isPresent()) ? optional.get() : null;
}
@Override
GroupBuild toDTO(BuildConfigSetRecord dbEntity);
@Override
@Reference
GroupBuildRef toRef(BuildConfigSetRecord dbEntity); // note GroupBuildRef is parent of GroupBuild
this worked in 1.3.1 but now I'm getting:
[ERROR] BuildMapper.java:[204,11] Qualifier error. No method found annotated with: [ @Reference ]. See https://mapstruct.org/faq/#qualifier for more info.
[ERROR] BuildMapper.java:[204,11] Can't map property "Optional<BuildConfigSetRecord> buildTask.buildSetTask.buildConfigSetRecord" to "GroupBuildRef groupBuild". Consider to declare/implement a mapping method: "GroupBuildRef map(Optional<BuildConfigSetRecord> value)".
I tried 1.5.0-SNAPSHOT but the same problem exists
adding
@Reference
public static <T> T unwrapReference(Optional<T> optional) {
return (optional != null && optional.isPresent()) ? optional.get() : null;
}
helped.
@janinko does this mean that we can now close this issue?
It's not ideal to have two same methods just with different annotations, but my problem is solved.
The original issue seems to be with @Named, we don't use that so I don't know if that wokrs with that as well or not.
@filiphr - I am still getting an error for this scenario.
I have a very simple case as well:
@Mapping(target="companyId", source="company.id", qualifiedByName = "idToCompany") PaymentGatewayStrategyDTO paymentGatewayStrategyToDto(PaymentGatewayStrategy paymentGateway); @Named("idToCompany") default Company idToCompany(String id) { Company company = new Company(null, null, null); company.setId(id); return company; }But I get an error:
java: Qualifier error. No method found annotated with @Named#value: [ idToCompany ]. See https://mapstruct.org/faq/#qualifier for more info.It used to work with 1.3.1 and does not work with 1.4. As per @filiphr's advice, I checked out the latest master and built the 1.5.0 SNAPSHOT and I still get the same error.
Can't work with the latest IntelliJ because of this error.
I can put in a new issue if you would prefer that.
Thanks!
@janinko you need to add the annotation due to qualifiedBy = Reference.class in your @Mapping.
@barunmishra13 are you 100% sure that the @Named you are using in your idToCompany is org.mapstruct.Named?
Closing this as it was fixed in #2233