Skip to content

Commit

Permalink
fix: change dynamic routing header field to camelCase (#1091)
Browse files Browse the repository at this point in the history
* fix: camelCase field dynamic routing header

* run formatting and lint fixer

* update test

* remove test case
  • Loading branch information
alicejli committed Jan 24, 2022
1 parent 45863c4 commit 1e05e76
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ service TestService {
routing_parameters {
field: "name"
}
routing_parameters {
field: "name"
path_template: "{routing_id=**}"
}
};
};

Expand All @@ -64,15 +68,15 @@ service TestService {
// This routing annotation is only for baseline testing purposes.
option (google.api.routing) = {
routing_parameters {
field: "parent",
field: "parent_id",
path_template: "{database=projects/*}"
}
routing_parameters {
field: "parent",
field: "nest1.nest2.name_id",
path_template: "{database=projects/*/databases/*}/documents"
}
routing_parameters {
field: "parent",
field: "another_parent_id",
path_template: "{routing_id=projects/*}/databases/*/documents"
}
};
Expand All @@ -82,10 +86,18 @@ service TestService {
// This is a message description.
// Lorum ipsum dolor sit amet consectetur adipiscing elit.
message FibonacciRequest {
message Inner1{
message Inner2{
string name_id = 1;
}
Inner2 nest2 = 1;
}
Inner1 nest1 = 1;
// The nth term to retrieve in the Fibonacci sequence.
int32 value = 1;
int32 value = 2;
// This is for testing the routing annotation.
string name = 2;
string name = 3;
// This is for testing the routing annotation.
string parent = 3;
string parent_id = 4;
string another_parent_id = 5;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function main() {
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
/**
*/
// const nest1 = {}
/**
* The nth term to retrieve in the Fibonacci sequence.
*/
Expand All @@ -31,7 +34,10 @@ function main() {
/**
* This is for testing the routing annotation.
*/
// const parent = 'abc123'
// const parentId = 'abc123'
/**
*/
// const anotherParentId = 'abc123'

// Imports the Routingtest library
const {TestServiceClient} = require('routingtest').v1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function main() {
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
/**
*/
// const nest1 = {}
/**
* The nth term to retrieve in the Fibonacci sequence.
*/
Expand All @@ -31,7 +34,10 @@ function main() {
/**
* This is for testing the routing annotation.
*/
// const parent = 'abc123'
// const parentId = 'abc123'
/**
*/
// const anotherParentId = 'abc123'

// Imports the Routingtest library
const {TestServiceClient} = require('routingtest').v1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function main() {
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
/**
*/
// const nest1 = {}
/**
* The nth term to retrieve in the Fibonacci sequence.
*/
Expand All @@ -31,7 +34,10 @@ function main() {
/**
* This is for testing the routing annotation.
*/
// const parent = 'abc123'
// const parentId = 'abc123'
/**
*/
// const anotherParentId = 'abc123'

// Imports the Routingtest library
const {TestServiceClient} = require('routingtest').v1;
Expand Down
32 changes: 21 additions & 11 deletions baselines/routingtest/src/v1/test_service_client.ts.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,14 @@ export class TestServiceClient {
*
* @param {Object} request
* The request object that will be sent.
* @param {google.routingtest.v1.FibonacciRequest.Inner1} request.nest1
* @param {number} request.value
* The nth term to retrieve in the Fibonacci sequence.
* @param {string} request.name
* This is for testing the routing annotation.
* @param {string} request.parent
* @param {string} request.parentId
* This is for testing the routing annotation.
* @param {string} request.anotherParentId
* @param {object} [options]
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Promise} - The promise which resolves to an array.
Expand Down Expand Up @@ -341,7 +343,7 @@ export class TestServiceClient {
options = options || {};
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};let routingParameter = {};
if(RegExp('(?<database>projects)/[^/]+/databases/[^/]+/documents').test(request.name!)){
if(RegExp('(?<database>projects)/[^/]+/databases/[^/]+/documents').test(request.name!)){
Object.assign(routingParameter, { database: request.name!.match(RegExp('(?<database>projects/[^/]+/databases/[^/]+)'))![0]})
} else
{}
Expand All @@ -358,12 +360,14 @@ export class TestServiceClient {
*
* @param {Object} request
* The request object that will be sent.
* @param {google.routingtest.v1.FibonacciRequest.Inner1} request.nest1
* @param {number} request.value
* The nth term to retrieve in the Fibonacci sequence.
* @param {string} request.name
* This is for testing the routing annotation.
* @param {string} request.parent
* @param {string} request.parentId
* This is for testing the routing annotation.
* @param {string} request.anotherParentId
* @param {object} [options]
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Promise} - The promise which resolves to an array.
Expand Down Expand Up @@ -420,9 +424,13 @@ export class TestServiceClient {
options = options || {};
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};let routingParameter = {};
if(RegExp('[^/]+').test(request.name!)){
if(RegExp('[^/]+').test(request.name!)){
Object.assign(routingParameter, { name: request.name!.match(RegExp('[^/]+'))![0]})
} else
{}
if(RegExp('(?<routing_id>(?:/.*)?)').test(request.name!)){
Object.assign(routingParameter, { routing_id: request.name!.match(RegExp('(?<routing_id>.*)'))![0]})
} else
{}
options.otherArgs.headers[
'x-goog-request-params'
Expand All @@ -437,12 +445,14 @@ export class TestServiceClient {
*
* @param {Object} request
* The request object that will be sent.
* @param {google.routingtest.v1.FibonacciRequest.Inner1} request.nest1
* @param {number} request.value
* The nth term to retrieve in the Fibonacci sequence.
* @param {string} request.name
* This is for testing the routing annotation.
* @param {string} request.parent
* @param {string} request.parentId
* This is for testing the routing annotation.
* @param {string} request.anotherParentId
* @param {object} [options]
* Call options. See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions} for more details.
* @returns {Promise} - The promise which resolves to an array.
Expand Down Expand Up @@ -499,15 +509,15 @@ export class TestServiceClient {
options = options || {};
options.otherArgs = options.otherArgs || {};
options.otherArgs.headers = options.otherArgs.headers || {};let routingParameter = {};
if(RegExp('(?<database>projects)/[^/]+/databases/[^/]+/documents').test(request.parent!)){
Object.assign(routingParameter, { database: request.parent!.match(RegExp('(?<database>projects/[^/]+/databases/[^/]+)'))![0]})
if(RegExp('(?<database>projects)/[^/]+/databases/[^/]+/documents').test(request.nest1.nest2.nameId!)){
Object.assign(routingParameter, { database: request.nest1.nest2.nameId!.match(RegExp('(?<database>projects/[^/]+/databases/[^/]+)'))![0]})
} else
if(RegExp('(?<database>projects)/[^/]+').test(request.parent!)){
Object.assign(routingParameter, { database: request.parent!.match(RegExp('(?<database>projects/[^/]+)'))![0]})
if(RegExp('(?<database>projects)/[^/]+').test(request.parentId!)){
Object.assign(routingParameter, { database: request.parentId!.match(RegExp('(?<database>projects/[^/]+)'))![0]})
} else
{}
if(RegExp('(?<routing_id>projects)/[^/]+/databases/[^/]+/documents').test(request.parent!)){
Object.assign(routingParameter, { routing_id: request.parent!.match(RegExp('(?<routing_id>projects/[^/]+)'))![0]})
if(RegExp('(?<routing_id>projects)/[^/]+/databases/[^/]+/documents').test(request.anotherParentId!)){
Object.assign(routingParameter, { routing_id: request.anotherParentId!.match(RegExp('(?<routing_id>projects/[^/]+)'))![0]})
} else
{}
options.otherArgs.headers[
Expand Down
6 changes: 3 additions & 3 deletions templates/typescript_gapic/_util.njk
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ request.{{ oneComment.paramName.toCamelCase() }}
{%- if method.dynamicRoutingRequestParams.length > 0 -%}
let routingParameter = {};
{%- for paramArray in method.dynamicRoutingRequestParams %}
{%- for sameParamArray in paramArray %}
if(RegExp('{{ sameParamArray.messageRegex | safe }}').test(request.{{ sameParamArray.fieldRetrieve }}!)){
Object.assign(routingParameter, { {{ sameParamArray.fieldSend }}: request.{{ sameParamArray.fieldRetrieve }}!.match(RegExp('{{ sameParamArray.namedSegment | safe }}'))![0]})
{%- for param in paramArray %}
if(RegExp('{{ param.messageRegex | safe }}').test(request.{{ param.fieldRetrieve }}!)){
Object.assign(routingParameter, { {{ param.fieldSend }}: request.{{ param.fieldRetrieve }}!.match(RegExp('{{ param.namedSegment | safe }}'))![0]})
} else
{%- endfor %}
{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ service TestService {
routing_parameters {
field: "name"
}
routing_parameters {
field: "name"
path_template: "{routing_id=**}"
}
};
};

Expand All @@ -64,15 +68,15 @@ service TestService {
// This routing annotation is only for baseline testing purposes.
option (google.api.routing) = {
routing_parameters {
field: "parent",
field: "parent_id",
path_template: "{database=projects/*}"
}
routing_parameters {
field: "parent",
field: "nest1.nest2.name_id",
path_template: "{database=projects/*/databases/*}/documents"
}
routing_parameters {
field: "parent",
field: "another_parent_id",
path_template: "{routing_id=projects/*}/databases/*/documents"
}
};
Expand All @@ -82,10 +86,18 @@ service TestService {
// This is a message description.
// Lorum ipsum dolor sit amet consectetur adipiscing elit.
message FibonacciRequest {
message Inner1{
message Inner2{
string name_id = 1;
}
Inner2 nest2 = 1;
}
Inner1 nest1 = 1;
// The nth term to retrieve in the Fibonacci sequence.
int32 value = 1;
int32 value = 2;
// This is for testing the routing annotation.
string name = 2;
string name = 3;
// This is for testing the routing annotation.
string parent = 3;
string parent_id = 4;
string another_parent_id = 5;
}
14 changes: 12 additions & 2 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,16 @@ export interface DynamicRoutingParameters {
namedSegment: string;
}

// The field to be retrieved needs to be converted into camelCase
export function convertFieldToCamelCase(field: string) {
const camelCaseFields: string[] = [];
const fieldsToRetrieve = field.split('.');
fieldsToRetrieve.forEach(field => {
camelCaseFields.push(field.toCamelCase());
});
return camelCaseFields.join('.');
}

// This parses a single Routing Parameter and returns a MapRoutingParameters interface.
export function getSingleRoutingHeaderParam(
rule: protos.google.api.IRoutingParameter
Expand All @@ -684,7 +694,7 @@ export function getSingleRoutingHeaderParam(
} else if (!rule.pathTemplate) {
// If there is no path template, then capture the full field from the message
dynamicRoutingRule = {
fieldRetrieve: rule.field,
fieldRetrieve: convertFieldToCamelCase(rule.field),
fieldSend: rule.field,
messageRegex: '[^/]+',
namedSegment: '[^/]+',
Expand All @@ -695,7 +705,7 @@ export function getSingleRoutingHeaderParam(
return dynamicRoutingRule;
} else {
dynamicRoutingRule = {
fieldRetrieve: rule.field,
fieldRetrieve: convertFieldToCamelCase(rule.field),
fieldSend: getNamedSegment(rule.pathTemplate)[1],
messageRegex: convertTemplateToRegex(rule.pathTemplate),
namedSegment: getNamedSegment(rule.pathTemplate)[3],
Expand Down
30 changes: 25 additions & 5 deletions typescript/test/unit/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as assert from 'assert';
import {describe, it} from 'mocha';
import * as protos from '../../../protos';
import {
convertFieldToCamelCase,
DynamicRoutingParameters,
getDynamicHeaderRequestParams,
getHeaderRequestParams,
Expand Down Expand Up @@ -191,7 +192,7 @@ describe('src/schema/proto.ts', () => {
],
[
{
fieldRetrieve: 'app_profile_id',
fieldRetrieve: 'appProfileId',
fieldSend: 'profile_id',
messageRegex: '(?<profile_id>projects)/[^/]+(?:/.*)?',
namedSegment: '(?<profile_id>projects/[^/]+)',
Expand Down Expand Up @@ -237,7 +238,7 @@ describe('src/schema/proto.ts', () => {
],
[
{
fieldRetrieve: 'app_profile_id',
fieldRetrieve: 'appProfileId',
fieldSend: 'profile_id',
messageRegex: '(?<profile_id>projects)/[^/]+(?:/.*)?',
namedSegment: '(?<profile_id>projects/[^/]+)',
Expand All @@ -251,6 +252,25 @@ describe('src/schema/proto.ts', () => {
});
});

describe('should return a string set to camelCase', () => {
it('should return this to camelCase', () => {
assert.deepStrictEqual(
convertFieldToCamelCase('name.name2.name3'),
'name.name2.name3'
);
assert.deepStrictEqual(convertFieldToCamelCase(''), '');
assert.deepStrictEqual(convertFieldToCamelCase('parent_id'), 'parentId');
assert.deepStrictEqual(
convertFieldToCamelCase('app_profile_id'),
'appProfileId'
);
assert.deepStrictEqual(
convertFieldToCamelCase('name.parent_id.another_parent_id'),
'name.parentId.anotherParentId'
);
});
});

describe('should get return an array from a single routing parameters rule', () => {
it('should return an empty DynamicRoutingParameters interface if the annotation is malformed', () => {
const routingRule: protos.google.api.IRoutingParameter = {
Expand Down Expand Up @@ -298,11 +318,11 @@ describe('src/schema/proto.ts', () => {
});
it('works with a basic path template', () => {
const routingRule: protos.google.api.IRoutingParameter = {
field: 'app_profile_id',
field: 'app_profile_id.parent_id',
pathTemplate: '{routing_id=**}',
};
const expectedRoutingParameters: DynamicRoutingParameters = {
fieldRetrieve: 'app_profile_id',
fieldRetrieve: 'appProfileId.parentId',
fieldSend: 'routing_id',
messageRegex: '(?<routing_id>(?:/.*)?)',
namedSegment: '(?<routing_id>.*)',
Expand All @@ -318,7 +338,7 @@ describe('src/schema/proto.ts', () => {
pathTemplate: '{routing_id=projects/*}/**',
};
const expectedRoutingParameters: DynamicRoutingParameters = {
fieldRetrieve: 'app_profile_id',
fieldRetrieve: 'appProfileId',
fieldSend: 'routing_id',
messageRegex: '(?<routing_id>projects)/[^/]+(?:/.*)?',
namedSegment: '(?<routing_id>projects/[^/]+)',
Expand Down

0 comments on commit 1e05e76

Please sign in to comment.