Skip to content

Commit 447dbdd

Browse files
Allowing empty merges
1 parent 1fc5c1b commit 447dbdd

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ private T performSet(
245245
DocumentTransform.fromFieldPathMap(documentReference, documentData);
246246

247247
if (options.isMerge()) {
248-
Preconditions.checkArgument(!fields.isEmpty(), "Data to merge cannot be empty.");
249248
if (options.getFieldMask() != null) {
250249
List<FieldPath> fieldMask = new ArrayList<>(options.getFieldMask());
251250
fieldMask.removeAll(documentTransform.getFields());
@@ -257,11 +256,12 @@ private T performSet(
257256

258257
Mutation mutation = addMutation();
259258

260-
if (!options.isMerge() || !documentSnapshot.isEmpty() || !documentMask.isEmpty()) {
261-
mutation.document = documentSnapshot.toPb();
262-
}
259+
boolean hasDocumentData = !documentSnapshot.isEmpty() || !documentMask.isEmpty();
263260

264-
if (!documentMask.isEmpty()) {
261+
if (!options.isMerge()) {
262+
mutation.document = documentSnapshot.toPb();
263+
} else if (hasDocumentData || documentTransform.isEmpty()) {
264+
mutation.document = documentSnapshot.toPb();
265265
mutation.document.setUpdateMask(documentMask.toPb());
266266
}
267267

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public class ConformanceTest {
8585
private interface ConformanceTestCase extends Test, Describable {}
8686

8787
/** Excluded tests by test description. */
88-
private final Set<String> excludedTests = Collections.emptySet();
88+
private final Set<String> excludedTests =
89+
Collections.singleton("set: MergeAll cannot be specified with empty data."); // b/73495873
8990

9091
/** If non-empty, only runs tests included in this set. */
9192
private final Set<String> includedTests = Collections.emptySet();

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,20 @@ public void setDocumentWithMerge() throws Exception {
411411
}
412412
}
413413

414+
@Test
415+
public void setDocumentWithEmptyMerge() throws Exception {
416+
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
417+
.when(firestoreMock)
418+
.sendRequest(
419+
commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
420+
421+
documentReference.set(map(), SetOptions.merge()).get();
422+
423+
assertCommitEquals(
424+
commit(set(Collections.<String, Value>emptyMap(), Collections.<String>emptyList())),
425+
commitCapture.getValue());
426+
}
427+
414428
@Test
415429
public void setDocumentWithNestedMerge() throws Exception {
416430
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)

0 commit comments

Comments
 (0)