Skip to content

Commit 8aa77f8

Browse files
DeusDataahundt
andcommitted
fix(extract): a decorator with arguments but no path is not a route
Distilled from #1245 by Andrew Hundt (a slice of d15071c), with a different rule than upstream chose. Python's unittest.mock is used as a decorator: @patch("subprocess.run") def test_runs(self, run): ... decorator_method_name maps a bare `patch` to the HTTP method PATCH, and try_route_from_decorator_call accepts a receiver-less decorator call. When extract_route_path_from_args then finds nothing path-shaped, the function falls back to "/". So every mock-decorated test function became a Route handler, PATCH "/", and every one of those was wrong. Upstream fixed it with a has_receiver heuristic. That still admits @mock.patch("os.getcwd") (it has a receiver) and would reject the bare framework decorators Litestar and BlackSheep use, @get("/x"). The rule here is about the arguments, not the callee: a decorator call that HAS arguments but none of them path-shaped is not a route. The "/" default survives only for a zero-argument call such as @app.route(), which is the one case where "/" is what the framework means. No test on main relied on the default for a call with non-path arguments; checked before changing it. RED before the fix: extract_python_mock_patch_is_not_route FAIL tests/test_extraction.c:4023: mocked->route_path is not NULL extract_python_bare_decorator_route_rules FAIL tests/test_extraction.c:4063: mocked->route_path is not NULL The second test also pins the two shapes that must keep working: bare @get("/health") still yields a GET route, and zero-argument @app.route() still yields "/" ANY -- those assertions passed before the fix and after. GREEN after: extraction 351 passed; extraction pipeline edge_types_probe route_canon cross_repo infrascan lang_contract 754 passed, with the CALLS-breadth contract at 54 languages / 0 failures. The single remaining red in both runs is extract_spill_round_trip_keeps_every_field refusing to spill onto a disk below its 10 GB floor -- this host, not this change. Co-authored-by: Andrew Hundt <ATHundt@gmail.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 92abefa commit 8aa77f8

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
@@ -1591,7 +1591,11 @@ static bool try_drf_action_decorator(CBMArena *a, TSNode dchild, const char *sou
15911591
}
15921592

15931593
// Try to extract a route from a single decorator call node.
1594-
// Returns true if a route method was found (even with fallback path "/").
1594+
// Returns true if a route method was found. The fallback path "/" applies only
1595+
// to a zero-argument call (`@app.route()`): a call that HAS arguments but none
1596+
// path-shaped is not a route -- unittest.mock's `@patch("subprocess.run")`
1597+
// shares its name with the HTTP verb and used to mint a PATCH "/" handler for
1598+
// every mocked test function (distilled from PR #1245).
15951599
static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char *source,
15961600
const char **out_path, const char **out_method) {
15971601
TSNode fn = ts_node_child_by_field_name(dchild, TS_FIELD("function"));
@@ -1616,6 +1620,9 @@ static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char
16161620
*out_method = method;
16171621
return true;
16181622
}
1623+
if (ts_node_named_child_count(args) > 0) {
1624+
return false;
1625+
}
16191626
}
16201627
*out_path = "/";
16211628
*out_method = method;

‎tests/test_extraction.c‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4001,6 +4001,77 @@ TEST(extract_java_method_annotations_issue382) {
40014001
PASS();
40024002
}
40034003

4004+
/* Distilled from PR #1245 (Andrew Hundt): unittest.mock's `@patch("x")` is a
4005+
* bare decorator whose name collides with the HTTP verb, and its only argument
4006+
* is a dotted target, not a path. The "/" fallback then minted a Route handler
4007+
* PATCH "/" for every mocked test function. Rule: a decorator call WITH
4008+
* arguments but no path-shaped one is not a route. */
4009+
TEST(extract_python_mock_patch_is_not_route) {
4010+
CBMFileResult *r = extract("from unittest.mock import patch\n\n"
4011+
"@patch(\"subprocess.run\")\n"
4012+
"def test_cmd(mock_run):\n"
4013+
" pass\n\n"
4014+
"@app.patch(\"/items/{id}\")\n"
4015+
"def update_item():\n"
4016+
" pass\n",
4017+
CBM_LANG_PYTHON, "t", "test_routes.py");
4018+
ASSERT_NOT_NULL(r);
4019+
ASSERT_FALSE(r->has_error);
4020+
4021+
const CBMDefinition *mocked = find_def_by_name(r, "test_cmd");
4022+
ASSERT_NOT_NULL(mocked);
4023+
ASSERT_NULL(mocked->route_path);
4024+
ASSERT_NULL(mocked->route_method);
4025+
4026+
const CBMDefinition *route = find_def_by_name(r, "update_item");
4027+
ASSERT_NOT_NULL(route);
4028+
ASSERT_STR_EQ(route->route_path, "/items/{id}");
4029+
ASSERT_STR_EQ(route->route_method, "PATCH");
4030+
4031+
cbm_free_result(r);
4032+
PASS();
4033+
}
4034+
4035+
/* Companion to the mock-patch case: the narrowing must not cost real routes.
4036+
* A receiver-less framework decorator with a path-shaped argument (Litestar /
4037+
* BlackSheep `@get("/x")`) stays a route, `@mock.patch("x")` (receiver, but
4038+
* no path) is not one, and a zero-argument `@app.route()` keeps the "/"
4039+
* default main emits today. */
4040+
TEST(extract_python_bare_decorator_route_rules) {
4041+
CBMFileResult *r = extract("from litestar import get\n"
4042+
"from unittest import mock\n\n"
4043+
"@get(\"/health\")\n"
4044+
"def health():\n"
4045+
" pass\n\n"
4046+
"@mock.patch(\"os.getcwd\")\n"
4047+
"def test_cwd(mock_cwd):\n"
4048+
" pass\n\n"
4049+
"@app.route()\n"
4050+
"def index():\n"
4051+
" pass\n",
4052+
CBM_LANG_PYTHON, "t", "routes.py");
4053+
ASSERT_NOT_NULL(r);
4054+
ASSERT_FALSE(r->has_error);
4055+
4056+
const CBMDefinition *health = find_def_by_name(r, "health");
4057+
ASSERT_NOT_NULL(health);
4058+
ASSERT_STR_EQ(health->route_path, "/health");
4059+
ASSERT_STR_EQ(health->route_method, "GET");
4060+
4061+
const CBMDefinition *mocked = find_def_by_name(r, "test_cwd");
4062+
ASSERT_NOT_NULL(mocked);
4063+
ASSERT_NULL(mocked->route_path);
4064+
ASSERT_NULL(mocked->route_method);
4065+
4066+
const CBMDefinition *index = find_def_by_name(r, "index");
4067+
ASSERT_NOT_NULL(index);
4068+
ASSERT_STR_EQ(index->route_path, "/");
4069+
ASSERT_STR_EQ(index->route_method, "ANY");
4070+
4071+
cbm_free_result(r);
4072+
PASS();
4073+
}
4074+
40044075
/* ── ArkTS (HarmonyOS .ets) ─────────────────────────────────────── */
40054076

40064077
TEST(arkts_component_struct) {
@@ -7926,6 +7997,8 @@ SUITE(extraction) {
79267997
RUN_TEST(js_index_module_qn_not_collide_with_folder);
79277998
RUN_TEST(python_regular_module_qn_unchanged);
79287999
RUN_TEST(extract_java_method_annotations_issue382);
8000+
RUN_TEST(extract_python_mock_patch_is_not_route);
8001+
RUN_TEST(extract_python_bare_decorator_route_rules);
79298002
RUN_TEST(arkts_component_struct);
79308003
RUN_TEST(arkts_exported_struct_decorators);
79318004
RUN_TEST(arkts_member_decorators);

0 commit comments

Comments
 (0)