Skip to content

Commit

Permalink
fix: proper processing of routing headers (#1160)
Browse files Browse the repository at this point in the history
* fix: proper processing of routing headers

* fix: pr feedback

* fix: make generated unit tests work
  • Loading branch information
alexander-fenster committed Jul 20, 2022
1 parent 5331691 commit 6e9f77f
Show file tree
Hide file tree
Showing 8 changed files with 410 additions and 448 deletions.
117 changes: 81 additions & 36 deletions baselines/routingtest/src/v1/test_service_client.ts.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,23 @@ export class TestServiceClient {
}
options = options || {};
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};let routingParameter = {};
if ((typeof request.name !== "undefined") && RegExp('(?<database>projects)/[^/]+/databases/[^/]+/documents').test(request.name!)){
Object.assign(routingParameter, { database: request.name!.match(RegExp('(?<database>projects/[^/]+/databases/[^/]+)'))![0]})}

options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams(
routingParameter
);
options.otherArgs.headers = options.otherArgs.headers || {};
let routingParameter = {};
{
const fieldValue = request.name;
if (fieldValue !== undefined && fieldValue !== null) {
const match = fieldValue.toString().match(RegExp('(?<database>projects/[^/]+/databases/[^/]+)/documents'));
if (match) {
const parameterValue = match.groups?.['database'] ?? fieldValue;
Object.assign(routingParameter, { database: parameterValue });
}
}
}
options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams(
routingParameter
);
this.initialize();
return this.innerApiCalls.fastFibonacci(request, options, callback);
}
Expand Down Expand Up @@ -421,18 +429,33 @@ export class TestServiceClient {
}
options = options || {};
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};let routingParameter = {};
if ((typeof request.name !== "undefined") && RegExp('[^/]+').test(request.name!)){
Object.assign(routingParameter, { name: request.name!.match(RegExp('[^/]+'))![0]})}

if ((typeof request.name !== "undefined") && RegExp('(?<routing_id>(?:/.*)?)').test(request.name!)){
Object.assign(routingParameter, { routing_id: request.name!.match(RegExp('(?<routing_id>.*)'))![0]})}

options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams(
routingParameter
);
options.otherArgs.headers = options.otherArgs.headers || {};
let routingParameter = {};
{
const fieldValue = request.name;
if (fieldValue !== undefined && fieldValue !== null) {
const match = fieldValue.toString().match(RegExp('(?<name>.*)'));
if (match) {
const parameterValue = match.groups?.['name'] ?? fieldValue;
Object.assign(routingParameter, { name: parameterValue });
}
}
}
{
const fieldValue = request.name;
if (fieldValue !== undefined && fieldValue !== null) {
const match = fieldValue.toString().match(RegExp('(?<routing_id>(?:.*)?)'));
if (match) {
const parameterValue = match.groups?.['routing_id'] ?? fieldValue;
Object.assign(routingParameter, { routing_id: parameterValue });
}
}
}
options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams(
routingParameter
);
this.initialize();
return this.innerApiCalls.randomFibonacci(request, options, callback);
}
Expand Down Expand Up @@ -504,21 +527,43 @@ export class TestServiceClient {
}
options = options || {};
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};let routingParameter = {};
if ((typeof request.parentId !== "undefined") && RegExp('(?<database>projects)/[^/]+').test(request.parentId!)){
Object.assign(routingParameter, { database: request.parentId!.match(RegExp('(?<database>projects/[^/]+)'))![0]})}

if ((typeof request.nest1?.nest2?.nameId !== "undefined") && RegExp('(?<database>projects)/[^/]+/databases/[^/]+/documents').test(request.nest1?.nest2?.nameId!)){
Object.assign(routingParameter, { database: request.nest1?.nest2?.nameId!.match(RegExp('(?<database>projects/[^/]+/databases/[^/]+)'))![0]})}

if ((typeof request.anotherParentId !== "undefined") && RegExp('(?<routing_id>projects)/[^/]+/databases/[^/]+/documents').test(request.anotherParentId!)){
Object.assign(routingParameter, { routing_id: request.anotherParentId!.match(RegExp('(?<routing_id>projects/[^/]+)'))![0]})}

options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams(
routingParameter
);
options.otherArgs.headers = options.otherArgs.headers || {};
let routingParameter = {};
{
const fieldValue = request.parentId;
if (fieldValue !== undefined && fieldValue !== null) {
const match = fieldValue.toString().match(RegExp('(?<database>projects/[^/]+)'));
if (match) {
const parameterValue = match.groups?.['database'] ?? fieldValue;
Object.assign(routingParameter, { database: parameterValue });
}
}
}
{
const fieldValue = request.nest1?.nest2?.nameId;
if (fieldValue !== undefined && fieldValue !== null) {
const match = fieldValue.toString().match(RegExp('(?<database>projects/[^/]+/databases/[^/]+)/documents'));
if (match) {
const parameterValue = match.groups?.['database'] ?? fieldValue;
Object.assign(routingParameter, { database: parameterValue });
}
}
}
{
const fieldValue = request.anotherParentId;
if (fieldValue !== undefined && fieldValue !== null) {
const match = fieldValue.toString().match(RegExp('(?<routing_id>projects/[^/]+)/databases/[^/]+/documents'));
if (match) {
const parameterValue = match.groups?.['routing_id'] ?? fieldValue;
Object.assign(routingParameter, { routing_id: parameterValue });
}
}
}
options.otherArgs.headers[
'x-goog-request-params'
] = gax.routingHeader.fromParams(
routingParameter
);
this.initialize();
return this.innerApiCalls.slowFibonacci(request, options, callback);
}
Expand Down
101 changes: 92 additions & 9 deletions baselines/routingtest/test/gapic_test_service_v1.ts.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ describe('v1.TestServiceClient', () => {
});
client.initialize();
const request = generateSampleMessage(new protos.google.routingtest.v1.FibonacciRequest());
const expectedHeaderRequestParams = "";
const expectedHeaderRequestParamsObj: {[key: string]: string} = {};
// path template: {database=projects/*/databases/*}/documents
request.name = 'projects/value/databases/value/documents';
expectedHeaderRequestParamsObj['database'] = 'projects%2Fvalue%2Fdatabases%2Fvalue';
const expectedHeaderRequestParams = Object.entries(expectedHeaderRequestParamsObj).map(([key, value]) => `${key}=${value}`).join('&');
const expectedOptions = {
otherArgs: {
headers: {
Expand All @@ -164,7 +168,11 @@ describe('v1.TestServiceClient', () => {
});
client.initialize();
const request = generateSampleMessage(new protos.google.routingtest.v1.FibonacciRequest());
const expectedHeaderRequestParams = "";
const expectedHeaderRequestParamsObj: {[key: string]: string} = {};
// path template: {database=projects/*/databases/*}/documents
request.name = 'projects/value/databases/value/documents';
expectedHeaderRequestParamsObj['database'] = 'projects%2Fvalue%2Fdatabases%2Fvalue';
const expectedHeaderRequestParams = Object.entries(expectedHeaderRequestParamsObj).map(([key, value]) => `${key}=${value}`).join('&');
const expectedOptions = {
otherArgs: {
headers: {
Expand Down Expand Up @@ -198,7 +206,11 @@ describe('v1.TestServiceClient', () => {
});
client.initialize();
const request = generateSampleMessage(new protos.google.routingtest.v1.FibonacciRequest());
const expectedHeaderRequestParams = "";
const expectedHeaderRequestParamsObj: {[key: string]: string} = {};
// path template: {database=projects/*/databases/*}/documents
request.name = 'projects/value/databases/value/documents';
expectedHeaderRequestParamsObj['database'] = 'projects%2Fvalue%2Fdatabases%2Fvalue';
const expectedHeaderRequestParams = Object.entries(expectedHeaderRequestParamsObj).map(([key, value]) => `${key}=${value}`).join('&');
const expectedOptions = {
otherArgs: {
headers: {
Expand All @@ -220,6 +232,8 @@ describe('v1.TestServiceClient', () => {
});
client.initialize();
const request = generateSampleMessage(new protos.google.routingtest.v1.FibonacciRequest());
// path template: {database=projects/*/databases/*}/documents
request.name = 'projects/value/databases/value/documents';
const expectedError = new Error('The client has already been closed.');
client.close();
await assert.rejects(client.fastFibonacci(request), expectedError);
Expand All @@ -234,7 +248,14 @@ describe('v1.TestServiceClient', () => {
});
client.initialize();
const request = generateSampleMessage(new protos.google.routingtest.v1.FibonacciRequest());
const expectedHeaderRequestParams = "";
const expectedHeaderRequestParamsObj: {[key: string]: string} = {};
// path template is empty
request.name = 'value';
expectedHeaderRequestParamsObj['name'] = 'value';
// path template: {routing_id=**}
request.name = 'value';
expectedHeaderRequestParamsObj['routing_id'] = 'value';
const expectedHeaderRequestParams = Object.entries(expectedHeaderRequestParamsObj).map(([key, value]) => `${key}=${value}`).join('&');
const expectedOptions = {
otherArgs: {
headers: {
Expand All @@ -257,7 +278,14 @@ describe('v1.TestServiceClient', () => {
});
client.initialize();
const request = generateSampleMessage(new protos.google.routingtest.v1.FibonacciRequest());
const expectedHeaderRequestParams = "";
const expectedHeaderRequestParamsObj: {[key: string]: string} = {};
// path template is empty
request.name = 'value';
expectedHeaderRequestParamsObj['name'] = 'value';
// path template: {routing_id=**}
request.name = 'value';
expectedHeaderRequestParamsObj['routing_id'] = 'value';
const expectedHeaderRequestParams = Object.entries(expectedHeaderRequestParamsObj).map(([key, value]) => `${key}=${value}`).join('&');
const expectedOptions = {
otherArgs: {
headers: {
Expand Down Expand Up @@ -291,7 +319,14 @@ describe('v1.TestServiceClient', () => {
});
client.initialize();
const request = generateSampleMessage(new protos.google.routingtest.v1.FibonacciRequest());
const expectedHeaderRequestParams = "";
const expectedHeaderRequestParamsObj: {[key: string]: string} = {};
// path template is empty
request.name = 'value';
expectedHeaderRequestParamsObj['name'] = 'value';
// path template: {routing_id=**}
request.name = 'value';
expectedHeaderRequestParamsObj['routing_id'] = 'value';
const expectedHeaderRequestParams = Object.entries(expectedHeaderRequestParamsObj).map(([key, value]) => `${key}=${value}`).join('&');
const expectedOptions = {
otherArgs: {
headers: {
Expand All @@ -313,6 +348,10 @@ describe('v1.TestServiceClient', () => {
});
client.initialize();
const request = generateSampleMessage(new protos.google.routingtest.v1.FibonacciRequest());
// path template is empty
request.name = 'value';
// path template: {routing_id=**}
request.name = 'value';
const expectedError = new Error('The client has already been closed.');
client.close();
await assert.rejects(client.randomFibonacci(request), expectedError);
Expand All @@ -327,7 +366,19 @@ describe('v1.TestServiceClient', () => {
});
client.initialize();
const request = generateSampleMessage(new protos.google.routingtest.v1.FibonacciRequest());
const expectedHeaderRequestParams = "";
const expectedHeaderRequestParamsObj: {[key: string]: string} = {};
// path template: {database=projects/*}
request.parentId = 'projects/value';
expectedHeaderRequestParamsObj['database'] = 'projects%2Fvalue';
request.nest1 = {};
request.nest1.nest2 = {};
// path template: {database=projects/*/databases/*}/documents
request.nest1.nest2.nameId = 'projects/value/databases/value/documents';
expectedHeaderRequestParamsObj['database'] = 'projects%2Fvalue%2Fdatabases%2Fvalue';
// path template: {routing_id=projects/*}/databases/*/documents
request.anotherParentId = 'projects/value/databases/value/documents';
expectedHeaderRequestParamsObj['routing_id'] = 'projects%2Fvalue';
const expectedHeaderRequestParams = Object.entries(expectedHeaderRequestParamsObj).map(([key, value]) => `${key}=${value}`).join('&');
const expectedOptions = {
otherArgs: {
headers: {
Expand All @@ -350,7 +401,19 @@ describe('v1.TestServiceClient', () => {
});
client.initialize();
const request = generateSampleMessage(new protos.google.routingtest.v1.FibonacciRequest());
const expectedHeaderRequestParams = "";
const expectedHeaderRequestParamsObj: {[key: string]: string} = {};
// path template: {database=projects/*}
request.parentId = 'projects/value';
expectedHeaderRequestParamsObj['database'] = 'projects%2Fvalue';
request.nest1 = {};
request.nest1.nest2 = {};
// path template: {database=projects/*/databases/*}/documents
request.nest1.nest2.nameId = 'projects/value/databases/value/documents';
expectedHeaderRequestParamsObj['database'] = 'projects%2Fvalue%2Fdatabases%2Fvalue';
// path template: {routing_id=projects/*}/databases/*/documents
request.anotherParentId = 'projects/value/databases/value/documents';
expectedHeaderRequestParamsObj['routing_id'] = 'projects%2Fvalue';
const expectedHeaderRequestParams = Object.entries(expectedHeaderRequestParamsObj).map(([key, value]) => `${key}=${value}`).join('&');
const expectedOptions = {
otherArgs: {
headers: {
Expand Down Expand Up @@ -384,7 +447,19 @@ describe('v1.TestServiceClient', () => {
});
client.initialize();
const request = generateSampleMessage(new protos.google.routingtest.v1.FibonacciRequest());
const expectedHeaderRequestParams = "";
const expectedHeaderRequestParamsObj: {[key: string]: string} = {};
// path template: {database=projects/*}
request.parentId = 'projects/value';
expectedHeaderRequestParamsObj['database'] = 'projects%2Fvalue';
request.nest1 = {};
request.nest1.nest2 = {};
// path template: {database=projects/*/databases/*}/documents
request.nest1.nest2.nameId = 'projects/value/databases/value/documents';
expectedHeaderRequestParamsObj['database'] = 'projects%2Fvalue%2Fdatabases%2Fvalue';
// path template: {routing_id=projects/*}/databases/*/documents
request.anotherParentId = 'projects/value/databases/value/documents';
expectedHeaderRequestParamsObj['routing_id'] = 'projects%2Fvalue';
const expectedHeaderRequestParams = Object.entries(expectedHeaderRequestParamsObj).map(([key, value]) => `${key}=${value}`).join('&');
const expectedOptions = {
otherArgs: {
headers: {
Expand All @@ -406,6 +481,14 @@ describe('v1.TestServiceClient', () => {
});
client.initialize();
const request = generateSampleMessage(new protos.google.routingtest.v1.FibonacciRequest());
// path template: {database=projects/*}
request.parentId = 'projects/value';
request.nest1 = {};
request.nest1.nest2 = {};
// path template: {database=projects/*/databases/*}/documents
request.nest1.nest2.nameId = 'projects/value/databases/value/documents';
// path template: {routing_id=projects/*}/databases/*/documents
request.anotherParentId = 'projects/value/databases/value/documents';
const expectedError = new Error('The client has already been closed.');
client.close();
await assert.rejects(client.slowFibonacci(request), expectedError);
Expand Down
Loading

0 comments on commit 6e9f77f

Please sign in to comment.