SonarQube Duplication Issue with MapStruct Mappers

Hi,

I hope this message finds you well.

I’m currently working on a Java project where SonarQube has identified duplicated code blocks within two different MapStruct mappers.

These mappers are used in distinct contexts, different source and target.

It seems that SonarQube may not fully understand MapStruct’s semantics, object differences, or annotation meanings, leading to these reported duplications.

Could you please advise if there’s a way to exclude only the specific lines identified as duplicated by SonarQube, rather than needing to exclude entire files or larger blocks?

// Mapper 1
@Mapper
public interface MyMapper1 {

    
    @Mapping(source = "filedA.sourceField1", target = "targetFieldA")
    @Mapping(source = "fieldA.sourceField2", target = "targetFieldB")
    TargetObject1 map(SourceObject1 source);
}

// Mapper 2
@Mapper
public interface MyMapper2 {

    @Mapping(source = "sourceField1", target = "targetFieldA")
    @Mapping(source = "sourceField2", target = "targetFieldB")
    TargetObject2 map(SourceObject2 source);
}

We are using

  • SonarQube Server v2025.1.3
  • sonar-scanner to launch analysis on CI

Thank you for your assistance.

Best regards,

Hi,

Unfortunately, the duplication detection algorithm ignores the values of strings, which is why this parses as a duplication. Also unfortunately, there’s no way to exclude part of a file from duplication detection. Your best bet is to exclude the (whole) file.

 
HTH,
Ann