Skip to content

Commit 3ef7d8c

Browse files
committed
fix(extract): read the url of a JS/TS request-config object (DeusData#2235)
A generated API client (Orval and similar) passes its request as a config object to a local fetch wrapper: http<Customer>({url: `/api/customers/${id}`, method: 'GET', signal}) The template literal itself already canonicalizes to "/api/customers/{}" (DeusData#1006). What was lost is the object wrapper: the call's arguments only carried a value for bare strings, template literals, constants and URL builders, so the object argument had no URL. The arg-url heuristic therefore minted no HTTP_CALLS edge, and cross-repo matching had nothing to join against the server's GET /api/customers/{id} Route. The same held for HTTP clients called with a config object (`axios({url})`): no URL, no edge. The extractor now reads the `url` property of an object argument in JS/TS/TSX/ArkTS -- both as that argument's value (for the arg-url heuristic) and as the call's URL (for HTTP-client classification). Only the `url` key counts. Route registrations (Fastify `route({url, handler})`) and axios `getUri(config)`, which formats a URL and sends nothing, are excluded. orval-labs/orval @ 08d7fcf1 (parallel resolver): HTTP_CALLS 1383 -> 1533 (+150 arg_url edges from the generated mutators such as customInstance, responseType and customClient; 0 lost), Route 302 -> 308, CALLS unchanged (19190). Without the getUri exclusion there would have been 546 URL-builder edges. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 64c23fa commit 3ef7d8c

3 files changed

Lines changed: 190 additions & 2 deletions

File tree

‎internal/cbm/extract_calls.c‎

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,56 @@ static void process_keyword_arg(CBMExtractCtx *ctx, TSNode arg_node, CBMCallArg
20612061
}
20622062
}
20632063

2064+
static const char *extract_string_value(CBMExtractCtx *ctx, TSNode val_node);
2065+
2066+
/* A JS/TS request-config object -- `http({url: `/users/${id}`, method: 'GET'})`,
2067+
* `axios({url: '/orders'})`, the shape Orval and similar generators emit -- names
2068+
* its request URL in the `url` property. Return that URL (template literals
2069+
* flattened to the canonical "{}" form, like a bare template argument), or NULL.
2070+
* Only the `url` key counts: `path`/`endpoint` in an options object name other
2071+
* things too often. Route registrations are excluded: an object there is the
2072+
* route definition (Fastify `route({url, handler})`), not a client request, and
2073+
* its handler lives inside the object where the route pass cannot read it. So is
2074+
* axios `getUri(config)`: it takes the same config but only formats the URL and
2075+
* sends nothing (Orval's `get<Op>Url` helpers). (#2235) */
2076+
static bool config_object_callee_eligible(const CBMExtractCtx *ctx, const char *callee_name) {
2077+
if (ctx->language != CBM_LANG_JAVASCRIPT && ctx->language != CBM_LANG_TYPESCRIPT &&
2078+
ctx->language != CBM_LANG_TSX && ctx->language != CBM_LANG_ARKTS) {
2079+
return false;
2080+
}
2081+
if (!callee_name || cbm_service_pattern_route_method(callee_name) != NULL) {
2082+
return false;
2083+
}
2084+
static const char get_uri[] = "getUri";
2085+
size_t clen = strlen(callee_name);
2086+
size_t glen = sizeof(get_uri) - 1;
2087+
return !(clen >= glen && strcmp(callee_name + clen - glen, get_uri) == 0);
2088+
}
2089+
2090+
static const char *js_config_object_url(CBMExtractCtx *ctx, TSNode obj, const char *callee_name) {
2091+
if (strcmp(ts_node_type(obj), "object") != 0 ||
2092+
!config_object_callee_eligible(ctx, callee_name)) {
2093+
return NULL;
2094+
}
2095+
uint32_t n = ts_node_named_child_count(obj);
2096+
for (uint32_t i = 0; i < n; i++) {
2097+
TSNode pair = ts_node_named_child(obj, i);
2098+
if (strcmp(ts_node_type(pair), "pair") != 0) {
2099+
continue;
2100+
}
2101+
TSNode key_n = ts_node_child_by_field_name(pair, TS_FIELD("key"));
2102+
TSNode val_n = ts_node_child_by_field_name(pair, TS_FIELD("value"));
2103+
if (ts_node_is_null(key_n) || ts_node_is_null(val_n)) {
2104+
continue;
2105+
}
2106+
const char *key = strip_quotes(ctx->arena, cbm_node_text(ctx->arena, key_n, ctx->source));
2107+
if (key && strcmp(key, "url") == 0) {
2108+
return extract_string_value(ctx, val_n);
2109+
}
2110+
}
2111+
return NULL;
2112+
}
2113+
20642114
/* Extract all arguments from a call expression into call->args[]. */
20652115
static void extract_call_args(CBMExtractCtx *ctx, TSNode args, CBMCall *call) {
20662116
uint32_t argc = ts_node_named_child_count(args);
@@ -2103,6 +2153,10 @@ static void extract_call_args(CBMExtractCtx *ctx, TSNode args, CBMCall *call) {
21032153
ca->value = cbm_template_string_text(ctx->arena, arg_node, ctx->source);
21042154
} else if (strcmp(ak, "identifier") == 0 && ca->expr) {
21052155
ca->value = lookup_string_constant(ctx, ca->expr);
2156+
} else if (strcmp(ak, "object") == 0) {
2157+
/* Request-config object: its `url` is what the arg-url
2158+
* heuristic reads for a local fetch wrapper (#2235). */
2159+
ca->value = js_config_object_url(ctx, arg_node, call->callee_name);
21062160
} else if (strcmp(ak, "call_expression") == 0) {
21072161
/* URL-builder helper call (issue #1009): resolve
21082162
* client(buildPath(id)) through the per-file builder map. */
@@ -2367,7 +2421,8 @@ static const char *extract_positional_url(CBMExtractCtx *ctx, TSNode arg, const
23672421
}
23682422

23692423
// Extract URL/topic from keyword or positional args.
2370-
static const char *extract_url_or_topic_arg(CBMExtractCtx *ctx, TSNode args) {
2424+
static const char *extract_url_or_topic_arg(CBMExtractCtx *ctx, TSNode args,
2425+
const char *callee_name) {
23712426
uint32_t nc = ts_node_named_child_count(args);
23722427
for (uint32_t ai = 0; ai < nc; ai++) {
23732428
TSNode arg = ts_node_named_child(args, ai);
@@ -2395,6 +2450,15 @@ static const char *extract_url_or_topic_arg(CBMExtractCtx *ctx, TSNode args) {
23952450
continue;
23962451
}
23972452

2453+
/* JS/TS request-config object: `axios({url: '/orders'})` (#2235). */
2454+
if (strcmp(ak, "object") == 0) {
2455+
const char *val = js_config_object_url(ctx, arg, callee_name);
2456+
if (val) {
2457+
return val;
2458+
}
2459+
continue;
2460+
}
2461+
23982462
/* Cloud SDK dispatch via input struct: the queue/topic target is a field
23992463
* of a composite literal (Go `&sqs.SendMessageInput{QueueUrl: ...}`), not
24002464
* a bare string arg. Recover it so the async edge forms. */
@@ -3832,7 +3896,7 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
38323896
args = swift_call_args(node);
38333897
}
38343898
if (!ts_node_is_null(args)) {
3835-
call.first_string_arg = extract_url_or_topic_arg(ctx, args);
3899+
call.first_string_arg = extract_url_or_topic_arg(ctx, args, call.callee_name);
38363900
/* #952: routes registered inside Laravel `prefix()->group()`
38373901
* closures must carry the composed path — the resolve passes
38383902
* only see the flat CBMCall, so the enclosing chain can only

‎tests/test_extraction.c‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4867,6 +4867,62 @@ TEST(extract_ts_template_string_url_issue1006) {
48674867
PASS();
48684868
}
48694869

4870+
/* Issue #2235: a request-config object argument carries the URL in its `url`
4871+
* property (Orval/axios style: `http({url: `/x/${id}`, method: 'GET'})`).
4872+
* The property's template literal flattens to the canonical "{}" form both on
4873+
* the argument (read by the arg-url heuristic for local helpers) and as the
4874+
* call's URL (read by HTTP-client classification, e.g. `axios({url})`). Other
4875+
* keys never stand in for the URL, and a route registration keeps its own
4876+
* (object-free) handling. */
4877+
TEST(extract_ts_config_object_url_issue2235) {
4878+
CBMFileResult *r =
4879+
extract("export const get1 = (id: string) => {\n"
4880+
" return http<Customer>({url: `/api/customers/${id}`, method: 'GET'});\n"
4881+
"};\n"
4882+
"export const load = () => axios({method: 'POST', 'url': '/api/orders'});\n"
4883+
"export const other = () => http({path: '/api/ignored/x'});\n"
4884+
"export const reg = () => fastify.route({url: '/srv/x', handler: h});\n"
4885+
"export const u = () => axiosInstance.getUri({url: `/pets`});\n",
4886+
CBM_LANG_TYPESCRIPT, "t", "client.ts");
4887+
ASSERT_NOT_NULL(r);
4888+
ASSERT_FALSE(r->has_error);
4889+
const CBMCall *c = NULL;
4890+
const CBMCall *o = NULL;
4891+
for (int i = 0; i < r->calls.count; i++) {
4892+
const CBMCall *k = &r->calls.items[i];
4893+
if (k->callee_name && strcmp(k->callee_name, "http") == 0 && k->arg_count > 0) {
4894+
if (k->args[0].expr && strstr(k->args[0].expr, "customers")) {
4895+
c = k;
4896+
} else {
4897+
o = k;
4898+
}
4899+
}
4900+
}
4901+
ASSERT_NOT_NULL(c);
4902+
ASSERT_NOT_NULL(c->args[0].value);
4903+
ASSERT_STR_EQ(c->args[0].value, "/api/customers/{}");
4904+
ASSERT_NOT_NULL(c->first_string_arg);
4905+
ASSERT_STR_EQ(c->first_string_arg, "/api/customers/{}");
4906+
const CBMCall *a = find_call_by_callee(r, "axios");
4907+
ASSERT_NOT_NULL(a);
4908+
ASSERT_NOT_NULL(a->first_string_arg);
4909+
ASSERT_STR_EQ(a->first_string_arg, "/api/orders");
4910+
ASSERT_NOT_NULL(o);
4911+
ASSERT_NULL(o->args[0].value);
4912+
ASSERT_NULL(o->first_string_arg);
4913+
const CBMCall *g = find_call_by_callee(r, "fastify.route");
4914+
ASSERT_NOT_NULL(g);
4915+
ASSERT_NULL(g->first_string_arg);
4916+
ASSERT(g->arg_count == 0 || g->args[0].value == NULL);
4917+
/* getUri only formats the URL; it sends no request. */
4918+
const CBMCall *gu = find_call_by_callee(r, "axiosInstance.getUri");
4919+
ASSERT_NOT_NULL(gu);
4920+
ASSERT_NULL(gu->first_string_arg);
4921+
ASSERT(gu->arg_count == 0 || gu->args[0].value == NULL);
4922+
cbm_free_result(r);
4923+
PASS();
4924+
}
4925+
48704926
/* Issue #1249: a mux route built as `configVar + "/literal"` (Go's idiomatic
48714927
* configurable-base-path pattern) must index the literal suffix, both for a
48724928
* route registration and for an outbound URL built the same way. A real BFF
@@ -8605,6 +8661,7 @@ SUITE(extraction) {
86058661
RUN_TEST(extract_razor_page_directive_routes_cshtml_view);
86068662
RUN_TEST(extract_razor_layout_without_page_has_no_route);
86078663
RUN_TEST(extract_ts_template_string_url_issue1006);
8664+
RUN_TEST(extract_ts_config_object_url_issue2235);
86088665
RUN_TEST(extract_go_binary_concat_url_issue1249);
86098666
RUN_TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249);
86108667
RUN_TEST(extract_ts_url_builder_issue1009);

‎tests/test_pipeline.c‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6726,6 +6726,72 @@ TEST(pipeline_arg_url_rejects_non_http_slash_arguments) {
67266726
PASS();
67276727
}
67286728

6729+
/* #2235: an Orval-style generated client passes its URL inside a request
6730+
* config object to a LOCAL helper -- `http({url: `/api/customers/${id}`,
6731+
* method: 'GET'})`. The template literal itself already canonicalizes to
6732+
* "/api/customers/{}"; what was lost is the object wrapper: the arg-url
6733+
* heuristic only read bare string/template arguments, so the call produced
6734+
* no HTTP_CALLS edge and cross-repo matching had nothing to join. Parallel
6735+
* resolver (> 50 files), where the arg-url heuristic lives. A config object
6736+
* without a `url` key must not mint a route. */
6737+
TEST(pipeline_ts_config_object_url_http_calls_issue2235) {
6738+
char tmp[256];
6739+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_i2235_XXXXXX");
6740+
if (!cbm_mkdtemp(tmp)) {
6741+
FAIL("tmpdir");
6742+
}
6743+
write_temp_file(tmp, "src/http.ts",
6744+
"export const http = <T,>(config: { url: string; method: string }): "
6745+
"Promise<T> => {\n"
6746+
" return fetch(config.url, { method: config.method }).then((r) => r.json());\n"
6747+
"};\n");
6748+
write_temp_file(
6749+
tmp, "src/gen/customer-controller.ts",
6750+
"import { http } from '../http';\n"
6751+
"export const get1 = (id: string, signal?: AbortSignal) => {\n"
6752+
" return http<unknown>({url: `/api/customers/${id}`, method: 'GET', signal});\n"
6753+
"};\n"
6754+
"export const noUrl = () => {\n"
6755+
" return http<unknown>({path: '/api/ignored/x', method: 'GET'});\n"
6756+
"};\n");
6757+
for (int i = 0; i < 52; i++) {
6758+
char name[64];
6759+
char body[128];
6760+
snprintf(name, sizeof(name), "src/filler%d.ts", i);
6761+
snprintf(body, sizeof(body), "export function filler%d(): number { return %d; }\n", i, i);
6762+
write_temp_file(tmp, name, body);
6763+
}
6764+
6765+
char *old_workers = getenv("CBM_WORKERS");
6766+
char *saved = old_workers ? strdup(old_workers) : NULL;
6767+
cbm_setenv("CBM_WORKERS", "4", 1);
6768+
6769+
char db_path[512];
6770+
snprintf(db_path, sizeof(db_path), "%s/i2235.db", tmp);
6771+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
6772+
ASSERT_NOT_NULL(p);
6773+
ASSERT_EQ(cbm_pipeline_run(p), 0);
6774+
const char *project = cbm_pipeline_project_name(p);
6775+
cbm_store_t *s = cbm_store_open_path(db_path);
6776+
ASSERT_NOT_NULL(s);
6777+
6778+
/* RED before the fix: the url inside the config object was never read. */
6779+
ASSERT_EQ(named_edge_count(s, project, "HTTP_CALLS", "get1", "/api/customers/{}"), 1);
6780+
/* Only the `url` key names the request URL. */
6781+
ASSERT_EQ(count_nodes_named(s, project, "/api/ignored/x"), 0);
6782+
6783+
cbm_store_close(s);
6784+
cbm_pipeline_free(p);
6785+
if (saved) {
6786+
cbm_setenv("CBM_WORKERS", saved, 1);
6787+
free(saved);
6788+
} else {
6789+
cbm_unsetenv("CBM_WORKERS");
6790+
}
6791+
th_rmtree(tmp);
6792+
PASS();
6793+
}
6794+
67296795
/* Native `fetch()` (#856), sequential path (< 50 files → pass_calls.c). A bare
67306796
* unqualified call to the global fetch API has no import and no local
67316797
* definition anywhere in this project, so registry resolution comes back
@@ -15160,6 +15226,7 @@ SUITE(pipeline) {
1516015226
RUN_TEST(pipeline_parallel_python_cross_only_dunder_gets_synthetic_carrier);
1516115227
RUN_TEST(pipeline_parallel_rust_cross_only_macro_hidden_gets_synthetic_carrier);
1516215228
RUN_TEST(pipeline_arg_url_rejects_non_http_slash_arguments);
15229+
RUN_TEST(pipeline_ts_config_object_url_http_calls_issue2235);
1516315230
RUN_TEST(pipeline_native_fetch_classified_as_http_calls);
1516415231
RUN_TEST(pipeline_swift_nested_url_makes_route_issue1892);
1516515232
RUN_TEST(pipeline_swift_http_call_makes_route_issue1892);

0 commit comments

Comments
 (0)