Skip to content

Commit 34c4927

Browse files
committed
Upgraded google-cloud-firestore to the latest version (0.61.0-beta) and added a FirebaseOptions#setFireStoreTimestampsInSnapshotsEnabled(boolean) method.
The FirestoreOptions#setTimestampsInSnapshotsEnabled(boolean) method is then called in the FirestoreClient constructor with the value passed to the FirebaseOptions#setFireStoreTimestampsInSnapshotsEnabled(boolean) method. This allows for firebase-admin-java users to adapt to the Timestamps being returned in DocumentSnapshots as added in googleapis/google-cloud-java#3317 com.google.api:gax was added as a dependency to the pom.xml file as there were "java.lang.NoClassDefFoundError: com/google/api/gax/rpc/ClientStream" errors when running "mvn test" without this dependency. A couple of tests were also added to the FirestoreClientTest class.
1 parent 7a58ee0 commit 34c4927

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,11 @@
398398
<artifactId>api-common</artifactId>
399399
<version>1.2.0</version>
400400
</dependency>
401+
<dependency>
402+
<groupId>com.google.api</groupId>
403+
<artifactId>gax</artifactId>
404+
<version>1.30.0</version>
405+
</dependency>
401406
<dependency>
402407
<groupId>com.google.auth</groupId>
403408
<artifactId>google-auth-library-oauth2-http</artifactId>
@@ -411,7 +416,7 @@
411416
<dependency>
412417
<groupId>com.google.cloud</groupId>
413418
<artifactId>google-cloud-firestore</artifactId>
414-
<version>0.45.0-beta</version>
419+
<version>0.61.0-beta</version>
415420
</dependency>
416421

417422
<!-- Utilities -->

src/main/java/com/google/firebase/FirebaseOptions.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public final class FirebaseOptions {
6666
private final int readTimeout;
6767
private final JsonFactory jsonFactory;
6868
private final ThreadManager threadManager;
69+
private final boolean firestoreTimestampsInSnapshotsEnabled;
6970

7071
private FirebaseOptions(@NonNull FirebaseOptions.Builder builder) {
7172
this.credentials = checkNotNull(builder.credentials,
@@ -94,6 +95,8 @@ private FirebaseOptions(@NonNull FirebaseOptions.Builder builder) {
9495
this.connectTimeout = builder.connectTimeout;
9596
checkArgument(builder.readTimeout >= 0);
9697
this.readTimeout = builder.readTimeout;
98+
this.firestoreTimestampsInSnapshotsEnabled =
99+
builder.firestoreTimestampsInSnapshotsEnabled;
97100
}
98101

99102
/**
@@ -188,6 +191,14 @@ public int getReadTimeout() {
188191
return readTimeout;
189192
}
190193

194+
/**
195+
* Returns whether or not {@link com.google.cloud.firestore.DocumentSnapshot DocumentSnapshots}
196+
* return timestamp fields as {@link com.google.cloud.Timestamp Timestamps}.
197+
*/
198+
public boolean areFirestoreTimestampsInSnapshotsEnabled() {
199+
return firestoreTimestampsInSnapshotsEnabled;
200+
}
201+
191202
@NonNull
192203
ThreadManager getThreadManager() {
193204
return threadManager;
@@ -218,6 +229,7 @@ public static final class Builder {
218229
private ThreadManager threadManager = FirebaseThreadManagers.DEFAULT_THREAD_MANAGER;
219230
private int connectTimeout;
220231
private int readTimeout;
232+
private boolean firestoreTimestampsInSnapshotsEnabled;
221233

222234
/** Constructs an empty builder. */
223235
public Builder() {}
@@ -239,6 +251,7 @@ public Builder(FirebaseOptions options) {
239251
threadManager = options.threadManager;
240252
connectTimeout = options.connectTimeout;
241253
readTimeout = options.readTimeout;
254+
firestoreTimestampsInSnapshotsEnabled = options.firestoreTimestampsInSnapshotsEnabled;
242255
}
243256

244257
/**
@@ -411,6 +424,20 @@ public Builder setReadTimeout(int readTimeout) {
411424
return this;
412425
}
413426

427+
/**
428+
* Sets whether timestamps are enabled in Firestore
429+
* {@link com.google.cloud.firestore.DocumentSnapshot DocumentSnapshots}.
430+
*
431+
* @param firestoreTimestampsInSnapshotsEnabled If true, timestamps are enabled in Firestore
432+
* DocumentSnapshots.
433+
* @return This <code>Builder</code> instance is returned so subsequent calls can be chained.
434+
*/
435+
public Builder setFirestoreTimestampsInSnapshotsEnabled(
436+
boolean firestoreTimestampsInSnapshotsEnabled) {
437+
this.firestoreTimestampsInSnapshotsEnabled = firestoreTimestampsInSnapshotsEnabled;
438+
return this;
439+
}
440+
414441
/**
415442
* Builds the {@link FirebaseOptions} instance from the previously set options.
416443
*

src/main/java/com/google/firebase/cloud/FirestoreClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ private FirestoreClient(FirebaseApp app) {
3535
+ "set the project ID explicitly via FirebaseOptions. Alternatively you can also "
3636
+ "set the project ID via the GOOGLE_CLOUD_PROJECT environment variable.");
3737
this.firestore = FirestoreOptions.newBuilder()
38+
.setTimestampsInSnapshotsEnabled(
39+
app.getOptions().areFirestoreTimestampsInSnapshotsEnabled())
3840
.setCredentials(ImplFirebaseTrampolines.getCredentials(app))
3941
.setProjectId(projectId)
4042
.build()

src/test/java/com/google/firebase/cloud/FirestoreClientTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,33 @@ public void testServiceAccountProjectId() throws IOException {
4646
assertEquals("mock-project-id", firestore.getOptions().getProjectId());
4747
}
4848

49+
@Test
50+
public void testFirestoreTimestampsInSnapshotsEnabled_defaultsToFalse() throws IOException {
51+
FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder()
52+
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
53+
.setProjectId("explicit-project-id")
54+
.build());
55+
Firestore firestore = FirestoreClient.getFirestore(app);
56+
assertEquals(false, firestore.getOptions().areTimestampsInSnapshotsEnabled());
57+
58+
firestore = FirestoreClient.getFirestore();
59+
assertEquals(false, firestore.getOptions().areTimestampsInSnapshotsEnabled());
60+
}
61+
62+
@Test
63+
public void testFirestoreTimestampsInSnapshotsEnabled_setToTrue() throws IOException {
64+
FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder()
65+
.setCredentials(GoogleCredentials.fromStream(ServiceAccount.EDITOR.asStream()))
66+
.setProjectId("explicit-project-id")
67+
.setFirestoreTimestampsInSnapshotsEnabled(true)
68+
.build());
69+
Firestore firestore = FirestoreClient.getFirestore(app);
70+
assertEquals(true, firestore.getOptions().areTimestampsInSnapshotsEnabled());
71+
72+
firestore = FirestoreClient.getFirestore();
73+
assertEquals(true, firestore.getOptions().areTimestampsInSnapshotsEnabled());
74+
}
75+
4976
@Test
5077
public void testAppDelete() throws IOException {
5178
FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder()

0 commit comments

Comments
 (0)