Skip to content

Commit

Permalink
ANS-219 Introduce RequestParamAnnotationCheck for invalid types
Browse files Browse the repository at this point in the history
  • Loading branch information
hpnog committed Mar 23, 2022
1 parent 793a70c commit 9be5648
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package tech.picnic.errorprone.bugpatterns;

import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.ALL;
import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.annotations;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.isSameType;
import static com.google.errorprone.matchers.Matchers.isSubtypeOf;
import static com.google.errorprone.matchers.Matchers.isType;
import static com.google.errorprone.matchers.Matchers.methodHasParameters;

import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.sun.source.tree.MethodTree;

/**
* A {@link BugChecker} which flags {@code @RequestParam} parameters that have an invalid type.
*
* <p>Types considered invalid are {@code ImmutableMap} and subtypes of {@code ImmutableCollection}.
*/
@AutoService(BugChecker.class)
@BugPattern(
name = "RequestParamAnnotationCheck",
summary = "Make sure all `@RequestParam` method parameters are valid",
linkType = BugPattern.LinkType.NONE,
severity = BugPattern.SeverityLevel.ERROR,
tags = BugPattern.StandardTags.LIKELY_ERROR)
public final class RequestParamAnnotationCheck extends BugChecker implements MethodTreeMatcher {
private static final long serialVersionUID = 1L;
private static final String ANN_PACKAGE_PREFIX = "org.springframework.web.bind.annotation.";

private static final Matcher<MethodTree> HAS_REQUEST_MAPPING_ANNOTATION =
allOf(
annotations(
ALL,
anyOf(
isType(ANN_PACKAGE_PREFIX + "DeleteMapping"),
isType(ANN_PACKAGE_PREFIX + "GetMapping"),
isType(ANN_PACKAGE_PREFIX + "PatchMapping"),
isType(ANN_PACKAGE_PREFIX + "PostMapping"),
isType(ANN_PACKAGE_PREFIX + "PutMapping"),
isType(ANN_PACKAGE_PREFIX + "RequestMapping"))),
methodHasParameters(
AT_LEAST_ONE,
annotations(AT_LEAST_ONE, anyOf(isType(ANN_PACKAGE_PREFIX + "RequestParam")))));

private static final Matcher<MethodTree> HAS_INVALID_REQUEST_PARAM_TYPE =
methodHasParameters(
AT_LEAST_ONE,
allOf(
annotations(ALL, anyOf(isType(ANN_PACKAGE_PREFIX + "RequestParam"))),
anyOf(
isSubtypeOf("com.google.common.collect.ImmutableCollection"),
isSameType("com.google.common.collect.ImmutableMap"))));

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
return HAS_REQUEST_MAPPING_ANNOTATION.matches(tree, state)
&& HAS_INVALID_REQUEST_PARAM_TYPE.matches(tree, state)
? buildDescription(tree)
.setMessage("At least one defined Request Parameters has an invalid type.")
.build()
: Description.NO_MATCH;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package tech.picnic.errorprone.bugpatterns;

import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;

final class RequestParamAnnotationCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(RequestParamAnnotationCheck.class, getClass());

@Test
void identification() {
compilationTestHelper
.addSourceLines(
"A.java",
"import java.util.List;",
"import java.util.Map;",
"import java.util.Set;",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.ImmutableMap;",
"import com.google.common.collect.ImmutableSet;",
"import org.springframework.web.bind.annotation.DeleteMapping;",
"import org.springframework.web.bind.annotation.GetMapping;",
"import org.springframework.web.bind.annotation.PostMapping;",
"import org.springframework.web.bind.annotation.PutMapping;",
"import org.springframework.web.bind.annotation.RequestBody;",
"import org.springframework.web.bind.annotation.RequestParam;",
"",
"interface A {",
" @PostMapping A properRequestParam(@RequestBody String body);",
" @GetMapping A properRequestParam(@RequestParam Integer param);",
" @GetMapping A properRequestParam(@RequestParam List<String> param);",
" @PostMapping A properRequestParam(@RequestBody String body, @RequestParam Set<String> param);",
" @PutMapping A properRequestParam(@RequestBody String body, @RequestParam Map<String, String> param);",
"",
" // BUG: Diagnostic contains:",
" @PostMapping A post(@RequestParam ImmutableList<String> param);",
"",
" // BUG: Diagnostic contains:",
" @PutMapping A put(@RequestBody String body, @RequestParam ImmutableSet<String> param);",
"",
" // BUG: Diagnostic contains:",
" @DeleteMapping A delete(@RequestBody String body, @RequestParam ImmutableMap<String, String> param);",
"}")
.doTest();
}
}

0 comments on commit 9be5648

Please sign in to comment.