From 028090a9d2532f896dc97d6b540e0b1b50ff4094 Mon Sep 17 00:00:00 2001 From: Leonardo Pilastri Date: Mon, 27 Jan 2025 13:29:16 +0100 Subject: [PATCH] SONARJAVA-5293 Modify rule S6856 to also cover opposite case --- rules/S6856/java/rule.adoc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rules/S6856/java/rule.adoc b/rules/S6856/java/rule.adoc index 4f688833ce6..a8df7723efd 100644 --- a/rules/S6856/java/rule.adoc +++ b/rules/S6856/java/rule.adoc @@ -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 does not have corresponding `@PathVariable` annotations and path templates placeholders. + == How to fix it === Code examples @@ -19,6 +21,11 @@ If a method has a path template containing a placeholder, like "/api/resource/{i public ResponseEntity 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 getAssetById(@PathVariable Long id) { // Noncompliant - The 'id' parameter does not have a corresponding placeholder + return ResponseEntity.ok("Fetching asset with ID: " + id); +} ---- ==== Compliant solution @@ -29,6 +36,11 @@ public ResponseEntity getResourceById(Long id) { // Noncompliant - The ' public ResponseEntity getResourceById(@PathVariable Long id) { // Compliant return ResponseEntity.ok("Fetching resource with ID: " + id); } + +@GetMapping("/api/asset/{id}") +public ResponseEntity getAssetById(@PathVariable Long id) { + return ResponseEntity.ok("Fetching asset with ID: " + id); +} ---- == Resources