Skip to content

Commit

Permalink
fix: disable auto-pagination for Talent API (#281)
Browse files Browse the repository at this point in the history
* for talent API, add unit test

* lint

* clean

* add pass unit test & fix condition check

* lint
  • Loading branch information
xiaozhenliu-gg5 authored Feb 18, 2020
1 parent cf40d0e commit 02aa5e5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
31 changes: 27 additions & 4 deletions typescript/src/schema/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,26 @@ function streaming(method: MethodDescriptorProto) {
return undefined;
}

function pagingField(messages: MessagesMap, method: MethodDescriptorProto) {
function pagingField(
messages: MessagesMap,
method: MethodDescriptorProto,
service?: ServiceDescriptorProto
) {
// TODO: remove this once the next version of the Talent API is published.
//
// This is a workaround to disable auto-pagination for specifc RPCs in
// Talent v4beta1. The API team will make their API non-conforming in the
// next version.
//
// This should not be done for any other API.
const serviceName =
service && service.packageName === 'google.cloud.talent.v4beta1';
const methodName =
method.name === 'SearchProfiles' || method.name === 'SearchJobs';
if (serviceName && methodName) {
return undefined;
}

const inputType = messages[method.inputType!];
const outputType = messages[method.outputType!];
const hasPageToken =
Expand Down Expand Up @@ -201,8 +220,12 @@ function pagingField(messages: MessagesMap, method: MethodDescriptorProto) {
return repeatedFields[0];
}

function pagingFieldName(messages: MessagesMap, method: MethodDescriptorProto) {
const field = pagingField(messages, method);
function pagingFieldName(
messages: MessagesMap,
method: MethodDescriptorProto,
service?: ServiceDescriptorProto
) {
const field = pagingField(messages, method, service);
return field?.name;
}

Expand Down Expand Up @@ -277,7 +300,7 @@ function augmentMethod(
method
),
streaming: streaming(method),
pagingFieldName: pagingFieldName(messages, method),
pagingFieldName: pagingFieldName(messages, method, service),
pagingResponseType: pagingResponseType(messages, method),
inputInterface: method.inputType!,
outputInterface: method.outputType!,
Expand Down
65 changes: 65 additions & 0 deletions typescript/test/unit/proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import * as assert from 'assert';
import { describe, it } from 'mocha';
import * as plugin from '../../../pbjs-genfiles/plugin';
import { getHeaderRequestParams } from '../../src/schema/proto';
import { Proto } from '../../src/schema/proto';
import { ResourceDatabase } from '../../src/schema/resource-database';

describe('src/schema/proto.ts', () => {
describe('should get header parameters from http rule', () => {
Expand Down Expand Up @@ -92,4 +94,67 @@ describe('src/schema/proto.ts', () => {
);
});
});
describe('special work around for talent API', () => {
it('The pagingFieldName should be undefined for SearchJobs & SearchProfiles rpc', () => {
const fd = new plugin.google.protobuf.FileDescriptorProto();
fd.name = 'google/cloud/talent/v4beta1/service.proto';
fd.package = 'google.cloud.talent.v4beta1';
fd.service = [new plugin.google.protobuf.ServiceDescriptorProto()];
fd.service[0].name = 'service';
fd.service[0].method = [
new plugin.google.protobuf.MethodDescriptorProto(),
];
fd.service[0].method[0] = new plugin.google.protobuf.MethodDescriptorProto();
fd.service[0].method[0].name = 'SearchJobs';
fd.service[0].method[1] = new plugin.google.protobuf.MethodDescriptorProto();
fd.service[0].method[1].name = 'SearchProfiles';
fd.service[0].method[2] = new plugin.google.protobuf.MethodDescriptorProto();
fd.service[0].method[2].name = 'ListJobs';
fd.service[0].method[2].outputType =
'.google.cloud.talent.v4beta1.ListJobsOutput';
fd.service[0].method[2].inputType =
'.google.cloud.talent.v4beta1.ListJobsInput';

fd.messageType = [new plugin.google.protobuf.DescriptorProto()];
fd.messageType[0] = new plugin.google.protobuf.DescriptorProto();
fd.messageType[1] = new plugin.google.protobuf.DescriptorProto();

fd.messageType[0].name = 'ListJobsOutput';
fd.messageType[1].name = 'ListJobsInput';

fd.messageType[0].field = [
new plugin.google.protobuf.FieldDescriptorProto(),
];
fd.messageType[0].field[0] = new plugin.google.protobuf.FieldDescriptorProto();
fd.messageType[0].field[0].name = 'next_page_token';
fd.messageType[0].field[0].label =
plugin.google.protobuf.FieldDescriptorProto.Label.LABEL_REPEATED;
fd.messageType[1].field = [
new plugin.google.protobuf.FieldDescriptorProto(),
];
fd.messageType[1].field[0] = new plugin.google.protobuf.FieldDescriptorProto();
fd.messageType[1].field[0].name = 'page_size';
fd.messageType[1].field[1] = new plugin.google.protobuf.FieldDescriptorProto();
fd.messageType[1].field[1].name = 'page_token';
const proto = new Proto(
fd,
'google.cloud.talent.v4beta1',
new plugin.grpc.service_config.ServiceConfig(),
new ResourceDatabase(),
new ResourceDatabase()
);
assert.deepStrictEqual(
proto.services['service'].method[0].pagingFieldName,
undefined
);
assert.deepStrictEqual(
proto.services['service'].method[1].pagingFieldName,
undefined
);
assert.deepStrictEqual(
proto.services['service'].method[2].pagingFieldName,
'next_page_token'
);
});
});
});

0 comments on commit 02aa5e5

Please sign in to comment.