Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject conflicting updates #2582

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,13 @@ boolean isPrefixOf(B path) {
abstract String[] splitChildPath(String path);

abstract B createPathWithSegments(ImmutableList<String> segments);

boolean isPrefixOf(BasePath<B> field) {
if (field.getSegments().size() < this.getSegments().size()) {
return false;
}

return this.equals(
createPathWithSegments(field.getSegments().subList(0, this.getSegments().size())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* field in the document).
*/
@AutoValue
public abstract class FieldPath extends BasePath<FieldPath> {
public abstract class FieldPath extends BasePath<FieldPath> implements Comparable<FieldPath> {

/**
* A special sentinel FieldPath to refer to the ID of a document. It can be used in queries to
Expand Down Expand Up @@ -152,4 +152,9 @@ FieldPath createPathWithSegments(ImmutableList<String> segments) {
public String toString() {
return getEncodedPath();
}

@Override
public int compareTo(@Nonnull FieldPath other) {
return getEncodedPath().compareTo(other.getEncodedPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand All @@ -56,10 +58,18 @@ abstract class UpdateBuilder<T extends UpdateBuilder> {
private static Map<String, Object> expandObject(Map<FieldPath, Object> data) {
Map<String, Object> result = new HashMap<>();

for (Map.Entry<FieldPath, Object> entrySet : data.entrySet()) {
List<String> segments = entrySet.getKey().getSegments();
Object value = entrySet.getValue();
SortedSet<FieldPath> sortedFields = new TreeSet<>(data.keySet());

FieldPath lastField = null;

for (FieldPath field : sortedFields) {
if (lastField != null && lastField.isPrefixOf(field)) {
throw new IllegalArgumentException(
String.format("Detected ambiguous definition for field '%s'.", lastField));
}

List<String> segments = field.getSegments();
Object value = data.get(field);
Map<String, Object> currentMap = result;

for (int i = 0; i < segments.size(); ++i) {
Expand All @@ -69,9 +79,12 @@ private static Map<String, Object> expandObject(Map<FieldPath, Object> data) {
if (!currentMap.containsKey(segments.get(i))) {
currentMap.put(segments.get(i), new HashMap<>());
}

currentMap = (Map<String, Object>) currentMap.get(segments.get(i));
}
}

lastField = field;
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,45 @@ public void updateWithDotsInFieldName() throws Exception {
assertCommitEquals(expectedCommit, commitCapture.getValue());
}

@Test
public void updateConflictingFields() throws Exception {
try {
documentReference
.update("a.b", "foo", "a", "foo")
.get();
fail();
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "Detected ambiguous definition for field 'a'.");
}

try {
documentReference
.update("a.b", "foo", "a.b.c", "foo")
.get();
fail();
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "Detected ambiguous definition for field 'a.b'.");
}

try {
documentReference
.update("a.b", SINGLE_FIELD_MAP, "a", SINGLE_FIELD_MAP)
.get();
fail();
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "Detected ambiguous definition for field 'a'.");
}

try {
documentReference
.update("a.b", SINGLE_FIELD_MAP, "a.b.c", SINGLE_FIELD_MAP)
.get();
fail();
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "Detected ambiguous definition for field 'a.b'.");
}
}

@Test
public void deleteField() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
Expand Down