Skip to content

Fix the implementation of collection() with multiple path segments. #5440

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

Merged
merged 2 commits into from
Sep 8, 2021
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
5 changes: 5 additions & 0 deletions .changeset/small-scissors-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/firestore": patch
---

Fix the implementation of `collection()` with multiple path segments.
7 changes: 3 additions & 4 deletions packages/firestore/src/lite-api/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,9 @@ export function collection(
'a DocumentReference or FirebaseFirestore'
);
}
const absolutePath = ResourcePath.fromString(
parent.path,
...pathSegments
).child(ResourcePath.fromString(path));
const absolutePath = parent._path.child(
ResourcePath.fromString(path, ...pathSegments)
);
validateCollectionPath(absolutePath);
return new CollectionReference(
parent.firestore,
Expand Down
24 changes: 20 additions & 4 deletions packages/firestore/test/lite/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ describe('collection', () => {
});
});

it('can be used relative to Firestore root with multiple arguments', () => {
return withTestDb(db => {
const result = collection(db, 'coll1/doc1', '/coll2', 'doc2/', '/coll3/');
expect(result).to.be.an.instanceOf(CollectionReference);
expect(result.id).to.equal('coll3');
expect(result.path).to.equal('coll1/doc1/coll2/doc2/coll3');
});
});

it('can be used relative to collection', () => {
return withTestDb(db => {
const result = collection(collection(db, 'coll'), 'doc/subcoll');
Expand All @@ -268,12 +277,19 @@ describe('collection', () => {
});
});

it('can be used with multiple arguments', () => {
it('can be used relative to collection with multiple arguments', () => {
return withTestDb(db => {
const result = collection(db, 'coll1/doc1', 'coll2');
const col = collection(db, 'coll1');
const result = collection(
col,
'/doc1/coll2/doc2/',
'/coll3',
'doc3/',
'/coll4/'
);
expect(result).to.be.an.instanceOf(CollectionReference);
expect(result.id).to.equal('coll2');
expect(result.path).to.equal('coll1/doc1/coll2');
expect(result.id).to.equal('coll4');
expect(result.path).to.equal('coll1/doc1/coll2/doc2/coll3/doc3/coll4');
});
});

Expand Down