mapstruct / mapstruct-idea

An IntelliJ IDEA plugin for working with MapStruct
Other
141 stars 38 forks source link

Intellij plugin reports unmapped properties which are mapped in custom annotation #118

Closed cvetan closed 1 year ago

cvetan commented 1 year ago

In one mapper class we have multiple same mappings used in couple of mapping methods. For this we made custom annotations which contain these mappings. Plugin doesn't recognize those attributes and shows warning for "unmapped" fields, even tough fields are mapped and code built successfully.

thunderhook commented 1 year ago

Can you provide a simple example (minimal reproduciable at best)?

thunderhook commented 1 year ago

Do you mean mapping composition as seen here in the reference documentation?

cvetan commented 1 year ago

I'll send you example from my code. Just to get to my computer.

Chr3is commented 1 year ago

same applies to mappers who inherit configuration from another mapper e.g.

import org.mapstruct.InheritConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Mapper
public interface Demo {

    @Mapping(source = "fieldOne", target = "field1")
    @Mapping(source = "fieldTwo", target = "field2")
    TargetObject1 sourceToTarget1(SourceObject address);

    @InheritConfiguration(name = "sourceToTarget1")
    @Mapping(source = "fieldThree", target = "field3")
    TargetObject2 sourceToTarget2(SourceObject address);

    @Data
    class SourceObject {

        private String fieldOne;

        private String fieldTwo;

        private String fieldThree;
    }

    @Data
    class TargetObject1 {

        protected String field1;

        protected String field2;
    }

    @EqualsAndHashCode(callSuper = true)
    @Data
    class TargetObject2 extends TargetObject1 {

        private String field3;
    }
}

Generated code

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2023-02-06T14:55:49+0100",
    comments = "version: 1.5.3.Final, compiler: javac, environment: Java 17.0.6 (Eclipse Adoptium)"
)
public class DemoImpl implements Demo {

    @Override
    public TargetObject1 sourceToTarget1(SourceObject address) {
        if ( address == null ) {
            return null;
        }

        TargetObject1 targetObject1 = new TargetObject1();

        targetObject1.setField1( address.getFieldOne() );
        targetObject1.setField2( address.getFieldTwo() );

        return targetObject1;
    }

    @Override
    public TargetObject2 sourceToTarget2(SourceObject address) {
        if ( address == null ) {
            return null;
        }

        TargetObject2 targetObject2 = new TargetObject2();

        targetObject2.setField3( address.getFieldThree() );
        targetObject2.setField1( address.getFieldOne() );
        targetObject2.setField2( address.getFieldTwo() );

        return targetObject2;
    }
}
Bildschirm­foto 2023-02-06 um 15 02 43

Intellij: Build #IU-223.8617.56, built on January 26, 2023 MapStruct Plugin: 1.4.0

filiphr commented 1 year ago

Closing this as a duplicate of #65