Skip to content

Commit 7d6c2c1

Browse files
authored
fix: add support for updating an individual field with pojo in all update method (#136)
1 parent 495f7f9 commit 7d6c2c1

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/DocumentSnapshot.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ static DocumentSnapshot fromObject(
8686
Map<String, Value> fields = new HashMap<>();
8787
for (Map.Entry<String, Object> entry : values.entrySet()) {
8888
Value encodedValue =
89-
UserDataConverter.encodeValue(FieldPath.of(entry.getKey()), entry.getValue(), options);
89+
UserDataConverter.encodeValue(
90+
FieldPath.of(entry.getKey()),
91+
CustomClassMapper.convertToPlainJavaTypes(entry.getValue()),
92+
options);
9093
if (encodedValue != null) {
9194
fields.put(entry.getKey(), encodedValue);
9295
}

google-cloud-firestore/src/test/java/com/google/cloud/firestore/DocumentReferenceTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_FIELD_PROTO;
3636
import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_WRITE_COMMIT_RESPONSE;
3737
import static com.google.cloud.firestore.LocalFirestoreHelper.TIMESTAMP;
38+
import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATED_POJO;
39+
import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATED_POJO_PROTO;
3840
import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATE_PRECONDITION;
3941
import static com.google.cloud.firestore.LocalFirestoreHelper.arrayRemove;
4042
import static com.google.cloud.firestore.LocalFirestoreHelper.arrayUnion;
@@ -82,6 +84,7 @@
8284
import java.util.List;
8385
import java.util.Map;
8486
import java.util.Map.Entry;
87+
import java.util.concurrent.ExecutionException;
8588
import org.junit.Before;
8689
import org.junit.Test;
8790
import org.junit.runner.RunWith;
@@ -255,6 +258,7 @@ public void deserializeBasicTypes() throws Exception {
255258
assertEquals(Timestamp.ofTimeSecondsAndNanos(5, 6), snapshot.getReadTime());
256259

257260
assertEquals(get(), getAllCapture.getValue());
261+
assertEquals("bar", ((Map<String, Object>) snapshot.get("model")).get("foo"));
258262
}
259263

260264
@Test
@@ -839,7 +843,8 @@ public void extractFieldMaskFromMerge() throws Exception {
839843
"second.nullValue",
840844
"second.objectValue.foo",
841845
"second.timestampValue",
842-
"second.trueValue");
846+
"second.trueValue",
847+
"second.model.foo");
843848

844849
CommitRequest expectedCommit = commit(set(nestedUpdate, updateMask));
845850
assertCommitEquals(expectedCommit, commitCapture.getValue());
@@ -1028,4 +1033,20 @@ public void updateDocumentWithPreconditions() throws Exception {
10281033
assertCommitEquals(expectedCommit, request);
10291034
}
10301035
}
1036+
1037+
@Test
1038+
public void updateIndividualPojo() throws ExecutionException, InterruptedException {
1039+
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
1040+
.when(firestoreMock)
1041+
.sendRequest(
1042+
commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
1043+
documentReference.update(UPDATED_POJO);
1044+
documentReference.update(UPDATED_POJO).get();
1045+
CommitRequest expectedCommit =
1046+
commit(update(UPDATED_POJO_PROTO, Collections.singletonList("model")));
1047+
1048+
for (CommitRequest request : commitCapture.getAllValues()) {
1049+
assertCommitEquals(expectedCommit, request);
1050+
}
1051+
}
10311052
}

google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public final class LocalFirestoreHelper {
9393
public static final Map<String, Object> UPDATED_FIELD_MAP;
9494
public static final Map<String, Value> UPDATED_FIELD_PROTO;
9595
public static final Map<String, Value> UPDATED_SINGLE_FIELD_PROTO;
96+
public static final Map<String, Value> UPDATED_POJO_PROTO;
9697

9798
public static final Map<String, Float> SINGLE_FLOAT_MAP;
9899
public static final Map<String, Value> SINGLE_FLOAT_PROTO;
@@ -114,6 +115,8 @@ public final class LocalFirestoreHelper {
114115

115116
public static final ApiFuture<CommitResponse> FIELD_TRANSFORM_COMMIT_RESPONSE;
116117

118+
public static final Map<String, Object> UPDATED_POJO;
119+
117120
public static final Date DATE;
118121
public static final Timestamp TIMESTAMP;
119122
public static final GeoPoint GEO_POINT;
@@ -660,6 +663,7 @@ public static class AllSupportedTypes {
660663
public String nullValue = null;
661664
public Blob bytesValue = BLOB;
662665
public GeoPoint geoPointValue = GEO_POINT;
666+
public Map<String, Object> model = ImmutableMap.of("foo", (Object) SINGLE_FIELD_OBJECT.foo);
663667

664668
@Override
665669
public boolean equals(Object o) {
@@ -684,7 +688,8 @@ public boolean equals(Object o) {
684688
&& Objects.equals(arrayValue, that.arrayValue)
685689
&& Objects.equals(nullValue, that.nullValue)
686690
&& Objects.equals(bytesValue, that.bytesValue)
687-
&& Objects.equals(geoPointValue, that.geoPointValue);
691+
&& Objects.equals(geoPointValue, that.geoPointValue)
692+
&& Objects.equals(model, that.model);
688693
}
689694
}
690695

@@ -717,6 +722,14 @@ public boolean equals(Object o) {
717722
SINGLE_FIELD_MAP = map("foo", (Object) "bar");
718723
SINGLE_FIELD_OBJECT = new SingleField();
719724
SINGLE_FIELD_PROTO = map("foo", Value.newBuilder().setStringValue("bar").build());
725+
UPDATED_POJO_PROTO =
726+
map(
727+
"model",
728+
Value.newBuilder()
729+
.setMapValue(
730+
MapValue.newBuilder()
731+
.putFields("foo", Value.newBuilder().setStringValue("foobar").build()))
732+
.build());
720733
SINGLE_FIELD_SNAPSHOT =
721734
new DocumentSnapshot(
722735
null,
@@ -777,7 +790,7 @@ public boolean equals(Object o) {
777790
ALL_SUPPORTED_TYPES_MAP.put("nullValue", null);
778791
ALL_SUPPORTED_TYPES_MAP.put("bytesValue", BLOB);
779792
ALL_SUPPORTED_TYPES_MAP.put("geoPointValue", GEO_POINT);
780-
793+
ALL_SUPPORTED_TYPES_MAP.put("model", map("foo", SINGLE_FIELD_OBJECT.foo));
781794
ALL_SUPPORTED_TYPES_PROTO =
782795
ImmutableMap.<String, Value>builder()
783796
.put("foo", Value.newBuilder().setStringValue("bar").build())
@@ -823,9 +836,13 @@ public boolean equals(Object o) {
823836
.setGeoPointValue(
824837
LatLng.newBuilder().setLatitude(50.1430847).setLongitude(-122.9477780))
825838
.build())
839+
.put(
840+
"model",
841+
Value.newBuilder()
842+
.setMapValue(MapValue.newBuilder().putAllFields(SINGLE_FIELD_PROTO))
843+
.build())
826844
.build();
827845
ALL_SUPPORTED_TYPES_OBJECT = new AllSupportedTypes();
828-
829846
SINGLE_WRITE_COMMIT_RESPONSE = commitResponse(/* adds= */ 1, /* deletes= */ 0);
830847
SINGLE_DELETE_COMMIT_RESPONSE = commitResponse(/* adds= */ 0, /* deletes= */ 1);
831848
SINGLE_CREATE_COMMIT_REQUEST = commit(create(SINGLE_FIELD_PROTO));
@@ -837,6 +854,7 @@ public boolean equals(Object o) {
837854
CREATE_PRECONDITION = Precondition.newBuilder().setExists(false).build();
838855

839856
UPDATE_PRECONDITION = Precondition.newBuilder().setExists(true).build();
857+
UPDATED_POJO = map("model", (Object) UPDATE_SINGLE_FIELD_OBJECT);
840858
}
841859

842860
public static String autoId() {

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.firestore.it;
1818

19+
import static com.google.cloud.firestore.LocalFirestoreHelper.UPDATE_SINGLE_FIELD_OBJECT;
1920
import static com.google.cloud.firestore.LocalFirestoreHelper.map;
2021
import static java.util.Arrays.asList;
2122
import static org.junit.Assert.assertEquals;
@@ -55,6 +56,7 @@
5556
import com.google.cloud.firestore.WriteBatch;
5657
import com.google.cloud.firestore.WriteResult;
5758
import com.google.common.collect.ImmutableList;
59+
import com.google.common.collect.ImmutableMap;
5860
import java.util.ArrayList;
5961
import java.util.Arrays;
6062
import java.util.Collections;
@@ -246,6 +248,10 @@ public void updateDocument() throws Exception {
246248
documentSnapshot = randomDoc.get().get();
247249
expectedResult.foo = "updated";
248250
assertEquals(expectedResult, documentSnapshot.toObject(AllSupportedTypes.class));
251+
expectedResult.model = ImmutableMap.of("foo", (Object) UPDATE_SINGLE_FIELD_OBJECT.foo);
252+
randomDoc.update("model", UPDATE_SINGLE_FIELD_OBJECT).get();
253+
documentSnapshot = randomDoc.get().get();
254+
assertEquals(expectedResult, documentSnapshot.toObject(AllSupportedTypes.class));
249255
}
250256

251257
@Test(expected = ExecutionException.class)

0 commit comments

Comments
 (0)