Skip to content

Compile error when using AfterMapping method with Builder and TargetObject #3703

@cgoIT

Description

@cgoIT

Expected behavior

Same behavior as in 1.5.5 (AfterMapping method only for the Builder type is called)

Actual behavior

Both AfterMapping methods are called and code does not compile

Steps to reproduce the problem

The generated code does not compile if I have the following configuration.

If the class names of the source and target class are the same but the classes reside in a different package, the generated code will not compile.

With this mapper

@Mapper
public abstract class ContactMapper {

    @Mapping(target = "nameInA", source = "nameInB")
    @Mapping(target = "mapped", ignore = true)
    public abstract Contact map(com.example.b.Contact contact);

    @AfterMapping
    void afterMapping(@MappingTarget Contact target, com.example.b.Contact contact) {
        target.setMapped();
    }

    @AfterMapping
    void afterMapping(@MappingTarget Contact.ContactBuilder target, com.example.b.Contact contact) {
        target.mapped( true);
    }
}

The generated code will be something like...

import com.example.b.Contact;
import javax.annotation.processing.Generated;

@Generated(
    value = "org.mapstruct.ap.MappingProcessor",
    date = "2024-09-03T09:05:41+0200",
    comments = "version: 1.6.0, compiler: javac, environment: Java 21.0.2 (BellSoft)"
)
public class ContactMapperImpl extends ContactMapper {

    @Override
    public com.example.a.Contact map(Contact contact) {
        if ( contact == null ) {
            return null;
        }

        com.example.a.Contact.ContactBuilder contact1 = com.example.a.Contact.builder();

        contact1.nameInA( contact.getNameInB() );

        afterMapping( contact1, contact );

        // here is the error. contact1.build() will generate a com.example.a.Contact. Due to the import of 
        // com.example.b.Contact the class Contact is from package b.
        Contact contact1Result = contact1.build();

        afterMapping( contact1Result, contact );

        return contact1Result;
    }
}

MapStruct Version

MapStruct 1.6.0

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions