Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SONARJAVA-5293 Modify rule S6856 to also cover opposite case #4619

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions rules/S6856/java/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ They are part of the Spring Web module and are commonly used to define the route

If a method has a path template containing a placeholder, like "/api/resource/{id}", and there's no `@PathVariable` annotation on a method parameter to capture the id path variable, Spring will disregard the id variable.

This rule will raise an issue if a method has a path template with a placeholder, but no corresponding `@PathVariable`, or vice-versa.

== How to fix it

=== Code examples
Expand All @@ -19,6 +21,11 @@ If a method has a path template containing a placeholder, like "/api/resource/{i
public ResponseEntity<String> getResourceById(Long id) { // Noncompliant - The 'id' parameter will not be automatically populated with the path variable value
return ResponseEntity.ok("Fetching resource with ID: " + id);
}

@GetMapping("/api/asset/")
public ResponseEntity<String> getAssetById(@PathVariable Long id) { // Noncompliant - The 'id' parameter does not have a corresponding placeholder
return ResponseEntity.ok("Fetching asset with ID: " + id);
}
----

==== Compliant solution
Expand All @@ -29,6 +36,11 @@ public ResponseEntity<String> getResourceById(Long id) { // Noncompliant - The '
public ResponseEntity<String> getResourceById(@PathVariable Long id) { // Compliant
return ResponseEntity.ok("Fetching resource with ID: " + id);
}

@GetMapping("/api/asset/{id}")
public ResponseEntity<String> getAssetById(@PathVariable Long id) {
return ResponseEntity.ok("Fetching asset with ID: " + id);
}
----

== Resources
Expand Down
Loading