mapstruct icon indicating copy to clipboard operation
mapstruct copied to clipboard

v1.4.1 Optional<T> mapping

Open phmaes opened this issue 6 years ago • 11 comments

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

phmaes avatar Dec 03 '20 14:12 phmaes

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.

filiphr avatar Dec 03 '20 16:12 filiphr

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...

phmaes avatar Dec 04 '20 10:12 phmaes

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.

barunmishra13 avatar Dec 08 '20 05:12 barunmishra13

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)".

janinko avatar Dec 08 '20 10:12 janinko

I tried 1.5.0-SNAPSHOT but the same problem exists

janinko avatar Dec 08 '20 10:12 janinko

adding

    @Reference
    public static <T> T unwrapReference(Optional<T> optional) {
        return (optional != null && optional.isPresent()) ? optional.get() : null;
    }

helped.

janinko avatar Dec 08 '20 10:12 janinko

@janinko does this mean that we can now close this issue?

filiphr avatar Dec 11 '20 20:12 filiphr

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.

janinko avatar Dec 14 '20 09:12 janinko

@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!

barunmishra13 avatar Dec 14 '20 09:12 barunmishra13

@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?

filiphr avatar Dec 15 '20 19:12 filiphr

Closing this as it was fixed in #2233

filiphr avatar Jan 31 '21 11:01 filiphr