Skip to content

Commit 8dad469

Browse files
Rejecting empty Document Ids
1 parent e30b794 commit 8dad469

3 files changed

Lines changed: 12 additions & 0 deletions

File tree

google-cloud-firestore/src/main/java/com/google/cloud/firestore/BasePath.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.firestore;
1818

19+
import com.google.common.base.Preconditions;
1920
import com.google.common.collect.ImmutableList;
2021
import javax.annotation.Nullable;
2122

@@ -52,6 +53,7 @@ B getParent() {
5253
* @param path A relative path
5354
*/
5455
B append(String path) {
56+
Preconditions.checkArgument(path != null && !path.isEmpty(), "'path' must be a non-empty String" );
5557
ImmutableList.Builder<String> components = ImmutableList.builder();
5658
components.addAll(this.getSegments());
5759
components.add(splitChildPath(path));

google-cloud-firestore/src/test/java/com/google/cloud/firestore/CollectionReferenceTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertNull;
2121
import static org.junit.Assert.assertTrue;
22+
import static org.junit.Assert.fail;
2223
import static org.mockito.Mockito.doReturn;
2324

2425
import com.google.api.core.ApiFuture;
2526
import com.google.api.gax.rpc.UnaryCallable;
27+
import com.google.cloud.firestore.Transaction.Function;
2628
import com.google.cloud.firestore.spi.v1beta1.FirestoreRpc;
2729
import com.google.firestore.v1beta1.CommitRequest;
2830
import com.google.firestore.v1beta1.CommitResponse;
@@ -68,6 +70,13 @@ public void getDocument() {
6870
assertEquals("doc", documentReference.getId());
6971
documentReference = collectionReference.document();
7072
assertEquals(20, documentReference.getId().length());
73+
74+
try {
75+
collectionReference.document("");
76+
fail();
77+
} catch (IllegalArgumentException e) {
78+
assertTrue(e.getMessage().endsWith("'path' must be a non-empty String"));
79+
}
7180
}
7281

7382
@Test

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ public String updateCallback(Transaction transaction) {
423423
}
424424
})
425425
.get();
426+
fail();
426427
} catch (Exception e) {
427428
assertTrue(e.getMessage().endsWith("User exception"));
428429
}

0 commit comments

Comments
 (0)