Skip to content

Commit 8fdec13

Browse files
author
Victoria Hall
committed
added warning log
1 parent 877ae6a commit 8fdec13

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

azure_functions_worker/functions.py

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import inspect
44
import operator
55
import pathlib
6+
import re
67
import typing
78
import uuid
89

@@ -11,6 +12,7 @@
1112
from ._thirdparty import typing_inspect
1213
from .constants import HTTP_TRIGGER
1314
from .protos import BindingInfo
15+
from .logging import logger
1416

1517

1618
class ParamTypeInfo(typing.NamedTuple):
@@ -130,6 +132,15 @@ def is_context_required(params, bound_params: dict,
130132
f'{ctx_anno!r}')
131133
return requires_context
132134

135+
@staticmethod
136+
def validate_arg_name(params: dict):
137+
pattern = r'(^\d|\b\w*__\w*|\w{129,})'
138+
for arg_name in params:
139+
if re.search(pattern, arg_name):
140+
logger.warning("Argument name %s is invalid. Please "
141+
"ensure it does not contain '__', start with a digit, "
142+
"or exceed 128 characters.", arg_name)
143+
133144
@staticmethod
134145
def validate_function_params(params: dict, bound_params: dict,
135146
annotations: dict, func_name: str):
@@ -389,6 +400,8 @@ def add_function(self, function_id: str,
389400
annotations,
390401
func_name)
391402

403+
self.validate_arg_name(params)
404+
392405
input_types, output_types, _ = self.validate_function_params(
393406
params, bound_params, annotations, func_name)
394407

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"scriptFile": "main.py",
3+
"bindings": [
4+
{
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"name": "__req"
8+
},
9+
{
10+
"type": "http",
11+
"direction": "out",
12+
"name": "$return"
13+
}
14+
]
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
import azure.functions as azf
4+
5+
6+
def main(__req: azf.HttpRequest):
7+
return azf.HttpResponse('<h1>Hello World™</h1>',
8+
mimetype='text/html')

tests/unittests/test_broken_functions.py

+21
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,24 @@ async def test_import_module_troubleshooting_url(self):
297297
self.assertRegex(
298298
r.response.result.exception.message,
299299
r'.*ModuleNotFoundError')
300+
301+
async def test_double_underscore_arg_name(self):
302+
async with testutils.start_mockhost(
303+
script_root=self.broken_funcs_dir) as host:
304+
await host.init_worker()
305+
func_id, r = await host.load_function('double_underscore_arg_name')
306+
307+
self.assertEqual(r.response.function_id, func_id)
308+
# Indexing does not fail here
309+
self.assertEqual(r.response.result.status,
310+
protos.StatusResult.Success)
311+
312+
message_present = False
313+
warning_log = ("Argument name __req is invalid. "
314+
"Please ensure it does not contain "
315+
"\'__\', start with a digit, or exceed 128 characters.")
316+
for log in r.logs:
317+
if warning_log in log.message:
318+
message_present = True
319+
break
320+
self.assertTrue(message_present)

0 commit comments

Comments
 (0)