-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a BugCheck for the useage of Mongo full text searches via t…
…he Java driver
- Loading branch information
1 parent
c141ebe
commit 3b742fb
Showing
6 changed files
with
117 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...ntrib/src/main/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import static com.google.errorprone.BugPattern.LinkType.CUSTOM; | ||
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; | ||
import static com.google.errorprone.BugPattern.StandardTags.PERFORMANCE; | ||
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; | ||
import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL; | ||
|
||
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.MethodInvocationTreeMatcher; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import tech.picnic.errorprone.bugpatterns.util.ThirdPartyLibrary; | ||
|
||
/** | ||
* A {@link BugChecker} that flags usages of Mongo $text filters used for full text searches. | ||
* | ||
* @see <a href="https://www.mongodb.com/docs/manual/text-search/">Mongo Text Search</a> | ||
*/ | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
summary = "Usage of the Mongo FTS ($text) queries is discouraged", | ||
link = BUG_PATTERNS_BASE_URL + "MongoFullTextSearchQueryUsage", | ||
linkType = CUSTOM, | ||
severity = WARNING, | ||
tags = PERFORMANCE) | ||
public final class MongoFullTextSearchQueryUsage extends BugChecker | ||
implements MethodInvocationTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
private static final Matcher<ExpressionTree> TEXT_FILTER_INVOCATION = | ||
staticMethod().onClass("com.mongodb.client.model.Filters").named("text"); | ||
|
||
/** Instantiates a new {@link MongoFullTextSearchQueryUsage} instance. */ | ||
public MongoFullTextSearchQueryUsage() {} | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
if (!ThirdPartyLibrary.MONGO.isIntroductionAllowed(state) | ||
|| !TEXT_FILTER_INVOCATION.matches(tree, state)) { | ||
return Description.NO_MATCH; | ||
} | ||
return describeMatch(tree); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...b/src/test/java/tech/picnic/errorprone/bugpatterns/MongoFullTextSearchQueryUsageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class MongoFullTextSearchQueryUsageTest { | ||
@Test | ||
void identification() { | ||
CompilationTestHelper.newInstance(MongoFullTextSearchQueryUsage.class, getClass()) | ||
.addSourceLines( | ||
"A.java", | ||
"import com.mongodb.client.model.Filters;", | ||
"import com.mongodb.client.model.TextSearchOptions;", | ||
"import org.bson.conversions.Bson;", | ||
"", | ||
"class A {", | ||
"", | ||
" void m() {", | ||
" Bson allowed = Filters.eq(\"a\", \"b\");", | ||
" // BUG: Diagnostic contains:", | ||
" Bson textSearch = Filters.text(\"Some text\");", | ||
" // BUG: Diagnostic contains:", | ||
" Bson textSearch2 = Filters.text(\"Some text\", new TextSearchOptions().caseSensitive(true));", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters