From 2e9291ef0b776d4ad3b2ac9cd4cce134a715b44b Mon Sep 17 00:00:00 2001 From: Hui Wu Date: Wed, 26 Jun 2019 11:10:09 -0400 Subject: [PATCH 1/4] Move DocumentId to public space --- .gitignore | 5 ++ .../google/firebase/firestore/POJOTest.java | 35 +++++++++++++ .../google/firebase/firestore/DocumentId.java | 49 +++++++++++++++++++ .../firestore/util/CustomClassMapper.java | 1 + .../firebase/firestore/util/DocumentId.java | 47 ------------------ .../firebase/firestore/util/MapperTest.java | 1 + 6 files changed, 91 insertions(+), 47 deletions(-) create mode 100644 firebase-firestore/src/main/java/com/google/firebase/firestore/DocumentId.java delete mode 100644 firebase-firestore/src/main/java/com/google/firebase/firestore/util/DocumentId.java diff --git a/.gitignore b/.gitignore index cf33dbecd3d..389c873773d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,8 @@ local.properties google-services.json /build.gradle _artifacts +# VS code/extension files +.project +.classpath +org.eclipse.buildship.core.prefs +bin/ \ No newline at end of file diff --git a/firebase-firestore/src/androidTest/java/com/google/firebase/firestore/POJOTest.java b/firebase-firestore/src/androidTest/java/com/google/firebase/firestore/POJOTest.java index 0253d829c9e..d10f40cb946 100644 --- a/firebase-firestore/src/androidTest/java/com/google/firebase/firestore/POJOTest.java +++ b/firebase-firestore/src/androidTest/java/com/google/firebase/firestore/POJOTest.java @@ -201,6 +201,28 @@ public void setShortValue(@Nullable Short shortValue) { } } + public static final class POJOWithDocumentIdAnnotation { + String str; + @DocumentId public DocumentReference autoPopulatedReference; + @DocumentId String docReferenceId; + + public String getDocReferenceId() { + return docReferenceId; + } + + public void setDocReferenceId(String id) { + this.docReferenceId = id; + } + + public String getStr() { + return str; + } + + public void setStr(String str) { + this.str = str; + } + } + @After public void tearDown() { IntegrationTestUtil.tearDown(); @@ -216,6 +238,19 @@ public void testWriteAndRead() { assertEquals(data, otherData); } + @Test + public void testDocumentIdAnnotation() { + CollectionReference collection = testCollection(); + POJOWithDocumentIdAnnotation data = new POJOWithDocumentIdAnnotation(); + data.setStr("name"); + DocumentReference reference = waitFor(collection.add(data)); + DocumentSnapshot doc = waitFor(reference.get()); + POJOWithDocumentIdAnnotation readFromStore = doc.toObject(POJOWithDocumentIdAnnotation.class); + assertEquals("name", readFromStore.getStr()); + assertEquals(reference, readFromStore.autoPopulatedReference); + assertEquals(reference.getPath().endsWith(readFromStore.getDocReferenceId()), true); + } + @Test public void testSetMerge() { CollectionReference collection = testCollection(); diff --git a/firebase-firestore/src/main/java/com/google/firebase/firestore/DocumentId.java b/firebase-firestore/src/main/java/com/google/firebase/firestore/DocumentId.java new file mode 100644 index 00000000000..6a3c61396b6 --- /dev/null +++ b/firebase-firestore/src/main/java/com/google/firebase/firestore/DocumentId.java @@ -0,0 +1,49 @@ +// Copyright 2019 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.firebase.firestore; + +import com.google.firebase.annotations.PublicApi; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation used to mark a POJO property to be automatically populated with the document's ID when + * the POJO is created from a Cloud Firestore document (for example, via {@link + * DocumentSnapshot#toObject}). + * + * + * + *

When using a POJO to write to a document (via {@link DocumentReference#set} or @{@link + * WriteBatch#set}), the property annotated by @DocumentId is ignored, which allows writes to any + * documents that are not the origin of the POJO. + */ +@PublicApi +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.FIELD, ElementType.METHOD}) +public @interface DocumentId {} diff --git a/firebase-firestore/src/main/java/com/google/firebase/firestore/util/CustomClassMapper.java b/firebase-firestore/src/main/java/com/google/firebase/firestore/util/CustomClassMapper.java index f3230a6b1e3..6c4388419d6 100644 --- a/firebase-firestore/src/main/java/com/google/firebase/firestore/util/CustomClassMapper.java +++ b/firebase-firestore/src/main/java/com/google/firebase/firestore/util/CustomClassMapper.java @@ -19,6 +19,7 @@ import com.google.firebase.Timestamp; import com.google.firebase.firestore.Blob; +import com.google.firebase.firestore.DocumentId; import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.Exclude; import com.google.firebase.firestore.FieldValue; diff --git a/firebase-firestore/src/main/java/com/google/firebase/firestore/util/DocumentId.java b/firebase-firestore/src/main/java/com/google/firebase/firestore/util/DocumentId.java deleted file mode 100644 index 7eab6674889..00000000000 --- a/firebase-firestore/src/main/java/com/google/firebase/firestore/util/DocumentId.java +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2019 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.google.firebase.firestore.util; - -import com.google.firebase.annotations.PublicApi; -import com.google.firebase.firestore.DocumentReference; -import com.google.firebase.firestore.DocumentSnapshot; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Annotation used to mark a POJO property to be automatically populated with the document's ID when - * the POJO is created from a Firestore document (e.g. via {@link DocumentSnapshot#toObject}). - * - *

This annotation can only be applied to properties of String or {@link DocumentReference}, - * otherwise a runtime exception will be thrown. - * - *

A run time exception will be thrown if this annotation is applied to a property that is not - * writeable (eg, a Java Bean getter without a backing field.). - * - *

If there are conflicts between this annotation and property name matches, a runtime exception - * will be thrown. For example: If a POJO has a field `firstName` annotated by @DocumentId, and - * there is a property from the document named `firstName` as well, an exception will be thrown when - * you try to read document into the POJO via {@link DocumentSnapshot#toObject} or {@link - * DocumentReference#get}. - * - *

When writing a POJO to Firestore, the @DocumentId-annotated property will be ignored, to allow - * writing to any documents that are not the origin of the POJO. - */ -@PublicApi -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.FIELD, ElementType.METHOD}) -@interface DocumentId {} diff --git a/firebase-firestore/src/test/java/com/google/firebase/firestore/util/MapperTest.java b/firebase-firestore/src/test/java/com/google/firebase/firestore/util/MapperTest.java index 3cd91f34684..a1175456389 100644 --- a/firebase-firestore/src/test/java/com/google/firebase/firestore/util/MapperTest.java +++ b/firebase-firestore/src/test/java/com/google/firebase/firestore/util/MapperTest.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; +import com.google.firebase.firestore.DocumentId; import com.google.firebase.firestore.DocumentReference; import com.google.firebase.firestore.Exclude; import com.google.firebase.firestore.PropertyName; From ad38eb4d9809a05baaf7414c31b358fd2b7591f0 Mon Sep 17 00:00:00 2001 From: Hui Wu Date: Wed, 26 Jun 2019 13:43:29 -0400 Subject: [PATCH 2/4] more ignored stuff --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 389c873773d..f1f4271d98e 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ _artifacts .project .classpath org.eclipse.buildship.core.prefs -bin/ \ No newline at end of file +bin/ +.vscode/ \ No newline at end of file From b873305ab01b0f6f688f94efe3f3e6b520f1779c Mon Sep 17 00:00:00 2001 From: Hui Wu Date: Wed, 26 Jun 2019 15:25:41 -0400 Subject: [PATCH 3/4] add nest object testing --- .gitignore | 6 ------ .../com/google/firebase/firestore/POJOTest.java | 9 ++++++++- .../google/firebase/firestore/DocumentId.java | 16 ++++++++-------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index f1f4271d98e..cf33dbecd3d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,9 +7,3 @@ local.properties google-services.json /build.gradle _artifacts -# VS code/extension files -.project -.classpath -org.eclipse.buildship.core.prefs -bin/ -.vscode/ \ No newline at end of file diff --git a/firebase-firestore/src/androidTest/java/com/google/firebase/firestore/POJOTest.java b/firebase-firestore/src/androidTest/java/com/google/firebase/firestore/POJOTest.java index d10f40cb946..8cceddb7188 100644 --- a/firebase-firestore/src/androidTest/java/com/google/firebase/firestore/POJOTest.java +++ b/firebase-firestore/src/androidTest/java/com/google/firebase/firestore/POJOTest.java @@ -206,6 +206,12 @@ public static final class POJOWithDocumentIdAnnotation { @DocumentId public DocumentReference autoPopulatedReference; @DocumentId String docReferenceId; + static class NestedPOJO { + @DocumentId public DocumentReference autoPopulatedReference; + } + + public NestedPOJO nested = new NestedPOJO(); + public String getDocReferenceId() { return docReferenceId; } @@ -248,7 +254,8 @@ public void testDocumentIdAnnotation() { POJOWithDocumentIdAnnotation readFromStore = doc.toObject(POJOWithDocumentIdAnnotation.class); assertEquals("name", readFromStore.getStr()); assertEquals(reference, readFromStore.autoPopulatedReference); - assertEquals(reference.getPath().endsWith(readFromStore.getDocReferenceId()), true); + assertEquals(reference, readFromStore.nested.autoPopulatedReference); + assertEquals(reference.getId(), readFromStore.getDocReferenceId()); } @Test diff --git a/firebase-firestore/src/main/java/com/google/firebase/firestore/DocumentId.java b/firebase-firestore/src/main/java/com/google/firebase/firestore/DocumentId.java index 6a3c61396b6..eb096e426ff 100644 --- a/firebase-firestore/src/main/java/com/google/firebase/firestore/DocumentId.java +++ b/firebase-firestore/src/main/java/com/google/firebase/firestore/DocumentId.java @@ -27,21 +27,21 @@ * *

* *

When using a POJO to write to a document (via {@link DocumentReference#set} or @{@link - * WriteBatch#set}), the property annotated by @DocumentId is ignored, which allows writes to any - * documents that are not the origin of the POJO. + * WriteBatch#set}), the property annotated by @DocumentId is ignored, which allows writing the POJO + * back to any document, even if it's not the origin of the POJO. */ @PublicApi @Retention(RetentionPolicy.RUNTIME) From 2bd2d195d3c25cb9ac51dadf283f430a35ddcd8a Mon Sep 17 00:00:00 2001 From: Hui Wu Date: Wed, 26 Jun 2019 16:10:18 -0400 Subject: [PATCH 4/4] add changelog --- firebase-firestore/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/firebase-firestore/CHANGELOG.md b/firebase-firestore/CHANGELOG.md index cecc22c7581..2e11f63e0c5 100644 --- a/firebase-firestore/CHANGELOG.md +++ b/firebase-firestore/CHANGELOG.md @@ -3,6 +3,9 @@ if it fails to load SSL Ciphers. To avoid these crashes, you must bundle Conscrypt to support non-GMSCore devices on Android KitKat or JellyBean (see https://github.com/grpc/grpc-java/blob/master/SECURITY.md#tls-on-android). +- [feature] Added a `@DocumentId` annotation which can be used on a + `DocumentReference` or `String` property in a POJO to indicate that the SDK + should automatically populate it with the document's ID. # 20.1.0 - [changed] SSL and gRPC initialization now happens on a separate thread, which