Skip to content

Commit 1750585

Browse files
mikelehenJesseLovelace
authored andcommitted
Firestore: change timestampsInSnapshots default to true. (#4353)
BREAKING CHANGE: The `areTimestampsInSnapshotsEnabled()` setting is now enabled by default so timestamp fields read from a `DocumentSnapshot` will be returned as `Timestamp` objects instead of `Date`. Any code expecting to receive a `Date` object must be updated.
1 parent cf7585b commit 1750585

File tree

11 files changed

+25
-81
lines changed

11 files changed

+25
-81
lines changed

google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreImpl.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -85,34 +85,6 @@ class FirestoreImpl implements Firestore {
8585
+ "Please explicitly set your Project ID in FirestoreOptions.");
8686
this.databasePath =
8787
ResourcePath.create(DatabaseRootName.of(options.getProjectId(), options.getDatabaseId()));
88-
89-
if (!options.areTimestampsInSnapshotsEnabled()) {
90-
LOGGER.warning(
91-
"The behavior for java.util.Date objects stored in Firestore is going to change "
92-
+ "AND YOUR APP MAY BREAK.\n"
93-
+ "To hide this warning and ensure your app does not break, you need to add "
94-
+ "the following code to your app before calling any other Cloud Firestore "
95-
+ "methods:\n"
96-
+ "\n"
97-
+ "FirestoreOptions options = \n"
98-
+ " FirestoreOptions.newBuilder().setTimestampsInSnapshotsEnabled(true).build();\n"
99-
+ "Firestore firestore = options.getService();\n"
100-
+ "\n"
101-
+ "With this change, timestamps stored in Cloud Firestore will be read back as "
102-
+ "com.google.cloud.Timestamp objects instead of as system java.util.Date "
103-
+ "objects. So you will also need to update code expecting a java.util.Date to "
104-
+ "instead expect a Timestamp. For example:\n"
105-
+ "\n"
106-
+ "// Old:\n"
107-
+ "java.util.Date date = (java.util.Date) snapshot.get(\"created_at\");\n"
108-
+ "// New:\n"
109-
+ "Timestamp timestamp = (Timestamp) snapshot.get(\"created_at\");\n"
110-
+ "java.util.Date date = timestamp.toDate();\n"
111-
+ "\n"
112-
+ "Please audit all existing usages of java.util.Date when you enable the new "
113-
+ "behavior. In a future release, the behavior will be changed to the new "
114-
+ "behavior, so if you do not follow these steps, YOUR APP MAY BREAK.");
115-
}
11688
}
11789

11890
/** Creates a pseudo-random 20-character ID that can be used for Firestore documents. */

google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/FirestoreOptions.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public final class FirestoreOptions extends ServiceOptions<Firestore, FirestoreO
4444
.add("https://www.googleapis.com/auth/cloud-platform")
4545
.add("https://www.googleapis.com/auth/datastore")
4646
.build();
47-
private static final boolean DEFAULT_TIMESTAMPS_IN_SNAPSHOTS_ENABLED = false;
47+
private static final boolean DEFAULT_TIMESTAMPS_IN_SNAPSHOTS_ENABLED = true;
4848

4949
private static final long serialVersionUID = -5853552236134770090L;
5050

@@ -179,26 +179,26 @@ public Builder setDatabaseId(@Nonnull String databaseId) {
179179
}
180180

181181
/**
182-
* Enables the use of {@link com.google.cloud.Timestamp Timestamps} for timestamp fields in
183-
* {@link DocumentSnapshot DocumentSnapshots}.
182+
* Specifies whether to use {@link com.google.cloud.Timestamp Timestamps} for timestamp fields
183+
* in {@link DocumentSnapshot DocumentSnapshots}. This is now enabled by default and should not
184+
* be disabled.
184185
*
185-
* <p>Currently, Firestore returns timestamp fields as {@link java.util.Date} but {@link
186-
* java.util.Date Date} only supports millisecond precision, which leads to truncation and
187-
* causes unexpected behavior when using a timestamp from a snapshot as a part of a subsequent
188-
* query.
186+
* <p>Previously, Firestore returned timestamp fields as {@link java.util.Date} but {@link
187+
* java.util.Date} only supports millisecond precision, which leads to truncation and causes
188+
* unexpected behavior when using a timestamp from a snapshot as a part of a subsequent query.
189189
*
190-
* <p>Setting {@code setTimestampsInSnapshotsEnabled(true)} will cause Firestore to return
191-
* {@link com.google.cloud.Timestamp Timestamp} values instead of {@link java.util.Date Date},
192-
* avoiding this kind of problem. To make this work you must also change any code that uses
193-
* {@link java.util.Date Date} to use {@link com.google.cloud.Timestamp Timestamp} instead.
190+
* <p>So now Firestore returns {@link com.google.cloud.Timestamp Timestamp} values instead of
191+
* {@link java.util.Date}, avoiding this kind of problem.
194192
*
195-
* <p>NOTE: in the future {@link FirestoreOptions#areTimestampsInSnapshotsEnabled} will default
196-
* to true and this option will be removed so you should change your code to use Timestamp now
197-
* and opt-in to this new behavior as soon as you can.
193+
* <p>To opt into the old behavior of returning {@link java.util.Date Dates}, you can
194+
* temporarily set {@link FirestoreOptions#areTimestampsInSnapshotsEnabled} to false.
198195
*
199-
* @return A settings object on which the return type for timestamp fields is configured as
200-
* specified by the given {@code value}.
196+
* @deprecated This setting now defaults to true and will be removed in a future release. If you
197+
* are already setting it to true, just remove the setting. If you are setting it to false,
198+
* you should update your code to expect {@link com.google.cloud.Timestamp Timestamps}
199+
* instead of {@link java.util.Date Dates} and then remove the setting.
201200
*/
201+
@Deprecated
202202
@Nonnull
203203
public Builder setTimestampsInSnapshotsEnabled(boolean value) {
204204
this.timestampsInSnapshotsEnabled = value;

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ public class CollectionReferenceTest {
4343
@Spy
4444
private FirestoreImpl firestoreMock =
4545
new FirestoreImpl(
46-
FirestoreOptions.newBuilder()
47-
.setProjectId("test-project")
48-
.setTimestampsInSnapshotsEnabled(true)
49-
.build(),
46+
FirestoreOptions.newBuilder().setProjectId("test-project").build(),
5047
Mockito.mock(FirestoreRpc.class));
5148

5249
@Captor private ArgumentCaptor<CommitRequest> argCaptor;

google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/ConformanceTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,7 @@ public ConformanceTest() {
133133
firestoreMock =
134134
Mockito.spy(
135135
new FirestoreImpl(
136-
FirestoreOptions.newBuilder()
137-
.setProjectId("projectID")
138-
.setTimestampsInSnapshotsEnabled(true)
139-
.build(),
140-
firestoreRpc));
136+
FirestoreOptions.newBuilder().setProjectId("projectID").build(), firestoreRpc));
141137
watchQuery = collection("projects/projectID/databases/(default)/documents/C").orderBy("a");
142138
}
143139

google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/DocumentReferenceTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ public class DocumentReferenceTest {
9494
@Spy
9595
private FirestoreImpl firestoreMock =
9696
new FirestoreImpl(
97-
FirestoreOptions.newBuilder()
98-
.setProjectId("test-project")
99-
.setTimestampsInSnapshotsEnabled(true)
100-
.build(),
97+
FirestoreOptions.newBuilder().setProjectId("test-project").build(),
10198
Mockito.mock(FirestoreRpc.class));
10299

103100
@Captor private ArgumentCaptor<CommitRequest> commitCapture;

google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/FirestoreTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ public class FirestoreTest {
4444
@Spy
4545
private FirestoreImpl firestoreMock =
4646
new FirestoreImpl(
47-
FirestoreOptions.newBuilder()
48-
.setProjectId("test-project")
49-
.setTimestampsInSnapshotsEnabled(true)
50-
.build(),
47+
FirestoreOptions.newBuilder().setProjectId("test-project").build(),
5148
Mockito.mock(FirestoreRpc.class));
5249

5350
@Captor private ArgumentCaptor<BatchGetDocumentsRequest> getAllCapture;

google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/QueryTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ public class QueryTest {
6161
@Spy
6262
private FirestoreImpl firestoreMock =
6363
new FirestoreImpl(
64-
FirestoreOptions.newBuilder()
65-
.setProjectId("test-project")
66-
.setTimestampsInSnapshotsEnabled(true)
67-
.build(),
64+
FirestoreOptions.newBuilder().setProjectId("test-project").build(),
6865
Mockito.mock(FirestoreRpc.class));
6966

7067
@Captor private ArgumentCaptor<RunQueryRequest> runQuery;

google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/TransactionTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ public class TransactionTest {
7777
@Spy
7878
private FirestoreImpl firestoreMock =
7979
new FirestoreImpl(
80-
FirestoreOptions.newBuilder()
81-
.setProjectId("test-project")
82-
.setTimestampsInSnapshotsEnabled(true)
83-
.build(),
84-
firestoreRpc);
80+
FirestoreOptions.newBuilder().setProjectId("test-project").build(), firestoreRpc);
8581

8682
@Captor private ArgumentCaptor<Message> requestCapture;
8783
@Captor private ArgumentCaptor<ApiStreamObserver<Message>> streamObserverCapture;

google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/WatchTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ public class WatchTest {
9797
@Spy
9898
private FirestoreImpl firestoreMock =
9999
new FirestoreImpl(
100-
FirestoreOptions.newBuilder()
101-
.setProjectId("test-project")
102-
.setTimestampsInSnapshotsEnabled(true)
103-
.build(),
104-
firestoreRpc);
100+
FirestoreOptions.newBuilder().setProjectId("test-project").build(), firestoreRpc);
105101

106102
@Captor private ArgumentCaptor<ApiStreamObserver<ListenResponse>> streamObserverCapture;
107103

google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/WriteBatchTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ public class WriteBatchTest {
5252
@Spy
5353
private FirestoreImpl firestoreMock =
5454
new FirestoreImpl(
55-
FirestoreOptions.newBuilder()
56-
.setProjectId("test-project")
57-
.setTimestampsInSnapshotsEnabled(true)
58-
.build(),
55+
FirestoreOptions.newBuilder().setProjectId("test-project").build(),
5956
Mockito.mock(FirestoreRpc.class));
6057

6158
@Captor private ArgumentCaptor<CommitRequest> commitCapture;

0 commit comments

Comments
 (0)