From de4b135b0ed898b72beb0431117b84db35bd6f6d Mon Sep 17 00:00:00 2001 From: Luqmansonar Date: Fri, 13 Nov 2026 10:42:55 +0100 Subject: [PATCH 1/3] SONARJAVA-7030 S4684 should support rule parameters declaring annotations/types resolved from server-side context Co-Authored-By: Claude Sonnet 5.5 --- ...AsRequestParameterCheckExcludedSample.java | 37 +++++++++++++++++++ ...tityUsedAsRequestParameterCheckSample.java | 10 +++++ ...tentEntityUsedAsRequestParameterCheck.java | 34 +++++++++++++++-- ...EntityUsedAsRequestParameterCheckTest.java | 23 ++++++++++++ 4 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 java-checks-test-sources/default/src/main/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckExcludedSample.java diff --git a/java-checks-test-sources/default/src/main/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckExcludedSample.java b/java-checks-test-sources/default/src/main/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckExcludedSample.java new file mode 100644 index 00000000000..4629097abe6 --- /dev/null +++ b/java-checks-test-sources/default/src/main/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckExcludedSample.java @@ -0,0 +1,37 @@ +package checks.spring; + +import javax.persistence.Entity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +class PersistentEntityUsedAsRequestParameterCheckExcludedSample { + @Entity + class Campaign { + } + + @Entity + class UserDetail { + } + + @Entity + class Other { + } + + @interface PartnerCampaign { + } + + @Controller + class SomeController { + @GetMapping("/a") + public void annotated(@PartnerCampaign Campaign campaign) { // Compliant, annotation declared + } + + @GetMapping("/b") + public void typed(Other other, UserDetail userDetail) { // Noncompliant + } + + @GetMapping("/c") + public void notDeclared(Campaign campaign) { // Noncompliant + } + } +} diff --git a/java-checks-test-sources/default/src/main/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckSample.java b/java-checks-test-sources/default/src/main/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckSample.java index 4e97497f985..8e53a8fb3e4 100644 --- a/java-checks-test-sources/default/src/main/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckSample.java +++ b/java-checks-test-sources/default/src/main/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckSample.java @@ -203,4 +203,14 @@ public String saveForLater2(WishConstruct wish) { // Compliant, WishConstruct ha return ""; } } + + @interface PartnerCampaign { + } + + @Controller + class CustomAnnotationController { + @GetMapping("/campaign") + public void campaign(@PartnerCampaign Foo foo) { // Noncompliant + } + } } diff --git a/java-checks/src/main/java/org/sonar/java/checks/spring/PersistentEntityUsedAsRequestParameterCheck.java b/java-checks/src/main/java/org/sonar/java/checks/spring/PersistentEntityUsedAsRequestParameterCheck.java index b340ac36644..5708f2133d5 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/spring/PersistentEntityUsedAsRequestParameterCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/spring/PersistentEntityUsedAsRequestParameterCheck.java @@ -17,8 +17,11 @@ package org.sonar.java.checks.spring; import java.util.Collections; +import java.util.Arrays; import java.util.List; +import java.util.stream.Stream; import org.sonar.check.Rule; +import org.sonar.check.RuleProperty; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.semantic.Symbol; import org.sonar.plugins.java.api.tree.MethodTree; @@ -28,6 +31,20 @@ @Rule(key = "S4684") public class PersistentEntityUsedAsRequestParameterCheck extends IssuableSubscriptionVisitor { + @RuleProperty( + key = "excludedAnnotations", + description = "Comma-separated list of fully qualified annotation types. Parameters carrying one of them are not reported. " + + "This is an assertion about your own argument resolvers, which the analyzer does not verify.", + defaultValue = "") + public String excludedAnnotations = ""; + + @RuleProperty( + key = "excludedTypes", + description = "Comma-separated list of fully qualified parameter types. Parameters of one of these types are not reported. " + + "This is an assertion about your own argument resolvers, which the analyzer does not verify.", + defaultValue = "") + public String excludedTypes = ""; + @Override public List nodesToVisit() { return Collections.singletonList(Tree.Kind.METHOD); @@ -63,7 +80,8 @@ public void visitNode(Tree tree) { if (isRequestMappingAnnotated(methodSymbol)) { methodTree.parameters().stream() - .filter(PersistentEntityUsedAsRequestParameterCheck::hasNoAllowedAnnotations) + .filter(this::hasNoAllowedAnnotations) + .filter(this::isNotExcludedType) .filter(PersistentEntityUsedAsRequestParameterCheck::isPersistentEntity) .filter(PersistentEntityUsedAsRequestParameterCheck::hasNoCustomSerialization) .forEach(p -> reportIssue(p.simpleName(), "Replace this persistent entity with a simple POJO or DTO object.")); @@ -78,8 +96,18 @@ private static boolean isPersistentEntity(VariableTree variableTree) { return ENTITY_ANNOTATIONS.stream().anyMatch(variableTree.type().symbolType().symbol().metadata()::isAnnotatedWith); } - private static boolean hasNoAllowedAnnotations(VariableTree variableTree) { - return PARAMETER_ANNOTATION_EXCEPTIONS.stream().noneMatch(variableTree.symbol().metadata()::isAnnotatedWith); + private boolean hasNoAllowedAnnotations(VariableTree variableTree) { + return Stream.concat(PARAMETER_ANNOTATION_EXCEPTIONS.stream(), split(excludedAnnotations)) + .noneMatch(variableTree.symbol().metadata()::isAnnotatedWith); + } + + private boolean isNotExcludedType(VariableTree variableTree) { + String typeName = variableTree.type().symbolType().fullyQualifiedName(); + return split(excludedTypes).noneMatch(typeName::equals); + } + + private static Stream split(String commaSeparated) { + return Arrays.stream(commaSeparated.split(",")).map(String::trim).filter(s -> !s.isEmpty()); } private static boolean hasNoCustomSerialization(VariableTree variableTree) { diff --git a/java-checks/src/test/java/org/sonar/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckTest.java b/java-checks/src/test/java/org/sonar/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckTest.java index a89e4160945..389873b989b 100644 --- a/java-checks/src/test/java/org/sonar/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckTest.java +++ b/java-checks/src/test/java/org/sonar/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckTest.java @@ -35,4 +35,27 @@ void test() { .withoutSemantic() .verifyNoIssues(); } + + @Test + void test_excluded_annotations_and_types() { + PersistentEntityUsedAsRequestParameterCheck check = new PersistentEntityUsedAsRequestParameterCheck(); + check.excludedAnnotations = "checks.spring.PersistentEntityUsedAsRequestParameterCheckExcludedSample$PartnerCampaign, "; + check.excludedTypes = "checks.spring.PersistentEntityUsedAsRequestParameterCheckExcludedSample$UserDetail"; + CheckVerifier.newVerifier() + .onFile(mainCodeSourcesPath("checks/spring/PersistentEntityUsedAsRequestParameterCheckExcludedSample.java")) + .withCheck(check) + .verifyIssues(); + } + + @Test + void test_excluded_without_semantic() { + PersistentEntityUsedAsRequestParameterCheck check = new PersistentEntityUsedAsRequestParameterCheck(); + check.excludedAnnotations = "checks.spring.PersistentEntityUsedAsRequestParameterCheckExcludedSample$PartnerCampaign"; + check.excludedTypes = "checks.spring.PersistentEntityUsedAsRequestParameterCheckExcludedSample$UserDetail"; + CheckVerifier.newVerifier() + .onFile(mainCodeSourcesPath("checks/spring/PersistentEntityUsedAsRequestParameterCheckExcludedSample.java")) + .withCheck(check) + .withoutSemantic() + .verifyNoIssues(); + } } From eca651da006e603964db688bd07121e90fb52aa5 Mon Sep 17 00:00:00 2001 From: Luqmansonar Date: Fri, 13 Nov 2026 11:07:54 +0100 Subject: [PATCH 2/3] SONARJAVA-7030 Parse S4684 exclusions once per method Co-Authored-By: Claude Sonnet 5.5 --- ...stentEntityUsedAsRequestParameterCheck.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/java-checks/src/main/java/org/sonar/java/checks/spring/PersistentEntityUsedAsRequestParameterCheck.java b/java-checks/src/main/java/org/sonar/java/checks/spring/PersistentEntityUsedAsRequestParameterCheck.java index 5708f2133d5..f091460d61f 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/spring/PersistentEntityUsedAsRequestParameterCheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/spring/PersistentEntityUsedAsRequestParameterCheck.java @@ -19,6 +19,8 @@ import java.util.Collections; import java.util.Arrays; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import java.util.stream.Stream; import org.sonar.check.Rule; import org.sonar.check.RuleProperty; @@ -79,9 +81,11 @@ public void visitNode(Tree tree) { Symbol.MethodSymbol methodSymbol = methodTree.symbol(); if (isRequestMappingAnnotated(methodSymbol)) { + List allowedAnnotations = Stream.concat(PARAMETER_ANNOTATION_EXCEPTIONS.stream(), split(excludedAnnotations)).toList(); + Set excludedTypeNames = split(excludedTypes).collect(Collectors.toSet()); methodTree.parameters().stream() - .filter(this::hasNoAllowedAnnotations) - .filter(this::isNotExcludedType) + .filter(p -> allowedAnnotations.stream().noneMatch(p.symbol().metadata()::isAnnotatedWith)) + .filter(p -> !excludedTypeNames.contains(p.type().symbolType().fullyQualifiedName())) .filter(PersistentEntityUsedAsRequestParameterCheck::isPersistentEntity) .filter(PersistentEntityUsedAsRequestParameterCheck::hasNoCustomSerialization) .forEach(p -> reportIssue(p.simpleName(), "Replace this persistent entity with a simple POJO or DTO object.")); @@ -96,16 +100,6 @@ private static boolean isPersistentEntity(VariableTree variableTree) { return ENTITY_ANNOTATIONS.stream().anyMatch(variableTree.type().symbolType().symbol().metadata()::isAnnotatedWith); } - private boolean hasNoAllowedAnnotations(VariableTree variableTree) { - return Stream.concat(PARAMETER_ANNOTATION_EXCEPTIONS.stream(), split(excludedAnnotations)) - .noneMatch(variableTree.symbol().metadata()::isAnnotatedWith); - } - - private boolean isNotExcludedType(VariableTree variableTree) { - String typeName = variableTree.type().symbolType().fullyQualifiedName(); - return split(excludedTypes).noneMatch(typeName::equals); - } - private static Stream split(String commaSeparated) { return Arrays.stream(commaSeparated.split(",")).map(String::trim).filter(s -> !s.isEmpty()); } From e0a3b89548a35d01bac319ff0194168bf2cb33ff Mon Sep 17 00:00:00 2001 From: Luqmansonar Date: Fri, 13 Nov 2026 15:56:07 +0100 Subject: [PATCH 3/3] SONARJAVA-7030 Split excluded-type sample into compliant and noncompliant methods Co-Authored-By: Claude Sonnet 5.5 --- ...tentEntityUsedAsRequestParameterCheckExcludedSample.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/java-checks-test-sources/default/src/main/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckExcludedSample.java b/java-checks-test-sources/default/src/main/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckExcludedSample.java index 4629097abe6..1f5456fc535 100644 --- a/java-checks-test-sources/default/src/main/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckExcludedSample.java +++ b/java-checks-test-sources/default/src/main/java/checks/spring/PersistentEntityUsedAsRequestParameterCheckExcludedSample.java @@ -27,7 +27,11 @@ public void annotated(@PartnerCampaign Campaign campaign) { // Compliant, annota } @GetMapping("/b") - public void typed(Other other, UserDetail userDetail) { // Noncompliant + public void excludedType(UserDetail userDetail) { // Compliant, type declared + } + + @GetMapping("/d") + public void notExcludedType(Other other) { // Noncompliant } @GetMapping("/c")