diff --git a/azure/functions/decorators/function_app.py b/azure/functions/decorators/function_app.py index fa7a6314..6166ae65 100644 --- a/azure/functions/decorators/function_app.py +++ b/azure/functions/decorators/function_app.py @@ -288,6 +288,30 @@ def app_script_file(self) -> str: """ return self._app_script_file + def function_name(self, name: str, + setting_extra_fields: Dict[str, Any] = {}, + ) -> Callable[..., Any]: + """Optional: Sets name of the :class:`Function` object. If not set, + it will default to the name of the method name. + + :param name: Name of the function. + :param setting_extra_fields: Keyword arguments for specifying + additional setting fields + :return: Decorator function. + """ + + @self._configure_function_builder + def wrap(fb): + def decorator(): + fb.add_setting(setting=FunctionName( + function_name=name, + **setting_extra_fields)) + return fb + + return decorator() + + return wrap + def _validate_type(self, func: Union[Callable[..., Any], FunctionBuilder]) \ -> FunctionBuilder: @@ -2601,30 +2625,6 @@ class SettingsApi(DecoratorApi, ABC): """Interface to extend for using existing settings decorator in functions.""" - def function_name(self, name: str, - setting_extra_fields: Dict[str, Any] = {}, - ) -> Callable[..., Any]: - """Optional: Sets name of the :class:`Function` object. If not set, - it will default to the name of the method name. - - :param name: Name of the function. - :param setting_extra_fields: Keyword arguments for specifying - additional setting fields - :return: Decorator function. - """ - - @self._configure_function_builder - def wrap(fb): - def decorator(): - fb.add_setting(setting=FunctionName( - function_name=name, - **setting_extra_fields)) - return fb - - return decorator() - - return wrap - def retry(self, strategy: str, max_retry_count: str, diff --git a/tests/decorators/test_function_app.py b/tests/decorators/test_function_app.py index 1d72a813..2f687262 100644 --- a/tests/decorators/test_function_app.py +++ b/tests/decorators/test_function_app.py @@ -11,10 +11,10 @@ TIMER_TRIGGER from azure.functions.decorators.core import DataType, AuthLevel, \ BindingDirection, SCRIPT_FILE_NAME -from azure.functions.decorators.function_app import FunctionBuilder, \ - FunctionApp, Function, Blueprint, DecoratorApi, AsgiFunctionApp, \ - WsgiFunctionApp, HttpFunctionsAuthLevelMixin, FunctionRegister, \ - TriggerApi, ExternalHttpFunctionApp +from azure.functions.decorators.function_app import BindingApi, \ + FunctionBuilder, FunctionApp, Function, Blueprint, DecoratorApi, \ + AsgiFunctionApp, WsgiFunctionApp, HttpFunctionsAuthLevelMixin, \ + FunctionRegister, TriggerApi, ExternalHttpFunctionApp from azure.functions.decorators.http import HttpTrigger, HttpOutput, \ HttpMethod from azure.functions.decorators.retry_policy import RetryPolicy @@ -489,6 +489,31 @@ def hello(name: str): self.assertEqual(setting.get_settings_value("function_name"), "timer_function") + def test_legacy_blueprints_with_function_name(self): + class LegacyBluePrint(TriggerApi, BindingApi): + pass + + class DummyFunctionApp(FunctionRegister, TriggerApi): + pass + + app = DummyFunctionApp(auth_level=AuthLevel.ANONYMOUS) + blueprint = LegacyBluePrint() + + @blueprint.function_name("timer_function") + @blueprint.schedule(arg_name="name", schedule="10****") + def hello(name: str): + return name + + app.register_blueprint(blueprint) + + functions = app.get_functions() + self.assertEqual(len(functions), 1) + + setting = functions[0].get_setting("function_name") + + self.assertEqual(setting.get_settings_value("function_name"), + "timer_function") + def test_function_register_register_function_register_error(self): class DummyFunctionApp(FunctionRegister): pass