File tree Expand file tree Collapse file tree 3 files changed +29
-2
lines changed Expand file tree Collapse file tree 3 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -175,6 +175,25 @@ describe('#onCall', () => {
175
175
} ;
176
176
expect ( cf . run ( data , context ) ) . to . deep . equal ( { data, context } ) ;
177
177
} ) ;
178
+
179
+ // Regression test for firebase-functions#947
180
+ it ( 'should lock to the v1 API even with function.length == 1' , async ( ) => {
181
+ let gotData : Record < string , any > ;
182
+ const func = https . onCall ( data => {
183
+ gotData = data ;
184
+ } ) ;
185
+
186
+ const req = new MockRequest ( {
187
+ data : { "foo" : "bar" } ,
188
+ } , {
189
+ "content-type" : "application/json" ,
190
+ } ) ;
191
+ req . method = "POST" ;
192
+
193
+ const response = await runHandler ( func , req as any ) ;
194
+ expect ( response . status ) . to . equal ( 200 ) ;
195
+ expect ( gotData ) . to . deep . equal ( { "foo" : "bar" } ) ;
196
+ } ) ;
178
197
} ) ;
179
198
180
199
describe ( 'callable CORS' , ( ) => {
Original file line number Diff line number Diff line change @@ -77,7 +77,11 @@ export function _onCallWithOptions(
77
77
handler : ( data : any , context : CallableContext ) => any | Promise < any > ,
78
78
options : DeploymentOptions
79
79
) : HttpsFunction & Runnable < any > {
80
- const func : any = onCallHandler ( { origin : true , methods : 'POST' } , handler ) ;
80
+ // onCallHandler sniffs the function length of the passed-in callback
81
+ // and the user could have only tried to listen to data. Wrap their handler
82
+ // in another handler to avoid accidentally triggering the v2 API
83
+ const fixedLen = ( data : any , context : CallableContext ) => handler ( data , context ) ;
84
+ const func : any = onCallHandler ( { origin : true , methods : 'POST' } , fixedLen ) ;
81
85
82
86
func . __trigger = {
83
87
labels : { } ,
Original file line number Diff line number Diff line change @@ -144,7 +144,11 @@ export function onCall<T = any, Return = any | Promise<any>>(
144
144
}
145
145
146
146
const origin = 'cors' in opts ? opts . cors : true ;
147
- const func : any = onCallHandler ( { origin, methods : 'POST' } , handler ) ;
147
+
148
+ // onCallHandler sniffs the function length to determine which API to present.
149
+ // fix the length to prevent api versions from being mismatched.
150
+ const fixedLen = ( req : CallableRequest < T > ) => handler ( req ) ;
151
+ const func : any = onCallHandler ( { origin, methods : 'POST' } , fixedLen ) ;
148
152
149
153
Object . defineProperty ( func , '__trigger' , {
150
154
get : ( ) => {
You can’t perform that action at this time.
0 commit comments