diff --git a/Firestore/Example/Tests/Integration/API/FIRValidationTests.mm b/Firestore/Example/Tests/Integration/API/FIRValidationTests.mm index cebc09a543..6491db7cda 100644 --- a/Firestore/Example/Tests/Integration/API/FIRValidationTests.mm +++ b/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 " diff --git a/Firestore/core/src/firebase/firestore/core/user_data.cc b/Firestore/core/src/firebase/firestore/core/user_data.cc index 6b46494436..e4c4e8a5ed 100644 --- a/Firestore/core/src/firebase/firestore/core/user_data.cc +++ b/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()); } }