Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 excludedType(UserDetail userDetail) { // Compliant, type declared
}

@GetMapping("/d")
public void notExcludedType(Other other) { // Noncompliant
}

@GetMapping("/c")
public void notDeclared(Campaign campaign) { // Noncompliant
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
package org.sonar.java.checks.spring;

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;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.tree.MethodTree;
Expand All @@ -28,6 +33,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<Tree.Kind> nodesToVisit() {
return Collections.singletonList(Tree.Kind.METHOD);
Expand Down Expand Up @@ -62,8 +81,11 @@ public void visitNode(Tree tree) {
Symbol.MethodSymbol methodSymbol = methodTree.symbol();

if (isRequestMappingAnnotated(methodSymbol)) {
List<String> allowedAnnotations = Stream.concat(PARAMETER_ANNOTATION_EXCEPTIONS.stream(), split(excludedAnnotations)).toList();
Set<String> excludedTypeNames = split(excludedTypes).collect(Collectors.toSet());
methodTree.parameters().stream()
.filter(PersistentEntityUsedAsRequestParameterCheck::hasNoAllowedAnnotations)
.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."));
Expand All @@ -78,8 +100,8 @@ 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 static Stream<String> split(String commaSeparated) {
return Arrays.stream(commaSeparated.split(",")).map(String::trim).filter(s -> !s.isEmpty());
}

private static boolean hasNoCustomSerialization(VariableTree variableTree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Loading