Skip to content

Commit 21305c8

Browse files
fix: preserve default values in x-goog-request-params header (#411)
- [ ] Regenerate this pull request now. PiperOrigin-RevId: 474338479 Source-Link: googleapis/googleapis@d5d35e0 Source-Link: googleapis/googleapis-gen@efcd3f9 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZWZjZDNmOTM5NjJhMTAzZjY4ZjAwM2UyYTFlZWNkZTZmYTIxNmEyNyJ9
1 parent cfccd98 commit 21305c8

File tree

2 files changed

+59
-44
lines changed

2 files changed

+59
-44
lines changed

packages/google-cloud-phishingprotection/src/v1beta1/phishing_protection_service_v1_beta1_client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ export class PhishingProtectionServiceV1Beta1Client {
413413
options.otherArgs.headers = options.otherArgs.headers || {};
414414
options.otherArgs.headers['x-goog-request-params'] =
415415
this._gaxModule.routingHeader.fromParams({
416-
parent: request.parent || '',
416+
parent: request.parent ?? '',
417417
});
418418
this.initialize();
419419
return this.innerApiCalls.reportPhishing(request, options, callback);

packages/google-cloud-phishingprotection/test/gapic_phishing_protection_service_v1_beta1_v1beta1.ts

+58-43
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ import * as phishingprotectionservicev1beta1Module from '../src';
2525

2626
import {protobuf} from 'google-gax';
2727

28+
// Dynamically loaded proto JSON is needed to get the type information
29+
// to fill in default values for request objects
30+
const root = protobuf.Root.fromJSON(
31+
require('../protos/protos.json')
32+
).resolveAll();
33+
34+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
35+
function getTypeDefaultValue(typeName: string, fields: string[]) {
36+
let type = root.lookupType(typeName) as protobuf.Type;
37+
for (const field of fields.slice(0, -1)) {
38+
type = type.fields[field]?.resolvedType as protobuf.Type;
39+
}
40+
return type.fields[fields[fields.length - 1]]?.defaultValue;
41+
}
42+
2843
function generateSampleMessage<T extends object>(instance: T) {
2944
const filledObject = (
3045
instance.constructor as typeof protobuf.Message
@@ -191,26 +206,25 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
191206
const request = generateSampleMessage(
192207
new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingRequest()
193208
);
194-
request.parent = '';
195-
const expectedHeaderRequestParams = 'parent=';
196-
const expectedOptions = {
197-
otherArgs: {
198-
headers: {
199-
'x-goog-request-params': expectedHeaderRequestParams,
200-
},
201-
},
202-
};
209+
const defaultValue1 = getTypeDefaultValue('ReportPhishingRequest', [
210+
'parent',
211+
]);
212+
request.parent = defaultValue1;
213+
const expectedHeaderRequestParams = `parent=${defaultValue1}`;
203214
const expectedResponse = generateSampleMessage(
204215
new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingResponse()
205216
);
206217
client.innerApiCalls.reportPhishing = stubSimpleCall(expectedResponse);
207218
const [response] = await client.reportPhishing(request);
208219
assert.deepStrictEqual(response, expectedResponse);
209-
assert(
210-
(client.innerApiCalls.reportPhishing as SinonStub)
211-
.getCall(0)
212-
.calledWith(request, expectedOptions, undefined)
213-
);
220+
const actualRequest = (
221+
client.innerApiCalls.reportPhishing as SinonStub
222+
).getCall(0).args[0];
223+
assert.deepStrictEqual(actualRequest, request);
224+
const actualHeaderRequestParams = (
225+
client.innerApiCalls.reportPhishing as SinonStub
226+
).getCall(0).args[1].otherArgs.headers['x-goog-request-params'];
227+
assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams));
214228
});
215229

216230
it('invokes reportPhishing without error using callback', async () => {
@@ -225,15 +239,11 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
225239
const request = generateSampleMessage(
226240
new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingRequest()
227241
);
228-
request.parent = '';
229-
const expectedHeaderRequestParams = 'parent=';
230-
const expectedOptions = {
231-
otherArgs: {
232-
headers: {
233-
'x-goog-request-params': expectedHeaderRequestParams,
234-
},
235-
},
236-
};
242+
const defaultValue1 = getTypeDefaultValue('ReportPhishingRequest', [
243+
'parent',
244+
]);
245+
request.parent = defaultValue1;
246+
const expectedHeaderRequestParams = `parent=${defaultValue1}`;
237247
const expectedResponse = generateSampleMessage(
238248
new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingResponse()
239249
);
@@ -256,11 +266,14 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
256266
});
257267
const response = await promise;
258268
assert.deepStrictEqual(response, expectedResponse);
259-
assert(
260-
(client.innerApiCalls.reportPhishing as SinonStub)
261-
.getCall(0)
262-
.calledWith(request, expectedOptions /*, callback defined above */)
263-
);
269+
const actualRequest = (
270+
client.innerApiCalls.reportPhishing as SinonStub
271+
).getCall(0).args[0];
272+
assert.deepStrictEqual(actualRequest, request);
273+
const actualHeaderRequestParams = (
274+
client.innerApiCalls.reportPhishing as SinonStub
275+
).getCall(0).args[1].otherArgs.headers['x-goog-request-params'];
276+
assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams));
264277
});
265278

266279
it('invokes reportPhishing with error', async () => {
@@ -275,26 +288,25 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
275288
const request = generateSampleMessage(
276289
new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingRequest()
277290
);
278-
request.parent = '';
279-
const expectedHeaderRequestParams = 'parent=';
280-
const expectedOptions = {
281-
otherArgs: {
282-
headers: {
283-
'x-goog-request-params': expectedHeaderRequestParams,
284-
},
285-
},
286-
};
291+
const defaultValue1 = getTypeDefaultValue('ReportPhishingRequest', [
292+
'parent',
293+
]);
294+
request.parent = defaultValue1;
295+
const expectedHeaderRequestParams = `parent=${defaultValue1}`;
287296
const expectedError = new Error('expected');
288297
client.innerApiCalls.reportPhishing = stubSimpleCall(
289298
undefined,
290299
expectedError
291300
);
292301
await assert.rejects(client.reportPhishing(request), expectedError);
293-
assert(
294-
(client.innerApiCalls.reportPhishing as SinonStub)
295-
.getCall(0)
296-
.calledWith(request, expectedOptions, undefined)
297-
);
302+
const actualRequest = (
303+
client.innerApiCalls.reportPhishing as SinonStub
304+
).getCall(0).args[0];
305+
assert.deepStrictEqual(actualRequest, request);
306+
const actualHeaderRequestParams = (
307+
client.innerApiCalls.reportPhishing as SinonStub
308+
).getCall(0).args[1].otherArgs.headers['x-goog-request-params'];
309+
assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams));
298310
});
299311

300312
it('invokes reportPhishing with closed client', async () => {
@@ -309,7 +321,10 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
309321
const request = generateSampleMessage(
310322
new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingRequest()
311323
);
312-
request.parent = '';
324+
const defaultValue1 = getTypeDefaultValue('ReportPhishingRequest', [
325+
'parent',
326+
]);
327+
request.parent = defaultValue1;
313328
const expectedError = new Error('The client has already been closed.');
314329
client.close();
315330
await assert.rejects(client.reportPhishing(request), expectedError);

0 commit comments

Comments
 (0)