Skip to content

Commit bf5772f

Browse files
authored
Fix the implementation of collection() with multiple path segments. (#5440)
* Fix the implementation of `collection()` with multiple path segments. * Create small-scissors-try.md
1 parent 2d46e08 commit bf5772f

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

.changeset/small-scissors-try.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/firestore": patch
3+
---
4+
5+
Fix the implementation of `collection()` with multiple path segments.

packages/firestore/src/lite-api/reference.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,9 @@ export function collection(
385385
'a DocumentReference or FirebaseFirestore'
386386
);
387387
}
388-
const absolutePath = ResourcePath.fromString(
389-
parent.path,
390-
...pathSegments
391-
).child(ResourcePath.fromString(path));
388+
const absolutePath = parent._path.child(
389+
ResourcePath.fromString(path, ...pathSegments)
390+
);
392391
validateCollectionPath(absolutePath);
393392
return new CollectionReference(
394393
parent.firestore,

packages/firestore/test/lite/integration.test.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,15 @@ describe('collection', () => {
250250
});
251251
});
252252

253+
it('can be used relative to Firestore root with multiple arguments', () => {
254+
return withTestDb(db => {
255+
const result = collection(db, 'coll1/doc1', '/coll2', 'doc2/', '/coll3/');
256+
expect(result).to.be.an.instanceOf(CollectionReference);
257+
expect(result.id).to.equal('coll3');
258+
expect(result.path).to.equal('coll1/doc1/coll2/doc2/coll3');
259+
});
260+
});
261+
253262
it('can be used relative to collection', () => {
254263
return withTestDb(db => {
255264
const result = collection(collection(db, 'coll'), 'doc/subcoll');
@@ -268,12 +277,19 @@ describe('collection', () => {
268277
});
269278
});
270279

271-
it('can be used with multiple arguments', () => {
280+
it('can be used relative to collection with multiple arguments', () => {
272281
return withTestDb(db => {
273-
const result = collection(db, 'coll1/doc1', 'coll2');
282+
const col = collection(db, 'coll1');
283+
const result = collection(
284+
col,
285+
'/doc1/coll2/doc2/',
286+
'/coll3',
287+
'doc3/',
288+
'/coll4/'
289+
);
274290
expect(result).to.be.an.instanceOf(CollectionReference);
275-
expect(result.id).to.equal('coll2');
276-
expect(result.path).to.equal('coll1/doc1/coll2');
291+
expect(result.id).to.equal('coll4');
292+
expect(result.path).to.equal('coll1/doc1/coll2/doc2/coll3/doc3/coll4');
277293
});
278294
});
279295

0 commit comments

Comments
 (0)