diff --git a/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/CustomClassMapper.java b/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/CustomClassMapper.java index fc9f009e8581..0db2808ad93e 100644 --- a/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/CustomClassMapper.java +++ b/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/CustomClassMapper.java @@ -22,6 +22,7 @@ import com.google.cloud.firestore.annotation.PropertyName; import com.google.cloud.firestore.annotation.ServerTimestamp; import com.google.cloud.firestore.annotation.ThrowOnExtraProperties; +import com.google.firestore.v1.Value; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -166,7 +167,8 @@ private static Object serialize(T o, ErrorPath path) { || o instanceof GeoPoint || o instanceof Blob || o instanceof DocumentReference - || o instanceof FieldValue) { + || o instanceof FieldValue + || o instanceof Value) { return o; } else { Class clazz = (Class) o.getClass(); diff --git a/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/UserDataConverter.java b/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/UserDataConverter.java index 24c36e36d4c7..20bd2bc71aee 100644 --- a/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/UserDataConverter.java +++ b/google-cloud-clients/google-cloud-firestore/src/main/java/com/google/cloud/firestore/UserDataConverter.java @@ -148,6 +148,8 @@ static Value encodeValue( } else if (sanitizedObject instanceof Blob) { Blob blob = (Blob) sanitizedObject; return Value.newBuilder().setBytesValue(blob.toByteString()).build(); + } else if (sanitizedObject instanceof Value) { + return (Value) sanitizedObject; } else if (sanitizedObject instanceof DocumentReference) { DocumentReference docRef = (DocumentReference) sanitizedObject; return Value.newBuilder().setReferenceValue(docRef.getName()).build(); diff --git a/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/WriteBatchTest.java b/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/WriteBatchTest.java index 6522d98a8096..4ba3c2bc1f4f 100644 --- a/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/WriteBatchTest.java +++ b/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/WriteBatchTest.java @@ -139,6 +139,36 @@ public void setDocument() throws Exception { assertEquals(commit(writes.toArray(new Write[] {})), commitRequest); } + @Test + public void setDocumentWithValue() throws Exception { + doReturn(commitResponse(4, 0)) + .when(firestoreMock) + .sendRequest( + commitCapture.capture(), Matchers.>any()); + + batch + .set(documentReference, LocalFirestoreHelper.SINGLE_FIELD_PROTO) + .set(documentReference, LocalFirestoreHelper.SINGLE_FIELD_OBJECT) + .set(documentReference, LocalFirestoreHelper.SINGLE_FIELD_PROTO, SetOptions.merge()) + .set(documentReference, LocalFirestoreHelper.SINGLE_FIELD_OBJECT, SetOptions.merge()); + + List writes = new ArrayList<>(); + writes.add(set(LocalFirestoreHelper.SINGLE_FIELD_PROTO)); + writes.add(set(LocalFirestoreHelper.SINGLE_FIELD_PROTO)); + writes.add(set(LocalFirestoreHelper.SINGLE_FIELD_PROTO, Arrays.asList("foo"))); + writes.add(set(LocalFirestoreHelper.SINGLE_FIELD_PROTO, Arrays.asList("foo"))); + + assertEquals(4, batch.getMutationsSize()); + + List writeResults = batch.commit().get(); + for (int i = 0; i < writeResults.size(); ++i) { + assertEquals(Timestamp.ofTimeSecondsAndNanos(i, i), writeResults.get(i).getUpdateTime()); + } + + CommitRequest commitRequest = commitCapture.getValue(); + assertEquals(commit(writes.toArray(new Write[] {})), commitRequest); + } + @Test public void omitWriteResultForDocumentTransforms() throws Exception { doReturn(commitResponse(2, 0)) @@ -179,6 +209,31 @@ public void createDocument() throws Exception { assertEquals(commit(writes.toArray(new Write[] {})), commitRequest); } + @Test + public void createDocumentWithValue() throws Exception { + doReturn(commitResponse(2, 0)) + .when(firestoreMock) + .sendRequest( + commitCapture.capture(), Matchers.>any()); + + batch + .create(documentReference, LocalFirestoreHelper.SINGLE_FIELD_PROTO) + .create(documentReference, LocalFirestoreHelper.SINGLE_FIELD_OBJECT); + + assertEquals(2, batch.getMutationsSize()); + + List writeResults = batch.commit().get(); + List writes = new ArrayList<>(); + + for (int i = 0; i < writeResults.size(); ++i) { + assertEquals(Timestamp.ofTimeSecondsAndNanos(i, i), writeResults.get(i).getUpdateTime()); + writes.add(create(LocalFirestoreHelper.SINGLE_FIELD_PROTO)); + } + + CommitRequest commitRequest = commitCapture.getValue(); + assertEquals(commit(writes.toArray(new Write[] {})), commitRequest); + } + @Test public void deleteDocument() throws Exception { doReturn(commitResponse(2, 0)) diff --git a/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java b/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java index ea9771e36602..92d771ecf454 100644 --- a/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java +++ b/google-cloud-clients/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java @@ -149,6 +149,14 @@ public void createDocument() throws Exception { assertEquals(SINGLE_FIELD_OBJECT, documentSnapshot.toObject(SingleField.class)); } + @Test + public void createDocumentWithValue() throws Exception { + assertEquals(20, randomDoc.getId().length()); + randomDoc.create(LocalFirestoreHelper.SINGLE_FIELD_PROTO).get(); + DocumentSnapshot documentSnapshot = randomDoc.get().get(); + assertEquals(SINGLE_FIELD_OBJECT, documentSnapshot.toObject(SingleField.class)); + } + @Test public void setDocument() throws Exception { Map nanNullMap = new HashMap<>(); @@ -160,6 +168,14 @@ public void setDocument() throws Exception { assertEquals(null, documentSnapshot.get("null")); } + @Test + public void setDocumentWithValue() throws Exception { + assertEquals(20, randomDoc.getId().length()); + randomDoc.set(LocalFirestoreHelper.SINGLE_FIELD_PROTO).get(); + DocumentSnapshot documentSnapshot = randomDoc.get().get(); + assertEquals(SINGLE_FIELD_OBJECT, documentSnapshot.toObject(SingleField.class)); + } + @Test public void setDocumentWithMerge() throws Exception { Map originalMap =