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

fix: Filters.(and/or/nor) not working with iterable arguments for Java Driver dialect INTELLIJ-157 #111

Merged
merged 3 commits into from
Jan 8, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ This data source is used for autocompletion and type checking.
### Removed

### Fixed
* [INTELLIJ-157](https://jira.mongodb.org/browse/INTELLIJ-157): Unable to parse and hence inspect `Filters.and`, `Filters.or` and `Filters.nor`, in code that uses Java Driver, when argument for these method calls is a parseable Java Iterable.

### Security
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import static com.mongodb.client.model.Filters.*;

Expand All @@ -158,7 +159,8 @@ public class Repository {
this.client = client;
}

public FindIterable<Document> exampleFind() {
// and queries
public FindIterable<Document> inlineAndQuery() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(
Expand All @@ -167,10 +169,102 @@ public class Repository {
)
);
}

public FindIterable<Document> andQueryFromAVariableWithVariableFieldName() {
var fieldName = "nonExistingField";
var andQuery = and(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">fieldName</warning>, "123")
);
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(andQuery);
}

public FindIterable<Document> andQueryFromAMethodCallWithFieldNameFromMethodCall() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(getAndQueryWithFieldNameFromMethodCall());
}

private Bson getAndQueryWithFieldNameFromMethodCall() {
return and(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">getFieldName()</warning>, "123")
);
}

private String getFieldName() {
return "nonExistingField";
}

// or queries
public FindIterable<Document> inlineOrQuery() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(
or(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">"nonExistingField"</warning>, "123")
)
);
}

public FindIterable<Document> orQueryFromAVariableWithVariableFieldName() {
var fieldName = "nonExistingField";
var orQuery = or(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">fieldName</warning>, "123")
);
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(orQuery);
}

public FindIterable<Document> orQueryFromAMethodCallWithFieldNameFromMethodCall() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(getOrQueryWithFieldNameFromMethodCall());
}

private Bson getOrQueryWithFieldNameFromMethodCall() {
return or(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">getFieldName()</warning>, "123")
);
}

// nor queries
public FindIterable<Document> inlineNorQuery() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(
nor(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">"nonExistingField"</warning>, "123")
)
);
}

public FindIterable<Document> norQueryFromAVariableWithVariableFieldName() {
var fieldName = "nonExistingField";
var norQuery = nor(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">fieldName</warning>, "123")
);
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(norQuery);
}

public FindIterable<Document> norQueryFromAMethodCallWithFieldNameFromMethodCall() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(getNorQueryWithFieldNameFromMethodCall());
}

private Bson getNorQueryWithFieldNameFromMethodCall() {
return nor(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">getFieldName()</warning>, "123")
);
}
}
""",
)
fun `shows an inspection when a field, referenced in a nested $and query, does not exists in the current namespace`(
fun `shows an inspection when a field, referenced in different forms of a nested $and, $or and $nor query, does not exists in the current namespace`(
fixture: CodeInsightTestFixture,
) {
val (dataSource, readModelProvider) = fixture.setupConnection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,19 @@ object JavaDriverDialectParser : DialectParser<PsiElement> {
HasValueReference(valueReference)
),
)
} else if (method.isVarArgs || method.name == "not") {
// Filters.and, Filters.or... are varargs
} else if (method.name == "and" || method.name == "or" || method.name == "nor") {
return Node(
filter,
listOf(
Named(Name.from(method.name)),
HasFilter(
filter.getVarArgsOrIterableArgs()
.mapNotNull { resolveBsonBuilderCall(it, FILTERS_FQN) }
.mapNotNull { parseFilterExpression(it) },
),
),
)
} else if (method.name == "not") {
return Node(
filter,
listOf(
Expand Down
Loading
Loading