From d5baa68bc24ba696b21f50020f6d4c1180f1ea57 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Fri, 6 Oct 2017 12:48:39 -0700 Subject: [PATCH] fix(Firestore): Rejecting 'null' and 'undefined' as arguments for CollectionRef.doc() --- src/firestore/api/database.ts | 8 +++++--- tests/firestore/integration/api/validation.test.ts | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/firestore/api/database.ts b/src/firestore/api/database.ts index 7473a7cd443..e168467caca 100644 --- a/src/firestore/api/database.ts +++ b/src/firestore/api/database.ts @@ -1639,11 +1639,13 @@ export class CollectionReference extends Query doc(pathString?: string): firestore.DocumentReference { validateBetweenNumberOfArgs('CollectionReference.doc', arguments, 0, 1); - validateOptionalArgType('CollectionReference.doc', 'string', 1, pathString); - if (pathString === undefined) { + // We allow omission of 'pathString' but explicitly prohibit passing in both + // 'undefined' and 'null'. + if (arguments.length === 0) { pathString = AutoId.newId(); } - if (typeof pathString !== 'string' || pathString === '') { + validateArgType('CollectionReference.doc', 'string', 1, pathString); + if (pathString === '') { throw new FirestoreError( Code.INVALID_ARGUMENT, 'Document path must be a non-empty string' diff --git a/tests/firestore/integration/api/validation.test.ts b/tests/firestore/integration/api/validation.test.ts index 9c66751ed38..c2d30460740 100644 --- a/tests/firestore/integration/api/validation.test.ts +++ b/tests/firestore/integration/api/validation.test.ts @@ -194,6 +194,10 @@ apiDescribe('Validation:', persistence => { 'Function CollectionReference.doc() requires its first ' + 'argument to be of type string, but it was: null' ); + expect(() => baseCollectionRef.doc(undefined as any)).to.throw( + 'Function CollectionReference.doc() requires its first ' + + 'argument to be of type string, but it was: undefined' + ); expect(() => (baseCollectionRef.doc as any)('foo', 'bar')).to.throw( 'Function CollectionReference.doc() requires between 0 and ' + '1 arguments, but was called with 2 arguments.'