Skip to content

Commit 47d754f

Browse files
authored
Change timestampsInSnapshots default to true and deprecate the setting. (#190)
Change timestampsInSnapshots default to true and deprecate the setting.
1 parent aa19fc6 commit 47d754f

File tree

4 files changed

+17
-49
lines changed

4 files changed

+17
-49
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/FieldsTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,6 @@ private static Map<String, Object> objectWithTimestamp(Timestamp timestamp) {
217217
return map("timestamp", timestamp, "nested", map("timestamp2", timestamp));
218218
}
219219

220-
// Note: because timestampsInSnapshotsEnabled is set to true in default test settings, this test
221-
// is unaffected by the current default value in FirebaseFirestoreSettings.
222220
@Test
223221
public void testTimestampsInSnapshots() {
224222
Timestamp originalTimestamp = new Timestamp(100, 123456789);

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/testutil/IntegrationTestUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public static FirebaseFirestoreSettings newTestSettings() {
9191
return newTestSettingsWithSnapshotTimestampsEnabled(true);
9292
}
9393

94+
@SuppressWarnings("deprecation") // for setTimestampsInSnapshotsEnabled()
9495
public static FirebaseFirestoreSettings newTestSettingsWithSnapshotTimestampsEnabled(
9596
boolean enabled) {
9697
return new FirebaseFirestoreSettings.Builder()

firebase-firestore/src/main/java/com/google/firebase/firestore/FirebaseFirestore.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -186,36 +186,6 @@ private void ensureClientConfigured() {
186186
if (client != null) {
187187
return;
188188
}
189-
if (!settings.areTimestampsInSnapshotsEnabled()) {
190-
Logger.warn(
191-
"Firestore",
192-
"The behavior for java.util.Date objects stored in Firestore is going to change "
193-
+ "AND YOUR APP MAY BREAK.\n"
194-
+ "To hide this warning and ensure your app does not break, you need to add "
195-
+ "the following code to your app before calling any other Cloud Firestore "
196-
+ "methods:\n"
197-
+ "\n"
198-
+ "FirebaseFirestore firestore = FirebaseFirestore.getInstance();\n"
199-
+ "FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()\n"
200-
+ " .setTimestampsInSnapshotsEnabled(true)\n"
201-
+ " .build();\n"
202-
+ "firestore.setFirestoreSettings(settings);\n"
203-
+ "\n"
204-
+ "With this change, timestamps stored in Cloud Firestore will be read back as "
205-
+ "com.google.firebase.Timestamp objects instead of as system java.util.Date "
206-
+ "objects. So you will also need to update code expecting a java.util.Date to "
207-
+ "instead expect a Timestamp. For example:\n"
208-
+ "\n"
209-
+ "// Old:\n"
210-
+ "java.util.Date date = snapshot.getDate(\"created_at\");\n"
211-
+ "// New:\n"
212-
+ "Timestamp timestamp = snapshot.getTimestamp(\"created_at\");\n"
213-
+ "java.util.Date date = timestamp.toDate();\n"
214-
+ "\n"
215-
+ "Please audit all existing usages of java.util.Date when you enable the new "
216-
+ "behavior. In a future release, the behavior will be changed to the new "
217-
+ "behavior, so if you do not follow these steps, YOUR APP MAY BREAK.");
218-
}
219189
DatabaseInfo databaseInfo =
220190
new DatabaseInfo(databaseId, persistenceKey, settings.getHost(), settings.isSslEnabled());
221191

firebase-firestore/src/main/java/com/google/firebase/firestore/FirebaseFirestoreSettings.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public final class FirebaseFirestoreSettings {
3737
// we will switch the default to the above value, 100 MB.
3838
private static final long DEFAULT_CACHE_SIZE_BYTES = CACHE_SIZE_UNLIMITED;
3939
private static final String DEFAULT_HOST = "firestore.googleapis.com";
40-
private static final boolean DEFAULT_TIMESTAMPS_IN_SNAPSHOTS_ENABLED = false;
40+
private static final boolean DEFAULT_TIMESTAMPS_IN_SNAPSHOTS_ENABLED = true;
4141

4242
/** A Builder for creating {@link FirebaseFirestoreSettings}. */
4343
@PublicApi
@@ -110,28 +110,27 @@ public Builder setPersistenceEnabled(boolean value) {
110110
}
111111

112112
/**
113-
* Enables the use of {@link com.google.firebase.Timestamp Timestamps} for timestamp fields in
114-
* {@link DocumentSnapshot DocumentSnapshots}.
113+
* Specifies whether to use {@link com.google.firebase.Timestamp Timestamps} for timestamp
114+
* fields in {@link DocumentSnapshot DocumentSnapshots}. This is now enabled by default and
115+
* should not be disabled.
115116
*
116-
* <p>Currently, Firestore returns timestamp fields as {@link java.util.Date} but {@link
117-
* java.util.Date Date} only supports millisecond precision, which leads to truncation and
118-
* causes unexpected behavior when using a timestamp from a snapshot as a part of a subsequent
119-
* query.
117+
* <p>Previously, Firestore returned timestamp fields as {@link java.util.Date} but {@link
118+
* java.util.Date} only supports millisecond precision, which leads to truncation and causes
119+
* unexpected behavior when using a timestamp from a snapshot as a part of a subsequent query.
120120
*
121-
* <p>Setting {@code setTimestampsInSnapshotsEnabled(true)} will cause Firestore to return
122-
* {@link com.google.firebase.Timestamp Timestamp} values instead of {@link java.util.Date
123-
* Date}, avoiding this kind of problem. To make this work you must also change any code that
124-
* uses {@link java.util.Date Date} to use {@link com.google.firebase.Timestamp Timestamp}
125-
* instead.
121+
* <p>So now Firestore returns {@link com.google.firebase.Timestamp Timestamp} values instead of
122+
* {@link java.util.Date}, avoiding this kind of problem.
126123
*
127-
* <p>NOTE: in the future {@link FirebaseFirestoreSettings#areTimestampsInSnapshotsEnabled} will
128-
* default to true and this option will be removed so you should change your code to use
129-
* Timestamp now and opt-in to this new behavior as soon as you can.
124+
* <p>To opt into the old behavior of returning {@link java.util.Date Dates}, you can
125+
* temporarily set {@link FirebaseFirestoreSettings#areTimestampsInSnapshotsEnabled} to false.
130126
*
131-
* @return A settings object on which the return type for timestamp fields is configured as
132-
* specified by the given {@code value}.
127+
* @deprecated This setting now defaults to true and will be removed in a future release. If you
128+
* are already setting it to true, just remove the setting. If you are setting it to false,
129+
* you should update your code to expect {@link com.google.firebase.Timestamp Timestamps}
130+
* instead of {@link java.util.Date Dates} and then remove the setting.
133131
*/
134132
@NonNull
133+
@Deprecated
135134
@PublicApi
136135
public Builder setTimestampsInSnapshotsEnabled(boolean value) {
137136
this.timestampsInSnapshotsEnabled = value;

0 commit comments

Comments
 (0)