1

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.

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.