Skip to content

SONARJAVA-6758: Fix FPs in S5673 for controllers without mappings and redundant annotations - #5925

Merged
romainbrenguier merged 3 commits into
masterfrom
romain/sonarjava-6758
Aug 17, 2026
Merged

SONARJAVA-6758: Fix FPs in S5673 for controllers without mappings and redundant annotations#5925
romainbrenguier merged 3 commits into
masterfrom
romain/sonarjava-6758

Conversation

@romainbrenguier

Copy link
Copy Markdown
Contributor

Summary

  • Controllers without mapping: Before suggesting @Controller or @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.
  • Non-web framework exclusions: Classes implementing ApplicationRunner, CommandLineRunner, HealthIndicator, or ReactiveHealthIndicator, or annotated with @Endpoint, @RestControllerEndpoint, or @ControllerEndpoint are excluded from Controller/RestController suggestions.
  • Redundant annotations: If a class already has a specialized stereotype annotation (@Controller, @RestController, @Service, @Repository) alongside @Component, the rule no longer raises.

Test plan

  • Updated test sample with noncompliant Controller/RestController cases that include request mapping methods
  • Added compliant cases for controllers without request mapping methods
  • Added compliant cases for controllers implementing non-web framework interfaces (ApplicationRunner, CommandLineRunner)
  • Added compliant cases for redundant stereotype annotations
  • All Spring-related tests pass (45 tests)

🤖 Generated with Claude Code

… 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>
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

SONARJAVA-6758

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>
@romainbrenguier
romainbrenguier marked this pull request as ready for review August 14, 2026 08:35

@nathsou nathsou left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 @Component is 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>
@sonarqube-next

Copy link
Copy Markdown

nathsou

This comment was marked as resolved.

@nathsou
nathsou self-requested a review August 17, 2026 08:48
@romainbrenguier
romainbrenguier merged commit c49527a into master Aug 17, 2026
19 checks passed
@romainbrenguier
romainbrenguier deleted the romain/sonarjava-6758 branch August 17, 2026 11:48
@gitar-bot

gitar-bot Bot commented Aug 17, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 1 resolved / 1 findings

Refines rule S5673 to check for request mapping methods, exclude non-web framework classes, and handle redundant stereotype annotations, addressing the class-level @RequestMapping detection finding. No issues found.

✅ 1 resolved
Edge Case: Class-level @RequestMapping not detected as a mapping method

📄 java-checks/src/main/java/org/sonar/java/checks/spring/SpringComponentSpecializationCheck.java:101-112
hasRequestMappingMethod only inspects method-level annotations, so a controller that declares its request mapping at the class level (e.g. @component @RequestMapping("/api") with handler methods that carry no mapping annotation, or methods inherited from a base class) will no longer be flagged. This trades the fixed FPs for potential false negatives on legitimate controllers. Consider also checking for a class-level @RequestMapping / mapping annotation before concluding the class handles no HTTP requests.

Implementation Status 🟡 0 / 1 issues implemented
SONARJAVA-6758 — 0 / 3 objectives

The PR diff is empty and does not implement any of the objectives.

  • ⬜ Check if a specialized stereotype is already present and skip raising redundant annotations in S5673
  • ⬜ Verify that a class has at least one request mapping annotation before suggesting @Controller in S5673
  • ⬜ Exclude classes implementing framework interfaces or annotations that legitimately require @Component (such as HealthIndicator, @Endpoint, ApplicationRunner) from S5673
Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing less information.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants