Răsfoiți Sursa

Disallow empty document fields (#4229)

Port of firebase/firebase-android-sdk#643.

Also make error messages align with Android's.
Gil 6 ani în urmă
părinte
comite
38d9643a00

+ 15 - 8
Firestore/Example/Tests/Integration/API/FIRValidationTests.mm

@@ -263,23 +263,30 @@ namespace testutil = firebase::firestore::testutil;
 
 - (void)testWritesWithReservedFieldsFail {
   [self expectWrite:@{@"__baz__" : @1}
-      toFailWithReason:@"Document fields cannot begin and end with __ (found in field __baz__)"];
+      toFailWithReason:@"Invalid data. Document fields cannot begin and end with \"__\" (found in "
+                       @"field __baz__)"];
   [self expectWrite:@{@"foo" : @{@"__baz__" : @1}}
-      toFailWithReason:
-          @"Document fields cannot begin and end with __ (found in field foo.__baz__)"];
+      toFailWithReason:@"Invalid data. Document fields cannot begin and end with \"__\" (found in "
+                       @"field foo.__baz__)"];
   [self expectWrite:@{@"__baz__" : @{@"foo" : @1}}
-      toFailWithReason:@"Document fields cannot begin and end with __ (found in field __baz__)"];
+      toFailWithReason:@"Invalid data. Document fields cannot begin and end with \"__\" (found in "
+                       @"field __baz__)"];
 
   [self expectUpdate:@{@"foo.__baz__" : @1}
-      toFailWithReason:
-          @"Document fields cannot begin and end with __ (found in field foo.__baz__)"];
+      toFailWithReason:@"Invalid data. Document fields cannot begin and end with \"__\" (found in "
+                       @"field foo.__baz__)"];
   [self expectUpdate:@{@"__baz__.foo" : @1}
-      toFailWithReason:
-          @"Document fields cannot begin and end with __ (found in field __baz__.foo)"];
+      toFailWithReason:@"Invalid data. Document fields cannot begin and end with \"__\" (found in "
+                       @"field __baz__.foo)"];
   [self expectUpdate:@{@1 : @1}
       toFailWithReason:@"Dictionary keys in updateData: must be NSStrings or FIRFieldPaths."];
 }
 
+- (void)testWritesMustNotContainEmptyFieldNames {
+  [self expectSet:@{@"" : @"foo"}
+      toFailWithReason:@"Invalid data. Document fields must not be empty (found in field ``)"];
+}
+
 - (void)testSetsWithFieldValueDeleteFail {
   [self expectSet:@{@"foo" : [FIRFieldValue fieldValueForDelete]}
       toFailWithReason:@"FieldValue.delete() can only be used with updateData() and setData() with "

+ 7 - 2
Firestore/core/src/firebase/firestore/core/user_data.cc

@@ -181,10 +181,15 @@ void ParseContext::ValidatePath() const {
 
 void ParseContext::ValidatePathSegment(absl::string_view segment) const {
   absl::string_view designator{RESERVED_FIELD_DESIGNATOR};
+  if (segment.empty()) {
+    ThrowInvalidArgument("Invalid data. Document fields must not be empty%s",
+                         FieldDescription());
+  }
   if (write() && absl::StartsWith(segment, designator) &&
       absl::EndsWith(segment, designator)) {
-    ThrowInvalidArgument("Document fields cannot begin and end with %s%s",
-                         RESERVED_FIELD_DESIGNATOR, FieldDescription());
+    ThrowInvalidArgument(
+        "Invalid data. Document fields cannot begin and end with \"%s\"%s",
+        RESERVED_FIELD_DESIGNATOR, FieldDescription());
   }
 }