-
Notifications
You must be signed in to change notification settings - Fork 39
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
Introduce RequestParamAnnotationCheck
for invalid types
#33
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks nice! 🚀 .
Pushed some suggestions, let me know what you think 😄
...ne-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RequestParamAnnotationCheck.java
Outdated
Show resolved
Hide resolved
AT_LEAST_ONE, | ||
annotations(AT_LEAST_ONE, anyOf(isType(ANN_PACKAGE_PREFIX + "RequestParam"))))); | ||
|
||
private static final Matcher<MethodTree> HAS_INVALID_REQUEST_PARAM_TYPE = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on RequestMappingAnnotationCheck
it is understandable that these Matchers
are separate. However, in this case we can also combine them, as they are almost identical. By adding the second condition of the second matcher to the first matcher, we only have to use one Matcher
without loosing clarity (IMO). Pushed a suggestion, WDYT 😄 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wrong here, leaving this as is. Still think we can combine it into one Matcher
🤔 .
Added an extra test case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wrong here, leaving this as is. Still think we can combine it into one Matcher
🤔 .
Added an extra test case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I was trying to merge the Matchers but I think I just found why you added that testcase 😅
...ne-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RequestParamAnnotationCheck.java
Outdated
Show resolved
Hide resolved
...ne-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RequestParamAnnotationCheck.java
Outdated
Show resolved
Hide resolved
...ne-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RequestParamAnnotationCheck.java
Outdated
Show resolved
Hide resolved
(There is a bug in my suggestion, so not pushing yet) |
RequestParamAnnotationCheck
for invalid types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the check @hpnog 🚀 !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conceptionally it makes sense to split these cases into two checks.
Nice one @hpnog, thanks!
RequestParamAnnotationCheck
for invalid typesRequestParamAnnotationCheck
for invalid types
Suggested commit message:
|
Rebase ☝️ |
bdf7e4d
to
6f529ea
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rebased and added two commits. Suggested commit message:
Introduce `RequestParamTypeCheck` (#33)
Please carefully re-review, as I've made quite some changes.
...ne-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/RequestParamAnnotationCheck.java
Outdated
Show resolved
Hide resolved
methodHasParameters( | ||
AT_LEAST_ONE, | ||
allOf( | ||
annotations(ALL, isType(ANN_PACKAGE_PREFIX + "RequestParam")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
annotations(ALL, isType(ANN_PACKAGE_PREFIX + "RequestParam")), | |
annotations(AT_LEAST_ONE, isType(ANN_PACKAGE_PREFIX + "RequestParam")), |
Otherwise the check ignores e.g. @Nullable @RequestParam
parameters.
annotations(ALL, isType(ANN_PACKAGE_PREFIX + "RequestParam")), | ||
anyOf( | ||
isSubtypeOf("com.google.common.collect.ImmutableCollection"), | ||
isSameType("com.google.common.collect.ImmutableMap")))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isSameType("com.google.common.collect.ImmutableMap")))); | |
isSubtypeOf("com.google.common.collect.ImmutableMap")))); |
This way e.g. ImmutableBiMap
is also covered.
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, isType(ANN_PACKAGE_PREFIX + "RequestParam")))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The @RequestParam
check is done twice (here and below). IMHO we can drop this whole HAS_REQUEST_MAPPING_ANNOTATION
constant; the other Matcher
should suffice.
.setMessage( | ||
"At least one defined Request Parameter has an invalid type. " | ||
+ "`ImmutableMap`, `ImmutableCollection` and subtypes are not allowed.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A shorter text could be
@RequestParam
does not supportImmutableCollection
andImmutableMap
subtypes
We can define this in the @BugPattern
summary and then simply use describeMatch(tree)
here.
methodHasParameters( | ||
AT_LEAST_ONE, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since in the end we're interested in method parameters and since @RequestParam
cannot be applied to anything other than a parameter, we can simplify the code by using a Matcher<VariableTree>
. This will also make the Error Prone error message point to the exact parameter impacted.
isSubtypeOf("com.google.common.collect.ImmutableCollection"), | ||
isSameType("com.google.common.collect.ImmutableMap")))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use string references for types that may not be on the annotation processor classpath; this holds for e.g. org.springframework.web.bind.annotation.RequestParam
.
On the other hand, Error Prone itself depends on Guava, so for ImmutableCollection
and ImmutableMap
we can use .class
references.
/** | ||
* A {@link BugChecker} which flags {@code RequestParam} parameters that have an invalid type. | ||
* | ||
* <p>Types considered invalid are {@link ImmutableMap} and subtypes of {@link ImmutableCollection}. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** | |
* A {@link BugChecker} which flags {@code RequestParam} parameters that have an invalid type. | |
* | |
* <p>Types considered invalid are {@link ImmutableMap} and subtypes of {@link ImmutableCollection}. | |
*/ | |
/** A {@link BugChecker} which flags {@code @RequestParam} parameters with an unsupported type. */ |
(Over time we may add additional unsupported types.)
WDYT of the changes @hpnog ? :) |
Took some time to circle back to this one. Looks good 🚀 If I could, I'd approve ✔️ |
[ANS-219]
As a follow-up to PicnicSupermarket/picnic-platform#8503:
ImmutableMap
/subtypes ofImmutableCollection
from being the type of ane@RequestParam
.