Skip to content

Fix bug where v1 HTTP used v2 API if callback had len 1 #955

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

Merged
merged 3 commits into from
Aug 16, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix bug where the <data> arg of https onCall functions sometimes deviates from the documented format.
22 changes: 22 additions & 0 deletions spec/v1/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,28 @@ describe('#onCall', () => {
};
expect(cf.run(data, context)).to.deep.equal({ data, context });
});

// Regression test for firebase-functions#947
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

it('should lock to the v1 API even with function.length == 1', async () => {
let gotData: Record<string, any>;
const func = https.onCall((data) => {
gotData = data;
});

const req = new MockRequest(
{
data: { foo: 'bar' },
},
{
'content-type': 'application/json',
}
);
req.method = 'POST';

const response = await runHandler(func, req as any);
expect(response.status).to.equal(200);
expect(gotData).to.deep.equal({ foo: 'bar' });
});
});

describe('callable CORS', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ export function _onCallWithOptions(
handler: (data: any, context: CallableContext) => any | Promise<any>,
options: DeploymentOptions
): HttpsFunction & Runnable<any> {
const func: any = onCallHandler({ origin: true, methods: 'POST' }, handler);
// onCallHandler sniffs the function length of the passed-in callback
// and the user could have only tried to listen to data. Wrap their handler
// in another handler to avoid accidentally triggering the v2 API
const fixedLen = (data: any, context: CallableContext) =>
handler(data, context);
const func: any = onCallHandler({ origin: true, methods: 'POST' }, fixedLen);

func.__trigger = {
labels: {},
Expand Down
6 changes: 5 additions & 1 deletion src/v2/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ export function onCall<T = any, Return = any | Promise<any>>(
}

const origin = 'cors' in opts ? opts.cors : true;
const func: any = onCallHandler({ origin, methods: 'POST' }, handler);

// onCallHandler sniffs the function length to determine which API to present.
// fix the length to prevent api versions from being mismatched.
const fixedLen = (req: CallableRequest<T>) => handler(req);
const func: any = onCallHandler({ origin, methods: 'POST' }, fixedLen);

Object.defineProperty(func, '__trigger', {
get: () => {
Expand Down