Sihem Hcine
Sihem Hcine

Reputation: 1129

Why Mapstruct cannot generate fields mapping

I'm using mapstruct to do the mapping between two calsses : Candidate and CandidateDTO .

My mapper interface is like that:

@Mapper
public interface CandidateMapper {
    CandidateDTO toCandidateDTO(Optional<CandidateEntity> candidateEntity);
}

And the generated source is like that :

public class CandidateMapperImpl implements CandidateMapper {
    @Override
    public CandidateDTO toCandidateDTO(Optional<CandidateEntity> candidateEntity) {
        if ( candidateEntity == null ) {
            return null;
        }
        CandidateDTO candidateDTO = new CandidateDTO();
        return candidateDTO;
    }
}

My problem here is that when mapping i get all DTO fields null because the mapping field is not generated.

Any help please.

Upvotes: 1

Views: 521

Answers (1)

Filip
Filip

Reputation: 21461

This is not yet supported out of the box by MapStruct. Have a look at issue mapstruct/mapstruct#674 in our issue tracker.

What you can do though is use a default custom method.

@Mapper
public interface CandidateMapper {

    default CandidateDTO toCandidateDTO(Optional<CandidateEntity> candidateEntity) {
        return toCandidateDTO(candidateEntity.orElse(null);
    }

    CandidateDTO toCandidateDTO(CandidateEntity candidateEntity);
}

Upvotes: 2

Related Questions