RSPEC-107 Methods should not have too many parameters with Spring shortcut annotations (@GetMapping, @PostMapping, etc)

java

(Laszlo Hathazy) #1

This issue found using Version 7.0 (build 36138), but could be reproduced also with the latest version (as the code for the rule on master seems to allow this).

Create any Java Spring REST controller, having more than 7 parameters. If the method is annotated with @RequestMapping(…) it will be ignored by the Sonar rule, but if using any of the shortcut annotations provided by Spring, like @GetMapping, then Sonar will complain about having too many parameters.

Code sample:

“Compliant” code:

public void foo(String param1, String param2, String param3, String param4, String param5, String param6, String param7, String param8) {
}

“Non-compliant” code:

public void foo(String param1, String param2, String param3, String param4, String param5, String param6, String param7, String param8) {
}

A fix is to whitelist the shortcut annotations, like on the following gist (lines 56-62):


(Michael Gumowski) #2

Hello @hathazylaszlojr,

Thanks for the feedback. Indeed, implementation of S107 in SonarJava does not cover all these annotations, which should be handled exactly like Spring’s RequestMapping.

I created the following ticket to handle it: SONARJAVA-2874

Now, feel free to open a Pull Request on SonarJava repository with the fix (that you already have) and some updated unit test. We will then review and merge it eventually. Don’t forget to have a look at our guidelines, especially regarding ruling, even if I don’t expect to get some issues disappearing there.

Cheers,
Michael


(Laszlo Hathazy) #3

Hi @Michael,

Pull request opened for this issue: https://github.com/SonarSource/sonar-java/pull/2195

Thanks,
Laszlo


(Michael Gumowski) #4

Nice job! Thanks for the contribution!
Merged.