Skip to content
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

Merged
merged 4 commits into from
Apr 12, 2022

Conversation

hpnog
Copy link
Contributor

@hpnog hpnog commented Jan 19, 2022

[ANS-219]

As a follow-up to PicnicSupermarket/picnic-platform#8503:

  • Introduce a new check: RequestParamAnnotationCheck;
    • This new check should block any instance of ImmutableMap/subtypes of ImmutableCollection from being the type of ane @RequestParam.

Copy link
Member

@rickie rickie left a 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 😄

AT_LEAST_ONE,
annotations(AT_LEAST_ONE, anyOf(isType(ANN_PACKAGE_PREFIX + "RequestParam")))));

private static final Matcher<MethodTree> HAS_INVALID_REQUEST_PARAM_TYPE =
Copy link
Member

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 😄 ?

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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 😅

@rickie
Copy link
Member

rickie commented Jan 24, 2022

(There is a bug in my suggestion, so not pushing yet)

@rickie rickie changed the title ANS-219 Introduce RequestParamAnnotationCheck for invalid types ANS-219 Introduce RequestParamAnnotationCheck for invalid types Jan 26, 2022
@hpnog hpnog requested a review from rickie January 26, 2022 17:11
Copy link
Member

@rickie rickie left a 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 🚀 !

Copy link
Member

@werli werli left a 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!

@rickie rickie changed the title ANS-219 Introduce RequestParamAnnotationCheck for invalid types Introduce RequestParamAnnotationCheck for invalid types Feb 9, 2022
@rickie
Copy link
Member

rickie commented Mar 3, 2022

Suggested commit message:

Introduce `RequestParamAnnotationCheck`

@hpnog
Copy link
Contributor Author

hpnog commented Mar 23, 2022

Rebase ☝️

Copy link
Member

@Stephan202 Stephan202 left a 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.

methodHasParameters(
AT_LEAST_ONE,
allOf(
annotations(ALL, isType(ANN_PACKAGE_PREFIX + "RequestParam")),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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"))));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
isSameType("com.google.common.collect.ImmutableMap"))));
isSubtypeOf("com.google.common.collect.ImmutableMap"))));

This way e.g. ImmutableBiMap is also covered.

Comment on lines 43 to 56
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"))));
Copy link
Member

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.

Comment on lines 72 to 74
.setMessage(
"At least one defined Request Parameter has an invalid type. "
+ "`ImmutableMap`, `ImmutableCollection` and subtypes are not allowed.")
Copy link
Member

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 support ImmutableCollection and ImmutableMap subtypes

We can define this in the @BugPattern summary and then simply use describeMatch(tree) here.

Comment on lines 59 to 60
methodHasParameters(
AT_LEAST_ONE,
Copy link
Member

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.

Comment on lines 64 to 65
isSubtypeOf("com.google.common.collect.ImmutableCollection"),
isSameType("com.google.common.collect.ImmutableMap"))));
Copy link
Member

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.

Comment on lines 27 to 31
/**
* 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}.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* 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.)

@rickie
Copy link
Member

rickie commented Apr 12, 2022

WDYT of the changes @hpnog ? :)

@hpnog
Copy link
Contributor Author

hpnog commented Apr 12, 2022

WDYT of the changes @hpnog ? :)

Took some time to circle back to this one. Looks good 🚀
And cleaner 🧹

If I could, I'd approve ✔️

@Stephan202 Stephan202 merged commit cc7074a into master Apr 12, 2022
@Stephan202 Stephan202 deleted the hpnog/ANS-219 branch April 12, 2022 08:45
@Stephan202 Stephan202 added this to the 0.1.0 milestone Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants