24
24
import com .google .firebase .firestore .DocumentReference ;
25
25
import com .google .firebase .firestore .Exclude ;
26
26
import com .google .firebase .firestore .PropertyName ;
27
+ import com .google .firebase .firestore .TestUtil ;
27
28
import com .google .firebase .firestore .ThrowOnExtraProperties ;
28
29
import java .io .Serializable ;
29
30
import java .util .ArrayList ;
@@ -906,8 +907,12 @@ public void setValue(String value) {
906
907
}
907
908
908
909
private static <T > T deserialize (String jsonString , Class <T > clazz ) {
910
+ return deserialize (jsonString , clazz , null );
911
+ }
912
+
913
+ private static <T > T deserialize (String jsonString , Class <T > clazz , DocumentReference docRef ) {
909
914
Map <String , Object > json = fromSingleQuotedString (jsonString );
910
- return CustomClassMapper .convertToCustomClass (json , clazz , null );
915
+ return CustomClassMapper .convertToCustomClass (json , clazz , docRef );
911
916
}
912
917
913
918
private static Object serialize (Object object ) {
@@ -2354,6 +2359,15 @@ private static class DocumentIdOnStringField {
2354
2359
@ DocumentId public String docId = "doc-id" ;
2355
2360
}
2356
2361
2362
+ private static class DocumentIdOnStringFieldAsProperty {
2363
+ @ PropertyName ("docIdProperty" )
2364
+ @ DocumentId
2365
+ public String docId = "doc-id" ;
2366
+
2367
+ @ PropertyName ("anotherProperty" )
2368
+ public int someOtherProperty = 0 ;
2369
+ }
2370
+
2357
2371
private static class DocumentIdOnDocRefGetter {
2358
2372
private DocumentReference docRef ;
2359
2373
@@ -2367,15 +2381,75 @@ public void setDocRef(DocumentReference ref) {
2367
2381
}
2368
2382
}
2369
2383
2370
- private static class DocumentIdOnInheritedDocRefSetter {}
2384
+ private static class DocumentIdOnInheritedDocRefSetter extends DocumentIdOnDocRefGetter {
2385
+
2386
+ private DocumentReference inheritedDocRef ;
2387
+
2388
+ @ DocumentId
2389
+ public DocumentReference getInheritedDocRef () {
2390
+ return inheritedDocRef ;
2391
+ }
2392
+
2393
+ public void setInheritedDocRef (DocumentReference ref ) {
2394
+ inheritedDocRef = ref ;
2395
+ }
2396
+ }
2371
2397
2372
- private static class DocumentIdOnNestObjects {}
2398
+ private static class DocumentIdOnNestedObjects {
2399
+ @ PropertyName ("nestedDocIdHolder" )
2400
+ public DocumentIdOnStringField nestedDocIdHolder ;
2401
+ }
2373
2402
2374
2403
@ Test
2375
- public void documentIdsDeserialize () {}
2404
+ public void documentIdsDeserialize () {
2405
+ DocumentReference ref = TestUtil .documentReference ("coll/doc123" );
2406
+
2407
+ assertEquals ("doc123" , deserialize ("{}" , DocumentIdOnStringField .class , ref ).docId );
2408
+
2409
+ DocumentIdOnStringFieldAsProperty target =
2410
+ deserialize ("{'anotherProperty': 100}" , DocumentIdOnStringFieldAsProperty .class , ref );
2411
+ assertEquals ("doc123" , target .docId );
2412
+ assertEquals (100 , target .someOtherProperty );
2413
+
2414
+ assertEquals (ref , deserialize ("{}" , DocumentIdOnDocRefGetter .class , ref ).getDocRef ());
2415
+
2416
+ DocumentIdOnInheritedDocRefSetter target1 =
2417
+ deserialize ("{}" , DocumentIdOnInheritedDocRefSetter .class , ref );
2418
+ assertEquals (ref , target1 .getInheritedDocRef ());
2419
+ assertEquals (ref , target1 .getDocRef ());
2420
+
2421
+ assertEquals (
2422
+ "doc123" ,
2423
+ deserialize ("{'nestedDocIdHolder': {}}" , DocumentIdOnNestedObjects .class , ref )
2424
+ .nestedDocIdHolder
2425
+ .docId );
2426
+ }
2376
2427
2377
2428
@ Test
2378
- public void documentIdsAreIgnoredWhenSerializing () {}
2429
+ public void documentIdsRoundTrip () {
2430
+ // Implicitly verifies @DocumentId is ignore during serialization.
2431
+
2432
+ DocumentReference ref = TestUtil .documentReference ("coll/doc123" );
2433
+
2434
+ assertEquals (
2435
+ Collections .emptyMap (), serialize (deserialize ("{}" , DocumentIdOnStringField .class , ref )));
2436
+
2437
+ assertEquals (
2438
+ Collections .singletonMap ("anotherProperty" , 100 ),
2439
+ serialize (
2440
+ deserialize ("{'anotherProperty': 100}" , DocumentIdOnStringFieldAsProperty .class , ref )));
2441
+
2442
+ assertEquals (
2443
+ Collections .emptyMap (), serialize (deserialize ("{}" , DocumentIdOnDocRefGetter .class , ref )));
2444
+
2445
+ assertEquals (
2446
+ Collections .emptyMap (),
2447
+ serialize (deserialize ("{}" , DocumentIdOnInheritedDocRefSetter .class , ref )));
2448
+
2449
+ assertEquals (
2450
+ Collections .singletonMap ("nestedDocIdHolder" , Collections .emptyMap ()),
2451
+ serialize (deserialize ("{'nestedDocIdHolder': {}}" , DocumentIdOnNestedObjects .class , ref )));
2452
+ }
2379
2453
2380
2454
private static class DocumentIdOnStringFieldWithConflict {}
2381
2455
@@ -2386,5 +2460,27 @@ private static class DocumentIdOnNestObjectStringProperyWithConflict {}
2386
2460
private static class DocumentIdOnInheritedDocRefSetterWithConflict {}
2387
2461
2388
2462
@ Test
2389
- public void documentIdsDeserializeConflictThrows () {}
2463
+ public void documentIdsDeserializeConflictThrows () {
2464
+ DocumentReference ref = TestUtil .documentReference ("coll/doc123" );
2465
+
2466
+ assertExceptionContains (
2467
+ "cannot apply @DocumentId on this property" ,
2468
+ () -> deserialize ("{'docId': 'toBeOverwritten'}" , DocumentIdOnStringField .class , ref ));
2469
+
2470
+ assertExceptionContains (
2471
+ "cannot apply @DocumentId on this property" ,
2472
+ () ->
2473
+ deserialize (
2474
+ "{'docIdProperty': 'toBeOverwritten', 'anotherProperty': 100}" ,
2475
+ DocumentIdOnStringFieldAsProperty .class ,
2476
+ ref ));
2477
+
2478
+ assertExceptionContains (
2479
+ "cannot apply @DocumentId on this property" ,
2480
+ () ->
2481
+ deserialize (
2482
+ "{'nestedDocIdHolder': {'docId': 'toBeOverwritten'}}" ,
2483
+ DocumentIdOnNestedObjects .class ,
2484
+ ref ));
2485
+ }
2390
2486
}
0 commit comments