From 21305c8eab7fee963f92d0a5df94aad1b862e80e Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Wed, 14 Sep 2022 22:38:33 +0000 Subject: [PATCH] fix: preserve default values in x-goog-request-params header (#411) - [ ] Regenerate this pull request now. PiperOrigin-RevId: 474338479 Source-Link: https://github.com/googleapis/googleapis/commit/d5d35e0353b59719e8917103b1bc7df2782bf6ba Source-Link: https://github.com/googleapis/googleapis-gen/commit/efcd3f93962a103f68f003e2a1eecde6fa216a27 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZWZjZDNmOTM5NjJhMTAzZjY4ZjAwM2UyYTFlZWNkZTZmYTIxNmEyNyJ9 --- ...hing_protection_service_v1_beta1_client.ts | 2 +- ...ing_protection_service_v1_beta1_v1beta1.ts | 101 ++++++++++-------- 2 files changed, 59 insertions(+), 44 deletions(-) diff --git a/packages/google-cloud-phishingprotection/src/v1beta1/phishing_protection_service_v1_beta1_client.ts b/packages/google-cloud-phishingprotection/src/v1beta1/phishing_protection_service_v1_beta1_client.ts index 3b1224fa61d..f7e750b3954 100644 --- a/packages/google-cloud-phishingprotection/src/v1beta1/phishing_protection_service_v1_beta1_client.ts +++ b/packages/google-cloud-phishingprotection/src/v1beta1/phishing_protection_service_v1_beta1_client.ts @@ -413,7 +413,7 @@ export class PhishingProtectionServiceV1Beta1Client { options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ - parent: request.parent || '', + parent: request.parent ?? '', }); this.initialize(); return this.innerApiCalls.reportPhishing(request, options, callback); diff --git a/packages/google-cloud-phishingprotection/test/gapic_phishing_protection_service_v1_beta1_v1beta1.ts b/packages/google-cloud-phishingprotection/test/gapic_phishing_protection_service_v1_beta1_v1beta1.ts index ccadd4a2bab..d0349e0c29b 100644 --- a/packages/google-cloud-phishingprotection/test/gapic_phishing_protection_service_v1_beta1_v1beta1.ts +++ b/packages/google-cloud-phishingprotection/test/gapic_phishing_protection_service_v1_beta1_v1beta1.ts @@ -25,6 +25,21 @@ import * as phishingprotectionservicev1beta1Module 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(instance: T) { const filledObject = ( instance.constructor as typeof protobuf.Message @@ -191,26 +206,25 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => { const request = generateSampleMessage( new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingRequest() ); - request.parent = ''; - const expectedHeaderRequestParams = 'parent='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('ReportPhishingRequest', [ + 'parent', + ]); + request.parent = defaultValue1; + const expectedHeaderRequestParams = `parent=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingResponse() ); client.innerApiCalls.reportPhishing = stubSimpleCall(expectedResponse); const [response] = await client.reportPhishing(request); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.reportPhishing as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.reportPhishing as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.reportPhishing as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes reportPhishing without error using callback', async () => { @@ -225,15 +239,11 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => { const request = generateSampleMessage( new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingRequest() ); - request.parent = ''; - const expectedHeaderRequestParams = 'parent='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('ReportPhishingRequest', [ + 'parent', + ]); + request.parent = defaultValue1; + const expectedHeaderRequestParams = `parent=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingResponse() ); @@ -256,11 +266,14 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => { }); const response = await promise; assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.reportPhishing as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = ( + client.innerApiCalls.reportPhishing as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.reportPhishing as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes reportPhishing with error', async () => { @@ -275,26 +288,25 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => { const request = generateSampleMessage( new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingRequest() ); - request.parent = ''; - const expectedHeaderRequestParams = 'parent='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('ReportPhishingRequest', [ + 'parent', + ]); + request.parent = defaultValue1; + const expectedHeaderRequestParams = `parent=${defaultValue1}`; const expectedError = new Error('expected'); client.innerApiCalls.reportPhishing = stubSimpleCall( undefined, expectedError ); await assert.rejects(client.reportPhishing(request), expectedError); - assert( - (client.innerApiCalls.reportPhishing as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.reportPhishing as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.reportPhishing as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes reportPhishing with closed client', async () => { @@ -309,7 +321,10 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => { const request = generateSampleMessage( new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingRequest() ); - request.parent = ''; + const defaultValue1 = getTypeDefaultValue('ReportPhishingRequest', [ + 'parent', + ]); + request.parent = defaultValue1; const expectedError = new Error('The client has already been closed.'); client.close(); await assert.rejects(client.reportPhishing(request), expectedError);