Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/firebase_functions/https_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def on_request_wrapped(request: Request) -> Response:
return _cross_origin(
methods=options.cors.cors_methods,
origins=options.cors.cors_origins,
)(func)(request)
)(_core._with_init(func))(request)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think (with the help of AI) this works, but has high overhead as we don't need to call _with_init or _cross_origin on every request.

Do you think this would work and be more efficient?

def on_request_inner_decorator(func: _C1):
       
        func_with_init = _core._with_init(func)
       
        if options.cors is not None:
            wrapped_func = _cross_origin(
                methods=options.cors.cors_methods,
                origins=options.cors.cors_origins,
            )(func_with_init)
        else:
            wrapped_func = func_with_init

        @_functools.wraps(func)
        def on_request_wrapped(request: Request) -> Response:
            return wrapped_func(request)
        _util.set_func_endpoint_attr(
            on_request_wrapped,
            options._endpoint(func_name=func.__name__),
        )
        return on_request_wrapped

I think this is consistent with oncall below? What do you think?

return _core._with_init(func)(request)

_util.set_func_endpoint_attr(
Expand Down
29 changes: 29 additions & 0 deletions tests/test_https_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from werkzeug.test import EnvironBuilder

from firebase_functions import core, https_fn
from firebase_functions.options import CorsOptions


class TestHttps(unittest.TestCase):
Expand Down Expand Up @@ -42,6 +43,34 @@ def init():

self.assertEqual(hello, "world")

def test_on_request_calls_init_function_with_cors(self):
app = Flask(__name__)

hello = None

@core.init
def init():
nonlocal hello
hello = "world"

func = Mock(__name__="example_func", return_value="OK")

with app.test_request_context("/"):
environ = EnvironBuilder(
method="POST",
json={
"data": {"test": "value"},
},
).get_environ()
request = Request(environ)
decorated_func = https_fn.on_request(
cors=CorsOptions(cors_origins="*", cors_methods="GET")
)(func)

decorated_func(request)

self.assertEqual(hello, "world")

def test_on_call_calls_init_function(self):
app = Flask(__name__)

Expand Down
Loading