Skip to content

Commit 97ebf18

Browse files
authored
Merge pull request DeusData#2254 from DeusData/distill/1245-patch-decorator-route
fix(extract): a decorator with arguments but no path is not a route
2 parents e71f23e + a0191b5 commit 97ebf18

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2987,7 +2987,11 @@ static bool try_drf_action_decorator(CBMArena *a, TSNode dchild, const char *sou
29872987
}
29882988

29892989
// Try to extract a route from a single decorator call node.
2990-
// Returns true if a route method was found (even with fallback path "/").
2990+
// Returns true if a route method was found. The fallback path "/" applies only
2991+
// to a zero-argument call (`@app.route()`): a call that HAS arguments but none
2992+
// path-shaped is not a route -- unittest.mock's `@patch("subprocess.run")`
2993+
// shares its name with the HTTP verb and used to mint a PATCH "/" handler for
2994+
// every mocked test function (distilled from PR #1245).
29912995
static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char *source,
29922996
const char **out_path, const char **out_method) {
29932997
TSNode fn = ts_node_child_by_field_name(dchild, TS_FIELD("function"));
@@ -3012,6 +3016,9 @@ static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char
30123016
*out_method = method;
30133017
return true;
30143018
}
3019+
if (ts_node_named_child_count(args) > 0) {
3020+
return false;
3021+
}
30153022
}
30163023
*out_path = "/";
30173024
*out_method = method;

‎tests/test_extraction.c‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4715,6 +4715,77 @@ TEST(extract_java_method_annotations_issue382) {
47154715
PASS();
47164716
}
47174717

4718+
/* Distilled from PR #1245 (Andrew Hundt): unittest.mock's `@patch("x")` is a
4719+
* bare decorator whose name collides with the HTTP verb, and its only argument
4720+
* is a dotted target, not a path. The "/" fallback then minted a Route handler
4721+
* PATCH "/" for every mocked test function. Rule: a decorator call WITH
4722+
* arguments but no path-shaped one is not a route. */
4723+
TEST(extract_python_mock_patch_is_not_route) {
4724+
CBMFileResult *r = extract("from unittest.mock import patch\n\n"
4725+
"@patch(\"subprocess.run\")\n"
4726+
"def test_cmd(mock_run):\n"
4727+
" pass\n\n"
4728+
"@app.patch(\"/items/{id}\")\n"
4729+
"def update_item():\n"
4730+
" pass\n",
4731+
CBM_LANG_PYTHON, "t", "test_routes.py");
4732+
ASSERT_NOT_NULL(r);
4733+
ASSERT_FALSE(r->has_error);
4734+
4735+
const CBMDefinition *mocked = find_def_by_name(r, "test_cmd");
4736+
ASSERT_NOT_NULL(mocked);
4737+
ASSERT_NULL(mocked->route_path);
4738+
ASSERT_NULL(mocked->route_method);
4739+
4740+
const CBMDefinition *route = find_def_by_name(r, "update_item");
4741+
ASSERT_NOT_NULL(route);
4742+
ASSERT_STR_EQ(route->route_path, "/items/{id}");
4743+
ASSERT_STR_EQ(route->route_method, "PATCH");
4744+
4745+
cbm_free_result(r);
4746+
PASS();
4747+
}
4748+
4749+
/* Companion to the mock-patch case: the narrowing must not cost real routes.
4750+
* A receiver-less framework decorator with a path-shaped argument (Litestar /
4751+
* BlackSheep `@get("/x")`) stays a route, `@mock.patch("x")` (receiver, but
4752+
* no path) is not one, and a zero-argument `@app.route()` keeps the "/"
4753+
* default main emits today. */
4754+
TEST(extract_python_bare_decorator_route_rules) {
4755+
CBMFileResult *r = extract("from litestar import get\n"
4756+
"from unittest import mock\n\n"
4757+
"@get(\"/health\")\n"
4758+
"def health():\n"
4759+
" pass\n\n"
4760+
"@mock.patch(\"os.getcwd\")\n"
4761+
"def test_cwd(mock_cwd):\n"
4762+
" pass\n\n"
4763+
"@app.route()\n"
4764+
"def index():\n"
4765+
" pass\n",
4766+
CBM_LANG_PYTHON, "t", "routes.py");
4767+
ASSERT_NOT_NULL(r);
4768+
ASSERT_FALSE(r->has_error);
4769+
4770+
const CBMDefinition *health = find_def_by_name(r, "health");
4771+
ASSERT_NOT_NULL(health);
4772+
ASSERT_STR_EQ(health->route_path, "/health");
4773+
ASSERT_STR_EQ(health->route_method, "GET");
4774+
4775+
const CBMDefinition *mocked = find_def_by_name(r, "test_cwd");
4776+
ASSERT_NOT_NULL(mocked);
4777+
ASSERT_NULL(mocked->route_path);
4778+
ASSERT_NULL(mocked->route_method);
4779+
4780+
const CBMDefinition *index = find_def_by_name(r, "index");
4781+
ASSERT_NOT_NULL(index);
4782+
ASSERT_STR_EQ(index->route_path, "/");
4783+
ASSERT_STR_EQ(index->route_method, "ANY");
4784+
4785+
cbm_free_result(r);
4786+
PASS();
4787+
}
4788+
47184789
/* ── ArkTS (HarmonyOS .ets) ─────────────────────────────────────── */
47194790

47204791
TEST(arkts_component_struct) {
@@ -9829,6 +9900,8 @@ SUITE(extraction) {
98299900
RUN_TEST(js_index_module_qn_not_collide_with_folder);
98309901
RUN_TEST(python_regular_module_qn_unchanged);
98319902
RUN_TEST(extract_java_method_annotations_issue382);
9903+
RUN_TEST(extract_python_mock_patch_is_not_route);
9904+
RUN_TEST(extract_python_bare_decorator_route_rules);
98329905
RUN_TEST(arkts_component_struct);
98339906
RUN_TEST(arkts_exported_struct_decorators);
98349907
RUN_TEST(arkts_member_decorators);

0 commit comments

Comments
 (0)