Skip to content

Commit 8bbbece

Browse files
fix(Firestore): Rejecting 'null' and 'undefined' as arguments for CollectionRef.doc() (#188)
1 parent 7593f5e commit 8bbbece

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/firestore/api/database.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1639,11 +1639,13 @@ export class CollectionReference extends Query
16391639

16401640
doc(pathString?: string): firestore.DocumentReference {
16411641
validateBetweenNumberOfArgs('CollectionReference.doc', arguments, 0, 1);
1642-
validateOptionalArgType('CollectionReference.doc', 'string', 1, pathString);
1643-
if (pathString === undefined) {
1642+
// We allow omission of 'pathString' but explicitly prohibit passing in both
1643+
// 'undefined' and 'null'.
1644+
if (arguments.length === 0) {
16441645
pathString = AutoId.newId();
16451646
}
1646-
if (typeof pathString !== 'string' || pathString === '') {
1647+
validateArgType('CollectionReference.doc', 'string', 1, pathString);
1648+
if (pathString === '') {
16471649
throw new FirestoreError(
16481650
Code.INVALID_ARGUMENT,
16491651
'Document path must be a non-empty string'

tests/firestore/integration/api/validation.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,10 @@ apiDescribe('Validation:', persistence => {
194194
'Function CollectionReference.doc() requires its first ' +
195195
'argument to be of type string, but it was: null'
196196
);
197+
expect(() => baseCollectionRef.doc(undefined as any)).to.throw(
198+
'Function CollectionReference.doc() requires its first ' +
199+
'argument to be of type string, but it was: undefined'
200+
);
197201
expect(() => (baseCollectionRef.doc as any)('foo', 'bar')).to.throw(
198202
'Function CollectionReference.doc() requires between 0 and ' +
199203
'1 arguments, but was called with 2 arguments.'

0 commit comments

Comments
 (0)