SONARJAVA-6758: Fix FPs in S5673 for controllers without mappings and redundant annotations - #5925
Conversation
… redundant annotations - Require request mapping annotations before suggesting @Controller/@RestController - Skip raising when a specialized stereotype annotation is already present alongside @component - Exclude classes implementing non-web framework interfaces (ApplicationRunner, CommandLineRunner, HealthIndicator) - Exclude classes annotated with actuator endpoint annotations Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Extract duplicated "Controller" and "RestController" string literals into constants to fix S1192 issues. Add test cases for HealthIndicator, ReactiveHealthIndicator, and @endpoint to improve coverage on new code above 90% threshold. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
RSPEC is now inconsistent with the analyzer
The ticket also asks to improve S5673 RSPEC (it was judged not actionable). There is no RSPEC PR, and the published examples are now wrong.
rules/S5673/java/rule.adoc still shows an empty FooBarRestController as noncompliant. After this change that class would not raise, which is why the unit test had to add @GetMapping to keep it noncompliant.
Please open a sibling RSPEC PR that:
- puts a mapping method on the Controller/RestController noncompliant example
- documents the new exceptions (no mapping methods, already-specialized stereotypes, actuator/startup types)
- aligns the issue message with the implementation (“or rename this type if
@Componentis intentional”)
…d actuator endpoint tests Walk the superclass hierarchy in hasRequestMappingMethod() to detect inherited mapping annotations, fixing false negatives for @component subclasses of base controllers with @GetMapping/@PostMapping methods. Add test cases for @RestControllerEndpoint and @ControllerEndpoint annotations to verify they are correctly excluded from the rule. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Code Review ✅ Approved 1 resolved / 1 findingsRefines rule S5673 to check for request mapping methods, exclude non-web framework classes, and handle redundant stereotype annotations, addressing the class-level ✅ 1 resolved✅ Edge Case: Class-level @RequestMapping not detected as a mapping method
Implementation Status 🟡 0 / 1 issues implemented⬜ SONARJAVA-6758 — 0 / 3 objectivesThe PR diff is empty and does not implement any of the objectives.
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |




Summary
@Controlleror@RestController, the rule now verifies the class has at least one method with a request mapping annotation (@RequestMapping,@GetMapping,@PostMapping,@PutMapping,@DeleteMapping,@PatchMapping). Classes named "Controller" that don't handle HTTP requests no longer trigger the rule.ApplicationRunner,CommandLineRunner,HealthIndicator, orReactiveHealthIndicator, or annotated with@Endpoint,@RestControllerEndpoint, or@ControllerEndpointare excluded from Controller/RestController suggestions.@Controller,@RestController,@Service,@Repository) alongside@Component, the rule no longer raises.Test plan
🤖 Generated with Claude Code