@@ -22,6 +22,7 @@ import com.google.firebase.firestore.DocumentSnapshot
22
22
import com.google.firebase.firestore.FieldPath
23
23
import com.google.firebase.firestore.FirebaseFirestore
24
24
import com.google.firebase.firestore.FirebaseFirestoreSettings
25
+ import com.google.firebase.firestore.PropertyName
25
26
import com.google.firebase.firestore.TestUtil
26
27
import com.google.firebase.firestore.model.ObjectValue
27
28
import com.google.firebase.firestore.testutil.TestUtil.wrap
@@ -113,6 +114,13 @@ class LibraryVersionTest : BaseTestCase() {
113
114
114
115
data class Room (var a : Int = 0 , var b : Int = 0 )
115
116
117
+ // NOTE: When Kotlin properties are annotated with `PropertyName` they need to use @get and @set.
118
+ // Also, the properties need to be mutable; that is, declared with `var` and not `val`.
119
+ // See https://github.com/firebase/firebase-android-sdk/issues/4822
120
+ data class DataClassWithPropertyName (
121
+ @get:PropertyName("dbName") @set:PropertyName("dbName") var objName : String = " DefaultObjName"
122
+ )
123
+
116
124
@RunWith(RobolectricTestRunner ::class )
117
125
class DocumentSnapshotTests {
118
126
@Before
@@ -155,6 +163,15 @@ class DocumentSnapshotTests {
155
163
assertThat(room)
156
164
.isEqualTo(ds.toObject(Room ::class .java, DocumentSnapshot .ServerTimestampBehavior .ESTIMATE ))
157
165
}
166
+
167
+ @Test
168
+ fun `PropertyName annotation works on data classes` () {
169
+ val ds = TestUtil .documentSnapshot(" foo/bar" , mapOf (" dbName" to " CustomValue" ), false )
170
+
171
+ val toObjectReturnValue = ds.toObject<DataClassWithPropertyName >()
172
+
173
+ assertThat(toObjectReturnValue).isEqualTo(DataClassWithPropertyName (" CustomValue" ))
174
+ }
158
175
}
159
176
160
177
@RunWith(RobolectricTestRunner ::class )
0 commit comments