Skip to content

Commit f42c066

Browse files
authored
Merge pull request DeusData#746 from DeusData/distill/647-drf-viewset-actions
feat(extract): Route+HANDLES for Django REST Framework @action methods
2 parents 39e3c07 + 024c0c5 commit f42c066

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,124 @@ static const char *extract_route_path_from_args(CBMArena *a, TSNode args, const
13531353
return NULL;
13541354
}
13551355

1356+
// Find a keyword argument by name in an argument_list node and return its value child.
1357+
static TSNode find_drf_kwarg_in_args(CBMArena *a, TSNode args, const char *kwarg_name,
1358+
const char *source) {
1359+
uint32_t nc = ts_node_named_child_count(args);
1360+
for (uint32_t ai = 0; ai < nc; ai++) {
1361+
TSNode child = ts_node_named_child(args, ai);
1362+
if (strcmp(ts_node_type(child), "keyword_argument") != 0)
1363+
continue;
1364+
TSNode name_node = ts_node_child_by_field_name(child, TS_FIELD("name"));
1365+
if (ts_node_is_null(name_node))
1366+
continue;
1367+
char *name = cbm_node_text(a, name_node, source);
1368+
if (name && strcmp(name, kwarg_name) == 0) {
1369+
return ts_node_child_by_field_name(child, TS_FIELD("value"));
1370+
}
1371+
}
1372+
TSNode null_node = {0};
1373+
return null_node;
1374+
}
1375+
1376+
// Try to extract a route from a Django REST Framework @action decorator on a
1377+
// ViewSet method:
1378+
// @action(detail=True, methods=["post"], url_path="approve")
1379+
// Sets *out_path/*out_method so the downstream Route+HANDLES pipeline (Phase 2a
1380+
// ensure_one_decorator_route) emits the handler->Route edge in the normal
1381+
// direction. Falls back to the method name for url_path and "GET" for methods.
1382+
// Known limitation: multi-method actions (methods=["get","post"]) capture only
1383+
// the first method -> a single Route rather than one per method.
1384+
static bool try_drf_action_decorator(CBMArena *a, TSNode dchild, const char *source,
1385+
TSNode func_node, const char **out_path,
1386+
const char **out_method) {
1387+
TSNode fn = ts_node_child_by_field_name(dchild, TS_FIELD("function"));
1388+
if (ts_node_is_null(fn)) {
1389+
fn = ts_node_named_child(dchild, 0);
1390+
}
1391+
if (ts_node_is_null(fn)) {
1392+
return false;
1393+
}
1394+
const char *fn_type = ts_node_type(fn);
1395+
if (strcmp(fn_type, "identifier") != 0) {
1396+
return false;
1397+
}
1398+
char *fn_text = cbm_node_text(a, fn, source);
1399+
if (!fn_text || strcmp(fn_text, "action") != 0) {
1400+
return false;
1401+
}
1402+
TSNode args = find_decorator_args(dchild);
1403+
if (ts_node_is_null(args)) {
1404+
return false;
1405+
}
1406+
const char *method = NULL;
1407+
TSNode methods_val = find_drf_kwarg_in_args(a, args, "methods", source);
1408+
if (!ts_node_is_null(methods_val) && strcmp(ts_node_type(methods_val), "list") == 0) {
1409+
uint32_t mc = ts_node_named_child_count(methods_val);
1410+
for (uint32_t mi = 0; mi < mc && !method; mi++) {
1411+
TSNode item = ts_node_named_child(methods_val, mi);
1412+
if (strcmp(ts_node_type(item), "string") != 0)
1413+
continue;
1414+
char *text = cbm_node_text(a, item, source);
1415+
if (!text)
1416+
continue;
1417+
int tlen = (int)strlen(text);
1418+
if (tlen < PAIR_CHARS || (text[0] != '"' && text[0] != '\''))
1419+
continue;
1420+
char inner[CBM_SZ_16];
1421+
int ilen = tlen - PAIR_CHARS;
1422+
if (ilen <= 0 || ilen >= (int)sizeof(inner))
1423+
continue;
1424+
memcpy(inner, text + SKIP_CHAR, (size_t)ilen);
1425+
inner[ilen] = '\0';
1426+
for (int ci = 0; inner[ci]; ci++) {
1427+
if (inner[ci] >= 'a' && inner[ci] <= 'z')
1428+
inner[ci] -= 32;
1429+
}
1430+
method = cbm_arena_strdup(a, inner);
1431+
}
1432+
}
1433+
if (!method) {
1434+
method = "GET";
1435+
}
1436+
const char *segment = NULL;
1437+
TSNode url_path_val = find_drf_kwarg_in_args(a, args, "url_path", source);
1438+
if (!ts_node_is_null(url_path_val) && strcmp(ts_node_type(url_path_val), "string") == 0) {
1439+
char *text = cbm_node_text(a, url_path_val, source);
1440+
if (text) {
1441+
int tlen = (int)strlen(text);
1442+
if (tlen >= PAIR_CHARS && (text[0] == '"' || text[0] == '\'')) {
1443+
segment = cbm_arena_strndup(a, text + SKIP_CHAR, (size_t)(tlen - PAIR_CHARS));
1444+
}
1445+
}
1446+
}
1447+
if (!segment) {
1448+
TSNode name_node = func_name_node(func_node);
1449+
if (!ts_node_is_null(name_node)) {
1450+
segment = cbm_node_text(a, name_node, source);
1451+
}
1452+
}
1453+
if (!segment) {
1454+
return false;
1455+
}
1456+
// Extract detail kwarg (default True in DRF)
1457+
bool detail = true;
1458+
TSNode detail_val = find_drf_kwarg_in_args(a, args, "detail", source);
1459+
if (!ts_node_is_null(detail_val)) {
1460+
const char *dv = ts_node_type(detail_val);
1461+
if (strcmp(dv, "false") == 0) {
1462+
detail = false;
1463+
}
1464+
}
1465+
if (detail) {
1466+
*out_path = cbm_arena_sprintf(a, "/{pk}/%s", segment);
1467+
} else {
1468+
*out_path = cbm_arena_sprintf(a, "/%s", segment);
1469+
}
1470+
*out_method = method;
1471+
return true;
1472+
}
1473+
13561474
// Try to extract a route from a single decorator call node.
13571475
// Returns true if a route method was found (even with fallback path "/").
13581476
static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char *source,
@@ -1516,6 +1634,9 @@ static void extract_route_from_decorators(CBMArena *a, TSNode func_node, const c
15161634
if (try_route_from_decorator_call(a, dchild, source, out_path, out_method)) {
15171635
return;
15181636
}
1637+
if (try_drf_action_decorator(a, dchild, source, func_node, out_path, out_method)) {
1638+
return;
1639+
}
15191640
}
15201641
/* JVM/C# annotation-form route mapping (Spring @GetMapping etc.) — the
15211642
* prev-sibling itself may be the annotation node. */

‎tests/test_edge_types_probe.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,23 @@ TEST(handles_fastapi_python) {
198198
PASS();
199199
}
200200

201+
/* DRF @action (Python) — issue #603. Before the try_drf_action_decorator
202+
* extractor, an @action-decorated ViewSet method carried no route_path, so
203+
* Phase 2a emitted 0 Route/HANDLES edges (this asserts >=1 → RED without the
204+
* fix, GREEN with it). Direction-agnostic count via cbm_store_count_edges_by_type. */
205+
TEST(handles_drf_action_python) {
206+
static const EtFile f[] = {
207+
{"viewsets.py",
208+
"from rest_framework.decorators import action\n"
209+
"from rest_framework.viewsets import ViewSet\n\n"
210+
"class CustomerTaskViewSet(ViewSet):\n"
211+
" @action(detail=True, methods=[\"post\"])\n"
212+
" def approve_draft_with_charge(self, request, pk=None):\n"
213+
" pass\n"}};
214+
ASSERT_TRUE(et_edge_present(f, 1, "HANDLES", 1));
215+
PASS();
216+
}
217+
201218
/* Express (JS/TS) — route registration must resolve to a callee QN containing
202219
* the "express" library substring AND pass the handler as an identifier (not an
203220
* inline-object method, which is never registered as a resolvable node). We use
@@ -1371,6 +1388,7 @@ SUITE(edge_types_probe) {
13711388
/* HANDLES — route→handler across web frameworks (8 frameworks) */
13721389
RUN_TEST(handles_flask_python);
13731390
RUN_TEST(handles_fastapi_python);
1391+
RUN_TEST(handles_drf_action_python);
13741392
RUN_TEST(handles_express_ts);
13751393
RUN_TEST(handles_fastify_js);
13761394
RUN_TEST(handles_gin_go);

0 commit comments

Comments
 (0)