Skip to content

Commit 7485b07

Browse files
authored
Include a trailing /documents on root resource paths (#2249)
* Silence warnings in serializer.cc * Include a trailing /documents on root resource paths
1 parent a52abec commit 7485b07

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

Firestore/Example/Tests/Remote/FSTSerializerBetaTests.mm

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ - (void)testEncodesFirstLevelAncestorQueries {
533533
FSTQueryData *model = [self queryDataForQuery:q];
534534

535535
GCFSTarget *expected = [GCFSTarget message];
536-
expected.query.parent = @"projects/p/databases/d";
536+
expected.query.parent = @"projects/p/databases/d/documents";
537537
GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
538538
from.collectionId = @"messages";
539539
[expected.query.structuredQuery.fromArray addObject:from];
@@ -565,7 +565,7 @@ - (void)testEncodesSingleFiltersAtFirstLevelCollections {
565565
FSTQueryData *model = [self queryDataForQuery:q];
566566

567567
GCFSTarget *expected = [GCFSTarget message];
568-
expected.query.parent = @"projects/p/databases/d";
568+
expected.query.parent = @"projects/p/databases/d/documents";
569569
GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
570570
from.collectionId = @"docs";
571571
[expected.query.structuredQuery.fromArray addObject:from];
@@ -646,7 +646,7 @@ - (void)unaryFilterTestWithValue:(id)value
646646
FSTQueryData *model = [self queryDataForQuery:q];
647647

648648
GCFSTarget *expected = [GCFSTarget message];
649-
expected.query.parent = @"projects/p/databases/d";
649+
expected.query.parent = @"projects/p/databases/d/documents";
650650
GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
651651
from.collectionId = @"docs";
652652
[expected.query.structuredQuery.fromArray addObject:from];
@@ -668,7 +668,7 @@ - (void)testEncodesSortOrders {
668668
FSTQueryData *model = [self queryDataForQuery:q];
669669

670670
GCFSTarget *expected = [GCFSTarget message];
671-
expected.query.parent = @"projects/p/databases/d";
671+
expected.query.parent = @"projects/p/databases/d/documents";
672672
GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
673673
from.collectionId = @"docs";
674674
[expected.query.structuredQuery.fromArray addObject:from];
@@ -706,7 +706,7 @@ - (void)testEncodesLimits {
706706
FSTQueryData *model = [self queryDataForQuery:q];
707707

708708
GCFSTarget *expected = [GCFSTarget message];
709-
expected.query.parent = @"projects/p/databases/d";
709+
expected.query.parent = @"projects/p/databases/d/documents";
710710
GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
711711
from.collectionId = @"docs";
712712
[expected.query.structuredQuery.fromArray addObject:from];
@@ -728,7 +728,7 @@ - (void)testEncodesResumeTokens {
728728
resumeToken:FSTTestData(1, 2, 3, -1)];
729729

730730
GCFSTarget *expected = [GCFSTarget message];
731-
expected.query.parent = @"projects/p/databases/d";
731+
expected.query.parent = @"projects/p/databases/d/documents";
732732
GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
733733
from.collectionId = @"docs";
734734
[expected.query.structuredQuery.fromArray addObject:from];

Firestore/Source/Remote/FSTSerializerBeta.mm

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,15 @@ - (ResourcePath)decodedResourcePathWithDatabaseID:(NSString *)name {
152152
}
153153

154154
- (NSString *)encodedQueryPath:(const ResourcePath &)path {
155-
if (path.size() == 0) {
156-
// If the path is empty, the backend requires we leave off the /documents at the end.
157-
return [self encodedDatabaseID];
158-
}
159155
return [self encodedResourcePathForDatabaseID:self.databaseID path:path];
160156
}
161157

162158
- (ResourcePath)decodedQueryPath:(NSString *)name {
163159
const ResourcePath resource = [self decodedResourcePathWithDatabaseID:name];
164160
if (resource.size() == 4) {
161+
// In v1beta1 queries for collections at the root did not have a trailing "/documents". In v1
162+
// all resource paths contain "/documents". Preserve the ability to read the v1beta1 form for
163+
// compatibility with queries persisted in the local query cache.
165164
return ResourcePath{};
166165
} else {
167166
return [self localResourcePathForQualifiedResourcePath:resource];

Firestore/core/src/firebase/firestore/remote/serializer.cc

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ google_firestore_v1_MapValue EncodeMapValue(
141141

142142
size_t count = object_value_map.size();
143143

144-
result.fields_count = count;
144+
result.fields_count = static_cast<pb_size_t>(count);
145145
result.fields = MakeArray<google_firestore_v1_MapValue_FieldsEntry>(count);
146146

147147
int i = 0;
@@ -406,7 +406,7 @@ google_firestore_v1_Document Serializer::EncodeDocument(
406406

407407
// Encode Document.fields (unless it's empty)
408408
size_t count = object_value.internal_value.size();
409-
result.fields_count = count;
409+
result.fields_count = static_cast<pb_size_t>(count);
410410
result.fields = MakeArray<google_firestore_v1_Document_FieldsEntry>(count);
411411
int i = 0;
412412
for (const auto& kv : object_value.internal_value) {
@@ -512,7 +512,7 @@ google_firestore_v1_Target_QueryTarget Serializer::EncodeQueryTarget(
512512

513513
if (!collection_id.empty()) {
514514
size_t count = 1;
515-
result.structured_query.from_count = count;
515+
result.structured_query.from_count = static_cast<pb_size_t>(count);
516516
result.structured_query.from =
517517
MakeArray<google_firestore_v1_StructuredQuery_CollectionSelector>(
518518
count);
@@ -538,8 +538,10 @@ google_firestore_v1_Target_QueryTarget Serializer::EncodeQueryTarget(
538538
ResourcePath DecodeQueryPath(Reader* reader, absl::string_view name) {
539539
ResourcePath resource = DecodeResourceName(reader, name);
540540
if (resource.size() == 4) {
541-
// Path missing the trailing documents path segment, indicating an empty
542-
// path.
541+
// In v1beta1 queries for collections at the root did not have a trailing
542+
// "/documents". In v1 all resource paths contain "/documents". Preserve the
543+
// ability to read the v1beta1 form for compatibility with queries persisted
544+
// in the local query cache.
543545
return ResourcePath::Empty();
544546
} else {
545547
return ExtractLocalPathFromResourceName(reader, resource);
@@ -581,11 +583,6 @@ Query Serializer::DecodeQueryTarget(
581583
}
582584

583585
std::string Serializer::EncodeQueryPath(const ResourcePath& path) const {
584-
if (path.empty()) {
585-
// If the path is empty, the backend requires we leave off the /documents at
586-
// the end.
587-
return database_name_;
588-
}
589586
return EncodeResourceName(database_id_, path);
590587
}
591588

0 commit comments

Comments
 (0)