Skip to content

Commit f9519f2

Browse files
Remove timestampInSnapshots Setting (#2026)
1 parent 83e9406 commit f9519f2

File tree

8 files changed

+17
-146
lines changed

8 files changed

+17
-146
lines changed

firebase-firestore/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# Unreleased
1+
# Unreleased (22.0.0)
2+
- [changed] Removed the deprecated `timestampsInSnapshotsEnabled` setting.
3+
Any timestamps in Firestore documents are now returned as `Timestamps`. To
4+
convert `Timestamp` classed to `java.util.Date`, use `Timestamp.toDate()`.
25

36
# 21.6.1
47
- [changed] Added new internal HTTP headers to the gRPC connection.

firebase-firestore/api.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ package com.google.firebase.firestore {
184184
}
185185

186186
public final class FirebaseFirestoreSettings {
187-
method public boolean areTimestampsInSnapshotsEnabled();
188187
method public long getCacheSizeBytes();
189188
method @NonNull public String getHost();
190189
method public boolean isPersistenceEnabled();
@@ -204,7 +203,6 @@ package com.google.firebase.firestore {
204203
method @NonNull public com.google.firebase.firestore.FirebaseFirestoreSettings.Builder setHost(@NonNull String);
205204
method @NonNull public com.google.firebase.firestore.FirebaseFirestoreSettings.Builder setPersistenceEnabled(boolean);
206205
method @NonNull public com.google.firebase.firestore.FirebaseFirestoreSettings.Builder setSslEnabled(boolean);
207-
method @Deprecated @NonNull public com.google.firebase.firestore.FirebaseFirestoreSettings.Builder setTimestampsInSnapshotsEnabled(boolean);
208206
}
209207

210208
public class GeoPoint implements java.lang.Comparable<com.google.firebase.firestore.GeoPoint> {

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

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,20 @@
1515
package com.google.firebase.firestore;
1616

1717
import static com.google.common.truth.Truth.assertThat;
18-
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.newTestSettingsWithSnapshotTimestampsEnabled;
1918
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.querySnapshotToValues;
2019
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testCollection;
21-
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.testFirestore;
2220
import static com.google.firebase.firestore.testutil.IntegrationTestUtil.waitFor;
2321
import static com.google.firebase.firestore.testutil.TestUtil.map;
24-
import static com.google.firebase.firestore.util.Util.autoId;
2522
import static org.junit.Assert.assertEquals;
2623
import static org.junit.Assert.assertNull;
2724

2825
import androidx.test.ext.junit.runners.AndroidJUnit4;
2926
import com.google.android.gms.tasks.Task;
3027
import com.google.android.gms.tasks.Tasks;
3128
import com.google.firebase.Timestamp;
32-
import com.google.firebase.firestore.DocumentSnapshot.ServerTimestampBehavior;
3329
import com.google.firebase.firestore.testutil.IntegrationTestUtil;
3430
import java.util.ArrayList;
3531
import java.util.Arrays;
36-
import java.util.Date;
3732
import java.util.List;
3833
import java.util.Map;
3934
import org.junit.After;
@@ -218,7 +213,7 @@ private static Map<String, Object> objectWithTimestamp(Timestamp timestamp) {
218213
}
219214

220215
@Test
221-
public void testTimestampsInSnapshots() {
216+
public void testTimestampsAreTruncated() {
222217
Timestamp originalTimestamp = new Timestamp(100, 123456789);
223218
// Timestamps are currently truncated to microseconds after being written to the database.
224219
Timestamp truncatedTimestamp =
@@ -240,42 +235,4 @@ public void testTimestampsInSnapshots() {
240235
Map<String, Object> nestedObject = (Map<String, Object>) data.get("nested");
241236
assertThat(nestedObject.get("timestamp2")).isEqualTo(readNestedTimestamp);
242237
}
243-
244-
private static DocumentReference createDocWithTimestampsDisabled() {
245-
FirebaseFirestoreSettings settings = newTestSettingsWithSnapshotTimestampsEnabled(false);
246-
return testFirestore(settings).collection(autoId()).document();
247-
}
248-
249-
@Test
250-
public void testTimestampsInSnapshotsLegacyBehaviorForTimestamps() {
251-
Timestamp timestamp = new Timestamp(100, 123456789);
252-
253-
DocumentReference docRef = createDocWithTimestampsDisabled();
254-
waitFor(docRef.set(objectWithTimestamp(timestamp)));
255-
DocumentSnapshot snapshot = waitFor(docRef.get());
256-
Map<String, Object> data = snapshot.getData();
257-
258-
assertThat(snapshot.get("timestamp")).isInstanceOf(Date.class);
259-
assertThat(data.get("timestamp")).isInstanceOf(Date.class);
260-
assertThat(snapshot.get("timestamp")).isEqualTo(timestamp.toDate());
261-
assertThat(data.get("timestamp")).isEqualTo(timestamp.toDate());
262-
263-
assertThat(snapshot.get("nested.timestamp2")).isInstanceOf(Date.class);
264-
assertThat(snapshot.get("nested.timestamp2")).isEqualTo(timestamp.toDate());
265-
@SuppressWarnings("unchecked")
266-
Map<String, Object> nestedObject = (Map<String, Object>) data.get("nested");
267-
assertThat(nestedObject).isNotNull();
268-
assertThat(nestedObject.get("timestamp2")).isEqualTo(timestamp.toDate());
269-
}
270-
271-
@Test
272-
public void testTimestampsInSnapshotsLegacyBehaviorForServerTimestamps() {
273-
DocumentReference docRef = createDocWithTimestampsDisabled();
274-
waitFor(docRef.set(map("timestamp", FieldValue.serverTimestamp())));
275-
DocumentSnapshot snapshot = waitFor(docRef.get());
276-
assertThat(snapshot.get("timestamp", ServerTimestampBehavior.ESTIMATE))
277-
.isInstanceOf(Date.class);
278-
assertThat(snapshot.getData(ServerTimestampBehavior.ESTIMATE).get("timestamp"))
279-
.isInstanceOf(Date.class);
280-
}
281238
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,6 @@ public static DatabaseInfo testEnvDatabaseInfo() {
139139
}
140140

141141
public static FirebaseFirestoreSettings newTestSettings() {
142-
return newTestSettingsWithSnapshotTimestampsEnabled(true);
143-
}
144-
145-
@SuppressWarnings("deprecation") // for setTimestampsInSnapshotsEnabled()
146-
public static FirebaseFirestoreSettings newTestSettingsWithSnapshotTimestampsEnabled(
147-
boolean enabled) {
148142
FirebaseFirestoreSettings.Builder settings = new FirebaseFirestoreSettings.Builder();
149143

150144
if (CONNECT_TO_EMULATOR) {
@@ -155,7 +149,6 @@ public static FirebaseFirestoreSettings newTestSettingsWithSnapshotTimestampsEna
155149
}
156150

157151
settings.setPersistenceEnabled(true);
158-
settings.setTimestampsInSnapshotsEnabled(enabled);
159152

160153
return settings.build();
161154
}

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

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,7 @@ public Map<String, Object> getData() {
144144
public Map<String, Object> getData(@NonNull ServerTimestampBehavior serverTimestampBehavior) {
145145
checkNotNull(
146146
serverTimestampBehavior, "Provided serverTimestampBehavior value must not be null.");
147-
UserDataWriter userDataWriter =
148-
new UserDataWriter(
149-
firestore,
150-
firestore.getFirestoreSettings().areTimestampsInSnapshotsEnabled(),
151-
serverTimestampBehavior);
147+
UserDataWriter userDataWriter = new UserDataWriter(firestore, serverTimestampBehavior);
152148
return doc == null ? null : userDataWriter.convertObject(doc.getData().getFieldsMap());
153149
}
154150

@@ -260,10 +256,7 @@ public Object get(
260256
checkNotNull(fieldPath, "Provided field path must not be null.");
261257
checkNotNull(
262258
serverTimestampBehavior, "Provided serverTimestampBehavior value must not be null.");
263-
return getInternal(
264-
fieldPath.getInternalPath(),
265-
serverTimestampBehavior,
266-
firestore.getFirestoreSettings().areTimestampsInSnapshotsEnabled());
259+
return getInternal(fieldPath.getInternalPath(), serverTimestampBehavior);
267260
}
268261

269262
/**
@@ -396,9 +389,6 @@ public Date getDate(@NonNull String field) {
396389
/**
397390
* Returns the value of the field as a Date.
398391
*
399-
* <p>This method ignores the global setting {@link
400-
* FirebaseFirestoreSettings#areTimestampsInSnapshotsEnabled}.
401-
*
402392
* @param field The path to the field.
403393
* @param serverTimestampBehavior Configures the behavior for server timestamps that have not yet
404394
* been set to their final value.
@@ -411,20 +401,13 @@ public Date getDate(
411401
checkNotNull(field, "Provided field path must not be null.");
412402
checkNotNull(
413403
serverTimestampBehavior, "Provided serverTimestampBehavior value must not be null.");
414-
Object maybeDate =
415-
getInternal(
416-
FieldPath.fromDotSeparatedPath(field).getInternalPath(),
417-
serverTimestampBehavior,
418-
/* timestampsInSnapshots= */ false);
419-
return castTypedValue(maybeDate, field, Date.class);
404+
@Nullable Timestamp timestamp = getTimestamp(field, serverTimestampBehavior);
405+
return timestamp != null ? timestamp.toDate() : null;
420406
}
421407

422408
/**
423409
* Returns the value of the field as a {@code com.google.firebase.Timestamp}.
424410
*
425-
* <p>This method ignores the global setting {@link
426-
* FirebaseFirestoreSettings#areTimestampsInSnapshotsEnabled}.
427-
*
428411
* @param field The path to the field.
429412
* @throws RuntimeException if this is not a timestamp field.
430413
* @return The value of the field
@@ -437,9 +420,6 @@ public Timestamp getTimestamp(@NonNull String field) {
437420
/**
438421
* Returns the value of the field as a {@code com.google.firebase.Timestamp}.
439422
*
440-
* <p>This method ignores the global setting {@link
441-
* FirebaseFirestoreSettings#areTimestampsInSnapshotsEnabled}.
442-
*
443423
* @param field The path to the field.
444424
* @param serverTimestampBehavior Configures the behavior for server timestamps that have not yet
445425
* been set to their final value.
@@ -454,9 +434,7 @@ public Timestamp getTimestamp(
454434
serverTimestampBehavior, "Provided serverTimestampBehavior value must not be null.");
455435
Object maybeTimestamp =
456436
getInternal(
457-
FieldPath.fromDotSeparatedPath(field).getInternalPath(),
458-
serverTimestampBehavior,
459-
/* timestampsInSnapshots= */ true);
437+
FieldPath.fromDotSeparatedPath(field).getInternalPath(), serverTimestampBehavior);
460438
return castTypedValue(maybeTimestamp, field, Timestamp.class);
461439
}
462440

@@ -526,13 +504,11 @@ private <T> T castTypedValue(Object value, String field, Class<T> clazz) {
526504
@Nullable
527505
private Object getInternal(
528506
@NonNull com.google.firebase.firestore.model.FieldPath fieldPath,
529-
@NonNull ServerTimestampBehavior serverTimestampBehavior,
530-
boolean timestampsInSnapshots) {
507+
@NonNull ServerTimestampBehavior serverTimestampBehavior) {
531508
if (doc != null) {
532509
Value val = doc.getField(fieldPath);
533510
if (val != null) {
534-
UserDataWriter userDataWriter =
535-
new UserDataWriter(firestore, timestampsInSnapshots, serverTimestampBehavior);
511+
UserDataWriter userDataWriter = new UserDataWriter(firestore, serverTimestampBehavior);
536512
return userDataWriter.convertValue(val);
537513
}
538514
}

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

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,19 @@ public final class FirebaseFirestoreSettings {
3232

3333
private static final long MINIMUM_CACHE_BYTES = 1 * 1024 * 1024; // 1 MB
3434
private static final long DEFAULT_CACHE_SIZE_BYTES = 100 * 1024 * 1024; // 100 MB
35-
private static final boolean DEFAULT_TIMESTAMPS_IN_SNAPSHOTS_ENABLED = true;
3635

3736
/** A Builder for creating {@code FirebaseFirestoreSettings}. */
3837
public static final class Builder {
3938
private String host;
4039
private boolean sslEnabled;
4140
private boolean persistenceEnabled;
42-
private boolean timestampsInSnapshotsEnabled;
4341
private long cacheSizeBytes;
4442

4543
/** Constructs a new {@code FirebaseFirestoreSettings} Builder object. */
4644
public Builder() {
4745
host = DEFAULT_HOST;
4846
sslEnabled = true;
4947
persistenceEnabled = true;
50-
timestampsInSnapshotsEnabled = DEFAULT_TIMESTAMPS_IN_SNAPSHOTS_ENABLED;
5148
cacheSizeBytes = DEFAULT_CACHE_SIZE_BYTES;
5249
}
5350

@@ -60,7 +57,6 @@ public Builder(@NonNull FirebaseFirestoreSettings settings) {
6057
host = settings.host;
6158
sslEnabled = settings.sslEnabled;
6259
persistenceEnabled = settings.persistenceEnabled;
63-
timestampsInSnapshotsEnabled = settings.timestampsInSnapshotsEnabled;
6460
}
6561

6662
/**
@@ -98,33 +94,6 @@ public Builder setPersistenceEnabled(boolean value) {
9894
return this;
9995
}
10096

101-
/**
102-
* Specifies whether to use {@link com.google.firebase.Timestamp Timestamps} for timestamp
103-
* fields in {@link DocumentSnapshot DocumentSnapshots}. This is now enabled by default and
104-
* should not be disabled.
105-
*
106-
* <p>Previously, Cloud Firestore returned timestamp fields as {@link java.util.Date} but {@link
107-
* java.util.Date} only supports millisecond precision, which leads to truncation and causes
108-
* unexpected behavior when using a timestamp from a snapshot as a part of a subsequent query.
109-
*
110-
* <p>So now Cloud Firestore returns {@link com.google.firebase.Timestamp Timestamp} values
111-
* instead of {@link java.util.Date}, avoiding this kind of problem.
112-
*
113-
* <p>To opt into the old behavior of returning {@link java.util.Date Dates}, you can
114-
* temporarily set {@link FirebaseFirestoreSettings#areTimestampsInSnapshotsEnabled} to false.
115-
*
116-
* @deprecated This setting now defaults to true and will be removed in a future release. If you
117-
* are already setting it to true, just remove the setting. If you are setting it to false,
118-
* you should update your code to expect {@link com.google.firebase.Timestamp Timestamps}
119-
* instead of {@link java.util.Date Dates} and then remove the setting.
120-
*/
121-
@NonNull
122-
@Deprecated
123-
public Builder setTimestampsInSnapshotsEnabled(boolean value) {
124-
this.timestampsInSnapshotsEnabled = value;
125-
return this;
126-
}
127-
12897
/**
12998
* Sets an approximate cache size threshold for the on-disk data. If the cache grows beyond this
13099
* size, Cloud Firestore will start removing data that hasn't been recently used. The size is
@@ -180,15 +149,13 @@ public FirebaseFirestoreSettings build() {
180149
private final String host;
181150
private final boolean sslEnabled;
182151
private final boolean persistenceEnabled;
183-
private final boolean timestampsInSnapshotsEnabled;
184152
private final long cacheSizeBytes;
185153

186154
/** Constructs a {@code FirebaseFirestoreSettings} object based on the values in the Builder. */
187155
private FirebaseFirestoreSettings(Builder builder) {
188156
host = builder.host;
189157
sslEnabled = builder.sslEnabled;
190158
persistenceEnabled = builder.persistenceEnabled;
191-
timestampsInSnapshotsEnabled = builder.timestampsInSnapshotsEnabled;
192159
cacheSizeBytes = builder.cacheSizeBytes;
193160
}
194161

@@ -205,7 +172,6 @@ public boolean equals(@Nullable Object o) {
205172
return host.equals(that.host)
206173
&& sslEnabled == that.sslEnabled
207174
&& persistenceEnabled == that.persistenceEnabled
208-
&& timestampsInSnapshotsEnabled == that.timestampsInSnapshotsEnabled
209175
&& cacheSizeBytes == that.cacheSizeBytes;
210176
}
211177

@@ -214,7 +180,6 @@ public int hashCode() {
214180
int result = host.hashCode();
215181
result = 31 * result + (sslEnabled ? 1 : 0);
216182
result = 31 * result + (persistenceEnabled ? 1 : 0);
217-
result = 31 * result + (timestampsInSnapshotsEnabled ? 1 : 0);
218183
result = 31 * result + (int) cacheSizeBytes;
219184
return result;
220185
}
@@ -229,8 +194,6 @@ public String toString() {
229194
+ sslEnabled
230195
+ ", persistenceEnabled="
231196
+ persistenceEnabled
232-
+ ", timestampsInSnapshotsEnabled="
233-
+ timestampsInSnapshotsEnabled
234197
+ ", cacheSizeBytes="
235198
+ cacheSizeBytes
236199
+ "}";
@@ -252,14 +215,6 @@ public boolean isPersistenceEnabled() {
252215
return persistenceEnabled;
253216
}
254217

255-
/**
256-
* Returns whether or not {@link DocumentSnapshot DocumentSnapshots} return timestamp fields as
257-
* {@link com.google.firebase.Timestamp Timestamps}.
258-
*/
259-
public boolean areTimestampsInSnapshotsEnabled() {
260-
return timestampsInSnapshotsEnabled;
261-
}
262-
263218
/**
264219
* Returns the threshold for the cache size above which the SDK will attempt to collect the least
265220
* recently used documents.

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,12 @@
5050
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
5151
public class UserDataWriter {
5252
private final FirebaseFirestore firestore;
53-
private final boolean timestampsInSnapshots;
5453
private final DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior;
5554

5655
UserDataWriter(
5756
FirebaseFirestore firestore,
58-
boolean timestampsInSnapshots,
5957
DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior) {
6058
this.firestore = firestore;
61-
this.timestampsInSnapshots = timestampsInSnapshots;
6259
this.serverTimestampBehavior = serverTimestampBehavior;
6360
}
6461

@@ -118,12 +115,7 @@ private Object convertServerTimestamp(Value serverTimestampValue) {
118115
}
119116

120117
private Object convertTimestamp(com.google.protobuf.Timestamp value) {
121-
Timestamp timestamp = new Timestamp(value.getSeconds(), value.getNanos());
122-
if (timestampsInSnapshots) {
123-
return timestamp;
124-
} else {
125-
return timestamp.toDate();
126-
}
118+
return new Timestamp(value.getSeconds(), value.getNanos());
127119
}
128120

129121
private List<Object> convertArray(ArrayValue arrayValue) {

firebase-firestore/src/test/java/com/google/firebase/firestore/UserDataWriterTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@
4848
public class UserDataWriterTest {
4949

5050
private final UserDataWriter writer =
51-
new UserDataWriter(
52-
TestUtil.firestore(), true, DocumentSnapshot.ServerTimestampBehavior.DEFAULT);
51+
new UserDataWriter(TestUtil.firestore(), DocumentSnapshot.ServerTimestampBehavior.DEFAULT);
5352

5453
@Test
5554
public void testConvertsNullValue() {
@@ -156,16 +155,14 @@ public void testConvertsDoubleValue() {
156155
@Test
157156
public void testConvertsDateValue() {
158157
UserDataWriter dateWriter =
159-
new UserDataWriter(
160-
TestUtil.firestore(),
161-
/* timestampsInSnapshots= */ false,
162-
DocumentSnapshot.ServerTimestampBehavior.DEFAULT);
158+
new UserDataWriter(TestUtil.firestore(), DocumentSnapshot.ServerTimestampBehavior.DEFAULT);
163159
List<Date> testCases = asList(new Date(0), new Date(1356048000000L));
164160
for (Date d : testCases) {
165161
Value value = wrap(d);
166162
assertValueType(Value.ValueTypeCase.TIMESTAMP_VALUE, value);
167163
Object convertedValue = dateWriter.convertValue(value);
168-
assertEquals(d, convertedValue);
164+
assertTrue(convertedValue instanceof Timestamp);
165+
assertEquals(d, ((Timestamp) convertedValue).toDate());
169166
}
170167
}
171168

0 commit comments

Comments
 (0)