Skip to content

Commit 8c0e7da

Browse files
committed
Move DocumentId to public space
1 parent cedb8c3 commit 8c0e7da

File tree

6 files changed

+91
-47
lines changed

6 files changed

+91
-47
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ local.properties
77
google-services.json
88
/build.gradle
99
_artifacts
10+
# VS code/extension files
11+
.project
12+
.classpath
13+
org.eclipse.buildship.core.prefs
14+
bin/

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,28 @@ 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+
public String getDocReferenceId() {
210+
return docReferenceId;
211+
}
212+
213+
public void setDocReferenceId(String id) {
214+
this.docReferenceId = id;
215+
}
216+
217+
public String getStr() {
218+
return str;
219+
}
220+
221+
public void setStr(String str) {
222+
this.str = str;
223+
}
224+
}
225+
204226
@After
205227
public void tearDown() {
206228
IntegrationTestUtil.tearDown();
@@ -216,6 +238,19 @@ public void testWriteAndRead() {
216238
assertEquals(data, otherData);
217239
}
218240

241+
@Test
242+
public void testDocumentIdAnnotation() {
243+
CollectionReference collection = testCollection();
244+
POJOWithDocumentIdAnnotation data = new POJOWithDocumentIdAnnotation();
245+
data.setStr("name");
246+
DocumentReference reference = waitFor(collection.add(data));
247+
DocumentSnapshot doc = waitFor(reference.get());
248+
POJOWithDocumentIdAnnotation readFromStore = doc.toObject(POJOWithDocumentIdAnnotation.class);
249+
assertEquals("name", readFromStore.getStr());
250+
assertEquals(reference, readFromStore.autoPopulatedReference);
251+
assertEquals(reference.getPath().endsWith(readFromStore.getDocReferenceId()), true);
252+
}
253+
219254
@Test
220255
public void testSetMerge() {
221256
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 properties of a type other than String or {@link
31+
* DocumentReference}.
32+
* <li>This annotation is applied to a property that is not writeable (for example, a Java Bean
33+
* getter without a backing field).
34+
* <li>There are conflicts between this annotation and property name matches. For example, if a
35+
* POJO has a field `firstName` annotated by @DocumentId, and there is a property from the
36+
* document named `firstName` as well, an exception is thrown when you try to read the
37+
* 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 writes to any
44+
* documents that are 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)