Java annotations reported as code duplicate

  • versions used (SonarQube, Scanner LTS, language analyzer JAVA)

Hi, I have some false positive with sonarqube and JAVA analyzer.

In order to improve our swagger generated documentation (based on java annotations) we had to annotate multiple endpoints with such annotations:

  @Operation(summary = "Do something")
  @ApiResponse(responseCode = "200", description = "Successfull")
  @ApiResponse(responseCode = "400", description = "Unable to do error")
  @ApiResponse(responseCode = "403", description = "Not Enough Permissions")
  @ApiResponse(responseCode = "404", description = "whatever not found")

Sonarqube report it as code duplicate.

Is there a way to tell sonarqube to not report duplication on annotations ?

I mean it’s a real problem. It is not really acceptable to have to manually approve all these things in our usage of sonarqube. This prevent us to merge pull requests as we really like to use sonarqube as a guardian in our code. And if a sonarqube admin have to come always to approve false positive like that… will be a night mare.

Any help will be very appreciated :slight_smile: thank you in advance

Hi @svermeille,

There’s no way in SonarQube to ignore duplication on Java annotations.
Perhaps you can create your own meta-annotation public @interface MyDefaultApiResponse that aggregates annotations that you always duplicate. I saw this kind of solution in discussions about “How to avoid repeating yourself with Swagger annotations?” like here:

Good luck,

Thank you @alban.auzeill , it seems to be a good workaround for these issues.

Have a good day

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.