Skip to content

Commit

Permalink
fix: preserve default values in x-goog-request-params header (#237)
Browse files Browse the repository at this point in the history
- [ ] Regenerate this pull request now.

PiperOrigin-RevId: 474338479

Source-Link: googleapis/googleapis@d5d35e0

Source-Link: googleapis/googleapis-gen@efcd3f9
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZWZjZDNmOTM5NjJhMTAzZjY4ZjAwM2UyYTFlZWNkZTZmYTIxNmEyNyJ9
  • Loading branch information
gcf-owl-bot[bot] authored Sep 14, 2022
1 parent d7922ad commit d8aae19
Show file tree
Hide file tree
Showing 4 changed files with 374 additions and 352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ export class AlphaAnalyticsDataClient {
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
this._gaxModule.routingHeader.fromParams({
property: request.property || '',
property: request.property ?? '',
});
this.initialize();
return this.innerApiCalls.runFunnelReport(request, options, callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ export class BetaAnalyticsDataClient {
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
this._gaxModule.routingHeader.fromParams({
property: request.property || '',
property: request.property ?? '',
});
this.initialize();
return this.innerApiCalls.runReport(request, options, callback);
Expand Down Expand Up @@ -613,7 +613,7 @@ export class BetaAnalyticsDataClient {
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
this._gaxModule.routingHeader.fromParams({
property: request.property || '',
property: request.property ?? '',
});
this.initialize();
return this.innerApiCalls.runPivotReport(request, options, callback);
Expand Down Expand Up @@ -716,7 +716,7 @@ export class BetaAnalyticsDataClient {
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
this._gaxModule.routingHeader.fromParams({
property: request.property || '',
property: request.property ?? '',
});
this.initialize();
return this.innerApiCalls.batchRunReports(request, options, callback);
Expand Down Expand Up @@ -825,7 +825,7 @@ export class BetaAnalyticsDataClient {
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
this._gaxModule.routingHeader.fromParams({
property: request.property || '',
property: request.property ?? '',
});
this.initialize();
return this.innerApiCalls.batchRunPivotReports(request, options, callback);
Expand Down Expand Up @@ -935,7 +935,7 @@ export class BetaAnalyticsDataClient {
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
this._gaxModule.routingHeader.fromParams({
name: request.name || '',
name: request.name ?? '',
});
this.initialize();
return this.innerApiCalls.getMetadata(request, options, callback);
Expand Down Expand Up @@ -1068,7 +1068,7 @@ export class BetaAnalyticsDataClient {
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
this._gaxModule.routingHeader.fromParams({
property: request.property || '',
property: request.property ?? '',
});
this.initialize();
return this.innerApiCalls.runRealtimeReport(request, options, callback);
Expand Down Expand Up @@ -1199,7 +1199,7 @@ export class BetaAnalyticsDataClient {
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
this._gaxModule.routingHeader.fromParams({
property: request.property || '',
property: request.property ?? '',
});
this.initialize();
return this.innerApiCalls.checkCompatibility(request, options, callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ import * as alphaanalyticsdataModule from '../src';

import {protobuf} from 'google-gax';

// Dynamically loaded proto JSON is needed to get the type information
// to fill in default values for request objects
const root = protobuf.Root.fromJSON(
require('../protos/protos.json')
).resolveAll();

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function getTypeDefaultValue(typeName: string, fields: string[]) {
let type = root.lookupType(typeName) as protobuf.Type;
for (const field of fields.slice(0, -1)) {
type = type.fields[field]?.resolvedType as protobuf.Type;
}
return type.fields[fields[fields.length - 1]]?.defaultValue;
}

function generateSampleMessage<T extends object>(instance: T) {
const filledObject = (
instance.constructor as typeof protobuf.Message
Expand Down Expand Up @@ -168,26 +183,25 @@ describe('v1alpha.AlphaAnalyticsDataClient', () => {
const request = generateSampleMessage(
new protos.google.analytics.data.v1alpha.RunFunnelReportRequest()
);
request.property = '';
const expectedHeaderRequestParams = 'property=';
const expectedOptions = {
otherArgs: {
headers: {
'x-goog-request-params': expectedHeaderRequestParams,
},
},
};
const defaultValue1 = getTypeDefaultValue('RunFunnelReportRequest', [
'property',
]);
request.property = defaultValue1;
const expectedHeaderRequestParams = `property=${defaultValue1}`;
const expectedResponse = generateSampleMessage(
new protos.google.analytics.data.v1alpha.RunFunnelReportResponse()
);
client.innerApiCalls.runFunnelReport = stubSimpleCall(expectedResponse);
const [response] = await client.runFunnelReport(request);
assert.deepStrictEqual(response, expectedResponse);
assert(
(client.innerApiCalls.runFunnelReport as SinonStub)
.getCall(0)
.calledWith(request, expectedOptions, undefined)
);
const actualRequest = (
client.innerApiCalls.runFunnelReport as SinonStub
).getCall(0).args[0];
assert.deepStrictEqual(actualRequest, request);
const actualHeaderRequestParams = (
client.innerApiCalls.runFunnelReport as SinonStub
).getCall(0).args[1].otherArgs.headers['x-goog-request-params'];
assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams));
});

it('invokes runFunnelReport without error using callback', async () => {
Expand All @@ -200,15 +214,11 @@ describe('v1alpha.AlphaAnalyticsDataClient', () => {
const request = generateSampleMessage(
new protos.google.analytics.data.v1alpha.RunFunnelReportRequest()
);
request.property = '';
const expectedHeaderRequestParams = 'property=';
const expectedOptions = {
otherArgs: {
headers: {
'x-goog-request-params': expectedHeaderRequestParams,
},
},
};
const defaultValue1 = getTypeDefaultValue('RunFunnelReportRequest', [
'property',
]);
request.property = defaultValue1;
const expectedHeaderRequestParams = `property=${defaultValue1}`;
const expectedResponse = generateSampleMessage(
new protos.google.analytics.data.v1alpha.RunFunnelReportResponse()
);
Expand All @@ -231,11 +241,14 @@ describe('v1alpha.AlphaAnalyticsDataClient', () => {
});
const response = await promise;
assert.deepStrictEqual(response, expectedResponse);
assert(
(client.innerApiCalls.runFunnelReport as SinonStub)
.getCall(0)
.calledWith(request, expectedOptions /*, callback defined above */)
);
const actualRequest = (
client.innerApiCalls.runFunnelReport as SinonStub
).getCall(0).args[0];
assert.deepStrictEqual(actualRequest, request);
const actualHeaderRequestParams = (
client.innerApiCalls.runFunnelReport as SinonStub
).getCall(0).args[1].otherArgs.headers['x-goog-request-params'];
assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams));
});

it('invokes runFunnelReport with error', async () => {
Expand All @@ -248,26 +261,25 @@ describe('v1alpha.AlphaAnalyticsDataClient', () => {
const request = generateSampleMessage(
new protos.google.analytics.data.v1alpha.RunFunnelReportRequest()
);
request.property = '';
const expectedHeaderRequestParams = 'property=';
const expectedOptions = {
otherArgs: {
headers: {
'x-goog-request-params': expectedHeaderRequestParams,
},
},
};
const defaultValue1 = getTypeDefaultValue('RunFunnelReportRequest', [
'property',
]);
request.property = defaultValue1;
const expectedHeaderRequestParams = `property=${defaultValue1}`;
const expectedError = new Error('expected');
client.innerApiCalls.runFunnelReport = stubSimpleCall(
undefined,
expectedError
);
await assert.rejects(client.runFunnelReport(request), expectedError);
assert(
(client.innerApiCalls.runFunnelReport as SinonStub)
.getCall(0)
.calledWith(request, expectedOptions, undefined)
);
const actualRequest = (
client.innerApiCalls.runFunnelReport as SinonStub
).getCall(0).args[0];
assert.deepStrictEqual(actualRequest, request);
const actualHeaderRequestParams = (
client.innerApiCalls.runFunnelReport as SinonStub
).getCall(0).args[1].otherArgs.headers['x-goog-request-params'];
assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams));
});

it('invokes runFunnelReport with closed client', async () => {
Expand All @@ -280,7 +292,10 @@ describe('v1alpha.AlphaAnalyticsDataClient', () => {
const request = generateSampleMessage(
new protos.google.analytics.data.v1alpha.RunFunnelReportRequest()
);
request.property = '';
const defaultValue1 = getTypeDefaultValue('RunFunnelReportRequest', [
'property',
]);
request.property = defaultValue1;
const expectedError = new Error('The client has already been closed.');
client.close();
await assert.rejects(client.runFunnelReport(request), expectedError);
Expand Down
Loading

0 comments on commit d8aae19

Please sign in to comment.