Skip to content

Commit 5eca22c

Browse files
Hui-Wuschmidt-sebastian
Hui-Wu
authored andcommitted
Move DocumentId to public space (#571)
* Move DocumentId to public space * add nest object testing * add changelog
1 parent 0afb5d7 commit 5eca22c

File tree

6 files changed

+96
-47
lines changed

6 files changed

+96
-47
lines changed

firebase-firestore/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
if it fails to load SSL Ciphers. To avoid these crashes, you must bundle
66
Conscrypt to support non-GMSCore devices on Android KitKat or JellyBean (see
77
https://github.com/grpc/grpc-java/blob/master/SECURITY.md#tls-on-android).
8+
- [feature] Added a `@DocumentId` annotation which can be used on a
9+
`DocumentReference` or `String` property in a POJO to indicate that the SDK
10+
should automatically populate it with the document's ID.
811

912
# 20.1.0
1013
- [changed] SSL and gRPC initialization now happens on a separate thread, which

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,34 @@ public void setShortValue(@Nullable Short shortValue) {
201201
}
202202
}
203203

204+
public static final class POJOWithDocumentIdAnnotation {
205+
String str;
206+
@DocumentId public DocumentReference autoPopulatedReference;
207+
@DocumentId String docReferenceId;
208+
209+
static class NestedPOJO {
210+
@DocumentId public DocumentReference autoPopulatedReference;
211+
}
212+
213+
public NestedPOJO nested = new NestedPOJO();
214+
215+
public String getDocReferenceId() {
216+
return docReferenceId;
217+
}
218+
219+
public void setDocReferenceId(String id) {
220+
this.docReferenceId = id;
221+
}
222+
223+
public String getStr() {
224+
return str;
225+
}
226+
227+
public void setStr(String str) {
228+
this.str = str;
229+
}
230+
}
231+
204232
@After
205233
public void tearDown() {
206234
IntegrationTestUtil.tearDown();
@@ -216,6 +244,20 @@ public void testWriteAndRead() {
216244
assertEquals(data, otherData);
217245
}
218246

247+
@Test
248+
public void testDocumentIdAnnotation() {
249+
CollectionReference collection = testCollection();
250+
POJOWithDocumentIdAnnotation data = new POJOWithDocumentIdAnnotation();
251+
data.setStr("name");
252+
DocumentReference reference = waitFor(collection.add(data));
253+
DocumentSnapshot doc = waitFor(reference.get());
254+
POJOWithDocumentIdAnnotation readFromStore = doc.toObject(POJOWithDocumentIdAnnotation.class);
255+
assertEquals("name", readFromStore.getStr());
256+
assertEquals(reference, readFromStore.autoPopulatedReference);
257+
assertEquals(reference, readFromStore.nested.autoPopulatedReference);
258+
assertEquals(reference.getId(), readFromStore.getDocReferenceId());
259+
}
260+
219261
@Test
220262
public void testSetMerge() {
221263
CollectionReference collection = testCollection();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.firestore;
16+
17+
import com.google.firebase.annotations.PublicApi;
18+
import java.lang.annotation.ElementType;
19+
import java.lang.annotation.Retention;
20+
import java.lang.annotation.RetentionPolicy;
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* Annotation used to mark a POJO property to be automatically populated with the document's ID when
25+
* the POJO is created from a Cloud Firestore document (for example, via {@link
26+
* DocumentSnapshot#toObject}).
27+
*
28+
* <ul>
29+
* Any of the following will throw a runtime exception:
30+
* <li>This annotation is applied to a property of a type other than String or {@link
31+
* DocumentReference}.
32+
* <li>This annotation is applied to a property that is not writable (for example, a Java Bean
33+
* getter without a backing field).
34+
* <li>This annotation is applied to a property with a name that conflicts with a read document
35+
* field. For example, if a POJO has a field `firstName` annotated by @DocumentId, and there
36+
* is a property from the document named `firstName` as well, an exception is thrown when you
37+
* try to read the document into the POJO via {@link DocumentSnapshot#toObject} or {@link
38+
* DocumentReference#get}.
39+
* <li>
40+
* </ul>
41+
*
42+
* <p>When using a POJO to write to a document (via {@link DocumentReference#set} or @{@link
43+
* WriteBatch#set}), the property annotated by @DocumentId is ignored, which allows writing the POJO
44+
* back to any document, even if it's not the origin of the POJO.
45+
*/
46+
@PublicApi
47+
@Retention(RetentionPolicy.RUNTIME)
48+
@Target({ElementType.FIELD, ElementType.METHOD})
49+
public @interface DocumentId {}

firebase-firestore/src/main/java/com/google/firebase/firestore/util/CustomClassMapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.firebase.Timestamp;
2121
import com.google.firebase.firestore.Blob;
22+
import com.google.firebase.firestore.DocumentId;
2223
import com.google.firebase.firestore.DocumentReference;
2324
import com.google.firebase.firestore.Exclude;
2425
import com.google.firebase.firestore.FieldValue;

firebase-firestore/src/main/java/com/google/firebase/firestore/util/DocumentId.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

firebase-firestore/src/test/java/com/google/firebase/firestore/util/MapperTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.Assert.assertNull;
2222
import static org.junit.Assert.fail;
2323

24+
import com.google.firebase.firestore.DocumentId;
2425
import com.google.firebase.firestore.DocumentReference;
2526
import com.google.firebase.firestore.Exclude;
2627
import com.google.firebase.firestore.PropertyName;

0 commit comments

Comments
 (0)