Skip to content

Allow IN queries with arrays of documentIds #560

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

Merged
merged 4 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -701,11 +701,6 @@ public void queriesFilteredByDocumentIDMustUseStringsOrDocumentReferences() {
+ "a valid String or DocumentReference, but it was of type: java.lang.Integer";
expectError(() -> collection.whereGreaterThanOrEqualTo(FieldPath.documentId(), 1), reason);

reason =
"Invalid query. When querying with FieldPath.documentId() you must provide "
+ "a valid String or DocumentReference, but it was of type: java.util.Arrays$ArrayList";
expectError(() -> collection.whereIn(FieldPath.documentId(), asList(1, 2)), reason);

reason =
"Invalid query. When querying a collection group by FieldPath.documentId(), the value "
+ "provided must result in a valid document path, but 'foo' is not because it has "
Expand All @@ -726,6 +721,36 @@ public void queriesFilteredByDocumentIDMustUseStringsOrDocumentReferences() {
() -> collection.whereArrayContainsAny(FieldPath.documentId(), asList(1, 2)), reason);
}

@Test
public void queriesUsingInAndDocumentIdMustHaveProperDocumentReferencesInArray() {
CollectionReference collection = testCollection();
String reason =
"Invalid query. When querying with FieldPath.documentId() you must provide "
+ "a valid document ID, but it was an empty string.";
expectError(() -> collection.whereIn(FieldPath.documentId(), asList("")), reason);

reason =
"Invalid query. When querying a collection by FieldPath.documentId() you must provide "
+ "a plain document ID, but 'foo/bar/baz' contains a '/' character.";
expectError(() -> collection.whereIn(FieldPath.documentId(), asList("foo/bar/baz")), reason);

reason =
"Invalid query. When querying with FieldPath.documentId() you must provide "
+ "a valid String or DocumentReference, but it was of type: java.lang.Integer";
expectError(() -> collection.whereIn(FieldPath.documentId(), asList(1, 2)), reason);

reason =
"Invalid query. When querying a collection group by FieldPath.documentId(), the value "
+ "provided must result in a valid document path, but 'foo' is not because it has "
+ "an odd number of segments (1).";
expectError(
() ->
testFirestore()
.collectionGroup("collection")
.whereIn(FieldPath.documentId(), asList("foo")),
reason);
}

// Helpers

/** Performs a write using each write API and makes sure it fails with the expected reason. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,77 @@ public FirebaseFirestore getFirestore() {
return firestore;
}

/** Validates the given document ID and returns the corresponding FieldValue. */
private FieldValue validateAndCalculateReferenceValue(FieldPath fieldPath, Object value) {
com.google.firebase.firestore.model.FieldPath internalPath = fieldPath.getInternalPath();
hardAssert(
internalPath.isKeyField(),
"fieldPath must be a key field to calculate the reference value.");
FieldValue fieldValue;
if (value instanceof String) {
String documentKey = (String) value;
if (documentKey.isEmpty()) {
throw new IllegalArgumentException(
"Invalid query. When querying with FieldPath.documentId() you must provide a valid "
+ "document ID, but it was an empty string.");
}
if (!query.isCollectionGroupQuery() && documentKey.contains("/")) {
throw new IllegalArgumentException(
"Invalid query. When querying a collection by FieldPath.documentId() you must "
+ "provide a plain document ID, but '"
+ documentKey
+ "' contains a '/' character.");
}
ResourcePath path = query.getPath().append(ResourcePath.fromString(documentKey));
if (!DocumentKey.isDocumentKey(path)) {
throw new IllegalArgumentException(
"Invalid query. When querying a collection group by FieldPath.documentId(), the "
+ "value provided must result in a valid document path, but '"
+ path
+ "' is not because it has an odd number of segments ("
+ path.length()
+ ").");
}
fieldValue =
ReferenceValue.valueOf(this.getFirestore().getDatabaseId(), DocumentKey.fromPath(path));
} else if (value instanceof DocumentReference) {
DocumentReference ref = (DocumentReference) value;
fieldValue = ReferenceValue.valueOf(this.getFirestore().getDatabaseId(), ref.getKey());
} else {
throw new IllegalArgumentException(
"Invalid query. When querying with FieldPath.documentId() you must provide a valid "
+ "String or DocumentReference, but it was of type: "
+ Util.typeName(value));
}
return fieldValue;
}

/** Validates that the value passed into a disjunctive filter satisfies all array requirements. */
private void validateDisjunctiveOperatorValueArray(Object value, Operator op) {
if (!(value instanceof List) || ((List) value).size() == 0) {
throw new IllegalArgumentException(
"Invalid Query. A non-empty array is required for '" + op.toString() + "' filters.");
}
if (((List) value).size() > 10) {
throw new IllegalArgumentException(
"Invalid Query. '"
+ op.toString()
+ "' filters support a maximum of 10 elements in the value array.");
}
if (((List) value).contains(null)) {
throw new IllegalArgumentException(
"Invalid Query. '"
+ op.toString()
+ "' filters cannot contain 'null' in the value array.");
}
if (((List) value).contains(Double.NaN) || ((List) value).contains(Float.NaN)) {
throw new IllegalArgumentException(
"Invalid Query. '"
+ op.toString()
+ "' filters cannot contain 'NaN' in the value array.");
}
}

private void validateOrderByFieldMatchesInequality(
com.google.firebase.firestore.model.FieldPath orderBy,
com.google.firebase.firestore.model.FieldPath inequality) {
Expand Down Expand Up @@ -422,65 +493,19 @@ private Query whereHelper(@NonNull FieldPath fieldPath, Operator op, Object valu
+ op.toString()
+ "' queries on FieldPath.documentId().");
}
if (value instanceof String) {
String documentKey = (String) value;
if (documentKey.isEmpty()) {
throw new IllegalArgumentException(
"Invalid query. When querying with FieldPath.documentId() you must provide a valid "
+ "document ID, but it was an empty string.");
}
if (!query.isCollectionGroupQuery() && documentKey.contains("/")) {
throw new IllegalArgumentException(
"Invalid query. When querying a collection by FieldPath.documentId() you must "
+ "provide a plain document ID, but '"
+ documentKey
+ "' contains a '/' character.");
}
ResourcePath path = query.getPath().append(ResourcePath.fromString(documentKey));
if (!DocumentKey.isDocumentKey(path)) {
throw new IllegalArgumentException(
"Invalid query. When querying a collection group by FieldPath.documentId(), the "
+ "value provided must result in a valid document path, but '"
+ path
+ "' is not because it has an odd number of segments ("
+ path.length()
+ ").");
if (op == Operator.IN) {
validateDisjunctiveOperatorValueArray(value, op);
List referenceList = new ArrayList();
Copy link
Contributor

Choose a reason for hiding this comment

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

Prefer generic versions of List<> for type-safety, so this would be List<ReferenceValue>.

Copy link
Author

Choose a reason for hiding this comment

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

ArrayValue.fromList() requires List<FieldValue>, so I'm using that instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, if we wanted to make this work, we could change ArrayValue to wrap a List<? extends FieldValue> instead of List<FieldValue> (read https://dzone.com/articles/covariance-and-contravariance for context). Probably fine to leave as-is though.

for (Object arrayValue : (List) value) {
referenceList.add(validateAndCalculateReferenceValue(fieldPath, arrayValue));
}
fieldValue =
ReferenceValue.valueOf(this.getFirestore().getDatabaseId(), DocumentKey.fromPath(path));
} else if (value instanceof DocumentReference) {
DocumentReference ref = (DocumentReference) value;
fieldValue = ReferenceValue.valueOf(this.getFirestore().getDatabaseId(), ref.getKey());
fieldValue = firestore.getDataConverter().parseQueryValue(referenceList);
} else {
throw new IllegalArgumentException(
"Invalid query. When querying with FieldPath.documentId() you must provide a valid "
+ "String or DocumentReference, but it was of type: "
+ Util.typeName(value));
fieldValue = validateAndCalculateReferenceValue(fieldPath, value);
}
} else {
if (op == Operator.IN || op == Operator.ARRAY_CONTAINS_ANY) {
if (!(value instanceof List) || ((List) value).size() == 0) {
throw new IllegalArgumentException(
"Invalid Query. A non-empty array is required for '" + op.toString() + "' filters.");
}
if (((List) value).size() > 10) {
throw new IllegalArgumentException(
"Invalid Query. '"
+ op.toString()
+ "' filters support a maximum of 10 elements in the value array.");
}
if (((List) value).contains(null)) {
throw new IllegalArgumentException(
"Invalid Query. '"
+ op.toString()
+ "' filters cannot contain 'null' in the value array.");
}
if (((List) value).contains(Double.NaN) || ((List) value).contains(Float.NaN)) {
throw new IllegalArgumentException(
"Invalid Query. '"
+ op.toString()
+ "' filters cannot contain 'NaN' in the value array.");
}
validateDisjunctiveOperatorValueArray(value, op);
}
fieldValue = firestore.getDataConverter().parseQueryValue(value);
}
Expand Down