Skip to content

Commit de4a88f

Browse files
DeusDataahundt
andcommitted
fix(routes): register FastAPI and Starlette WebSocket decorators
Distilled from #1245 by Andrew Hundt (a slice of 1ac3c13). The route-registration suffix table knew .add_api_websocket_route but not the decorator forms FastAPI and Starlette actually use: @router.websocket("/ws") @app.websocket_route("/events") so a WebSocket endpoint produced no Route node on either resolver path and was invisible to the graph. Two entries join the table, both recorded as ANY like the existing WebSocket entry -- a socket upgrade is not an HTTP verb. Both are needed: matching is a pure suffix compare and .route does not match ...websocket_route, whose tail is _route. Upstream also changed pass_calls.c, pass_parallel.c and pipeline_internal.h. None of that is taken: main already carries the #952 sequential unresolved-callee route fallback its July base lacked, so the table entries are the only missing piece. RED before the fix, each form recorded on its own (the harness stops at the first failing assert, so the two were swapped and re-run): pipeline_python_websocket_decorators_register_routes FAIL tests/test_pipeline.c:6163: count_nodes_named(s, project, "/ws") (0) not >= 1 (1) FAIL tests/test_pipeline.c:6163: count_nodes_named(s, project, "/events") (0) not >= 1 (1) The @app.get("/health") control above those lines passed before and after. GREEN after: pipeline 282 passed; pipeline parallel extraction edge_types_probe route_canon infrascan 779 passed, 0 failed. The test drives the sequential resolver (one file). The parallel resolver reads the same table through cbm_service_pattern_route_method and its suite is green; a CBM_WORKERS=4 twin of this test is a small follow-up. Co-authored-by: Andrew Hundt <ATHundt@gmail.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 92abefa commit de4a88f

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎internal/cbm/service_patterns.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,11 @@ static const method_suffix_t route_reg_suffixes[] = {
514514
{".add_route", "ANY"},
515515
{".add_api_route", "ANY"},
516516
{".add_api_websocket_route", "ANY"},
517+
/* FastAPI/Starlette WebSocket decorators (`@router.websocket('/ws')`,
518+
* `@app.websocket_route('/events')`): not an HTTP verb, so ANY — mirrors
519+
* .add_api_websocket_route above (#1245). */
520+
{".websocket", "ANY"},
521+
{".websocket_route", "ANY"},
517522
{NULL, NULL},
518523
};
519524

‎tests/test_pipeline.c‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6119,6 +6119,56 @@ TEST(pipeline_tsjs_receiver_parallel_keeps_service_edges) {
61196119
PASS();
61206120
}
61216121

6122+
/* FastAPI/Starlette WebSocket route registration (distilled from #1245). The
6123+
* route-registration suffix table knew `.add_api_websocket_route` but not the
6124+
* decorator forms `.websocket` / `.websocket_route`, so a WebSocket endpoint
6125+
* registered via `@router.websocket('/ws')` produced no Route node at all —
6126+
* the endpoint was invisible to the graph. `@app.get('/health')` is the
6127+
* positive control (already a route suffix). Fewer than 50 files exercises
6128+
* pass_calls.c; the same suffix table drives the parallel resolver. */
6129+
TEST(pipeline_python_websocket_decorators_register_routes) {
6130+
char tmp[256];
6131+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_py_ws_routes_XXXXXX");
6132+
if (!cbm_mkdtemp(tmp)) {
6133+
FAIL("tmpdir");
6134+
}
6135+
6136+
write_temp_file(tmp, "app.py",
6137+
"from fastapi import APIRouter, FastAPI, WebSocket\n\n"
6138+
"app = FastAPI()\n"
6139+
"router = APIRouter()\n\n"
6140+
"@app.get('/health')\n"
6141+
"def health():\n"
6142+
" return {'ok': True}\n\n"
6143+
"@router.websocket('/ws')\n"
6144+
"async def ws_endpoint(websocket: WebSocket):\n"
6145+
" await websocket.accept()\n\n"
6146+
"@app.websocket_route('/events')\n"
6147+
"async def events_endpoint(websocket: WebSocket):\n"
6148+
" await websocket.accept()\n");
6149+
6150+
char db_path[512];
6151+
snprintf(db_path, sizeof(db_path), "%s/py_ws.db", tmp);
6152+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
6153+
ASSERT_NOT_NULL(p);
6154+
ASSERT_EQ(cbm_pipeline_run(p), 0);
6155+
const char *project = cbm_pipeline_project_name(p);
6156+
6157+
cbm_store_t *s = cbm_store_open_path(db_path);
6158+
ASSERT_NOT_NULL(s);
6159+
6160+
/* Positive control: the HTTP decorator mints its Route node. */
6161+
ASSERT_GTE(count_nodes_named(s, project, "/health"), 1);
6162+
/* The two WebSocket decorator forms must mint Route nodes too. */
6163+
ASSERT_GTE(count_nodes_named(s, project, "/ws"), 1);
6164+
ASSERT_GTE(count_nodes_named(s, project, "/events"), 1);
6165+
6166+
cbm_store_close(s);
6167+
cbm_pipeline_free(p);
6168+
th_rmtree(tmp);
6169+
PASS();
6170+
}
6171+
61226172
/* Python bare-call local-binding suppression, sequential path. The bare-call
61236173
* counterpart of the receiver guard above: `run` is a PARAMETER, so `run()`
61246174
* cannot be the module-level `run` and must not bind SatoriLive.run.
@@ -14846,6 +14896,7 @@ SUITE(pipeline) {
1484614896
RUN_TEST(pipeline_go_bare_ref_never_binds_field);
1484714897
RUN_TEST(pipeline_go_bare_ref_never_binds_field_parallel);
1484814898
RUN_TEST(pipeline_tsjs_receiver_parallel_keeps_service_edges);
14899+
RUN_TEST(pipeline_python_websocket_decorators_register_routes);
1484914900
RUN_TEST(pipeline_python_receiver_parallel_suppresses_weak_method_edges);
1485014901
RUN_TEST(pipeline_python_bare_local_binding_suppresses_weak_edge);
1485114902
RUN_TEST(pipeline_python_bare_local_binding_parallel_suppresses_weak_edge);

0 commit comments

Comments
 (0)