Skip to content
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

Add x-goog-request-params to request header. #7440

Merged
merged 22 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 20 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/eighty-roses-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/firestore": patch
---

Fixed issue where count and lite API queries did not work with named databases.
27 changes: 18 additions & 9 deletions packages/firestore/src/remote/rest_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

import { SDK_VERSION } from '../../src/core/version';
import { Token } from '../api/credentials';
import { DatabaseId, DatabaseInfo } from '../core/database_info';
import {
DatabaseId,
DatabaseInfo,
DEFAULT_DATABASE_NAME
} from '../core/database_info';
import { debugAssert } from '../util/assert';
import { generateUniqueDebugId } from '../util/debug_uid';
import { FirestoreError } from '../util/error';
Expand Down Expand Up @@ -54,7 +58,8 @@ function getGoogApiClientValue(): string {
export abstract class RestConnection implements Connection {
protected readonly databaseId: DatabaseId;
protected readonly baseUrl: string;
private readonly databaseRoot: string;
private readonly databasePath: string;
private readonly requestParams: string;

get shouldResourcePathBeIncludedInRequest(): boolean {
// Both `invokeRPC()` and `invokeStreamingRPC()` use their `path` arguments to determine
Expand All @@ -65,13 +70,14 @@ export abstract class RestConnection implements Connection {
constructor(private readonly databaseInfo: DatabaseInfo) {
this.databaseId = databaseInfo.databaseId;
const proto = databaseInfo.ssl ? 'https' : 'http';
const projectId = encodeURIComponent(this.databaseId.projectId);
const databaseId = encodeURIComponent(this.databaseId.database);
this.baseUrl = proto + '://' + databaseInfo.host;
this.databaseRoot =
'projects/' +
this.databaseId.projectId +
'/databases/' +
this.databaseId.database +
'/documents';
this.databasePath = `projects/${projectId}/databases/${databaseId}`;
this.requestParams =
this.databaseId.database === DEFAULT_DATABASE_NAME
? `project_id=${projectId}`
: `project_id=${projectId}&database_id=${databaseId}`;
}

invokeRPC<Req, Resp>(
Expand All @@ -85,7 +91,10 @@ export abstract class RestConnection implements Connection {
const url = this.makeUrl(rpcName, path);
logDebug(LOG_TAG, `Sending RPC '${rpcName}' ${streamId}:`, url, req);

const headers = {};
const headers: StringMap = {
'google-cloud-resource-prefix': this.databasePath,
'x-goog-request-params': this.requestParams
};
this.modifyHeadersForRequest(headers, authToken, appCheckToken);

return this.performRPCRequest<Req, Resp>(rpcName, url, headers, req).then(
Expand Down
8 changes: 6 additions & 2 deletions packages/firestore/test/unit/remote/rest_connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ describe('RestConnection', () => {
'Content-Type': 'text/plain',
'X-Firebase-GMPID': 'test-app-id',
'X-Goog-Api-Client': `gl-js/ fire/${SDK_VERSION}`,
'x-firebase-appcheck': 'some-app-check-token'
'x-firebase-appcheck': 'some-app-check-token',
'x-goog-request-params': 'project_id=testproject',
'google-cloud-resource-prefix': 'projects/testproject/databases/(default)'
});
});

Expand All @@ -111,7 +113,9 @@ describe('RestConnection', () => {
expect(connection.lastHeaders).to.deep.equal({
'Content-Type': 'text/plain',
'X-Firebase-GMPID': 'test-app-id',
'X-Goog-Api-Client': `gl-js/ fire/${SDK_VERSION}`
'X-Goog-Api-Client': `gl-js/ fire/${SDK_VERSION}`,
'x-goog-request-params': 'project_id=testproject',
'google-cloud-resource-prefix': 'projects/testproject/databases/(default)'
// Note: AppCheck token should not exist here.
});
});
Expand Down
4 changes: 2 additions & 2 deletions scripts/emulator-testing/emulators/firestore-emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export class FirestoreEmulator extends Emulator {

constructor(port: number, projectId = 'test-emulator') {
super(
'cloud-firestore-emulator-v1.14.4.jar',
'cloud-firestore-emulator-v1.18.1.jar',
// Use locked version of emulator for test to be deterministic.
// The latest version can be found from firestore emulator doc:
// https://firebase.google.com/docs/firestore/security/test-rules-emulator
'https://storage.googleapis.com/firebase-preview-drop/emulator/cloud-firestore-emulator-v1.14.4.jar',
'https://storage.googleapis.com/firebase-preview-drop/emulator/cloud-firestore-emulator-v1.18.1.jar',
port
);
this.projectId = projectId;
Expand Down