Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly bundle requests with snake_case #921

Merged
merged 1 commit into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/bundlingCalls/bundleDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ import {NormalApiCaller} from '../normalCalls/normalApiCaller';
import {BundleApiCaller} from './bundleApiCaller';
import {BundleExecutor} from './bundleExecutor';

/**
* Capitalizes the first character of the given string.
*/
function capitalize(str: string) {
if (str.length === 0) {
return str;
}
return str[0].toUpperCase() + str.slice(1);
}

/**
* Converts a given string from snake_case (normally used in proto definitions) to
* camelCase (used by protobuf.js)
*/
function toCamelCase(str: string) {
// split on spaces, non-alphanumeric, or capital letters
const splitted = str
.split(/(?=[A-Z])|[\s\W_]+/)
.filter(w => w.length > 0)
.map(word => word.toLowerCase());
if (splitted.length === 0) {
return str;
}
return [splitted[0], ...splitted.slice(1).map(capitalize)].join('');
}

/**
* A descriptor for calls that can be bundled into one call.
*/
Expand Down Expand Up @@ -68,7 +94,9 @@ export class BundleDescriptor implements Descriptor {
subresponseField = null;
}
this.bundledField = bundledField;
this.requestDiscriminatorFields = requestDiscriminatorFields;
this.requestDiscriminatorFields = requestDiscriminatorFields.map(
toCamelCase
);
this.subresponseField = subresponseField;
this.byteLengthFunction = byteLengthFunction;
}
Expand Down
49 changes: 49 additions & 0 deletions test/unit/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,4 +977,53 @@ describe('bundleable', () => {
});
p.cancel();
});

it('properly processes camel case fields', done => {
const descriptor = new BundleDescriptor(
'data',
['log_name'],
'data',
byteLength
);
const settings = {
settings: {bundleOptions},
descriptor,
};
const spy = sinon.spy(func);
const callback = sinon.spy(() => {
if (callback.callCount === 4) {
assert.strictEqual(spy.callCount, 2); // we expect two requests, each has two items
done();
}
});
const apiCall = createApiCall(spy, settings);
apiCall({data: ['data1'], logName: 'log1'}, undefined, err => {
if (err) {
done(err);
} else {
callback();
}
});
apiCall({data: ['data1'], logName: 'log2'}, undefined, err => {
if (err) {
done(err);
} else {
callback();
}
});
apiCall({data: ['data2'], logName: 'log1'}, undefined, err => {
if (err) {
done(err);
} else {
callback();
}
});
apiCall({data: ['data2'], logName: 'log2'}, undefined, err => {
if (err) {
done(err);
} else {
callback();
}
});
});
});