Skip to content

Commit 1ff6299

Browse files
authored
Merge pull request DeusData#2354 from DeusData/fix/issue-2235
fix(extract): read the url of a JS/TS request-config object (DeusData#2235)
2 parents 368f33f + 3ef7d8c commit 1ff6299

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
@@ -2105,6 +2105,56 @@ static void process_keyword_arg(CBMExtractCtx *ctx, TSNode arg_node, CBMCallArg
21052105
}
21062106
}
21072107

2108+
static const char *extract_string_value(CBMExtractCtx *ctx, TSNode val_node);
2109+
2110+
/* A JS/TS request-config object -- `http({url: `/users/${id}`, method: 'GET'})`,
2111+
* `axios({url: '/orders'})`, the shape Orval and similar generators emit -- names
2112+
* its request URL in the `url` property. Return that URL (template literals
2113+
* flattened to the canonical "{}" form, like a bare template argument), or NULL.
2114+
* Only the `url` key counts: `path`/`endpoint` in an options object name other
2115+
* things too often. Route registrations are excluded: an object there is the
2116+
* route definition (Fastify `route({url, handler})`), not a client request, and
2117+
* its handler lives inside the object where the route pass cannot read it. So is
2118+
* axios `getUri(config)`: it takes the same config but only formats the URL and
2119+
* sends nothing (Orval's `get<Op>Url` helpers). (#2235) */
2120+
static bool config_object_callee_eligible(const CBMExtractCtx *ctx, const char *callee_name) {
2121+
if (ctx->language != CBM_LANG_JAVASCRIPT && ctx->language != CBM_LANG_TYPESCRIPT &&
2122+
ctx->language != CBM_LANG_TSX && ctx->language != CBM_LANG_ARKTS) {
2123+
return false;
2124+
}
2125+
if (!callee_name || cbm_service_pattern_route_method(callee_name) != NULL) {
2126+
return false;
2127+
}
2128+
static const char get_uri[] = "getUri";
2129+
size_t clen = strlen(callee_name);
2130+
size_t glen = sizeof(get_uri) - 1;
2131+
return !(clen >= glen && strcmp(callee_name + clen - glen, get_uri) == 0);
2132+
}
2133+
2134+
static const char *js_config_object_url(CBMExtractCtx *ctx, TSNode obj, const char *callee_name) {
2135+
if (strcmp(ts_node_type(obj), "object") != 0 ||
2136+
!config_object_callee_eligible(ctx, callee_name)) {
2137+
return NULL;
2138+
}
2139+
uint32_t n = ts_node_named_child_count(obj);
2140+
for (uint32_t i = 0; i < n; i++) {
2141+
TSNode pair = ts_node_named_child(obj, i);
2142+
if (strcmp(ts_node_type(pair), "pair") != 0) {
2143+
continue;
2144+
}
2145+
TSNode key_n = ts_node_child_by_field_name(pair, TS_FIELD("key"));
2146+
TSNode val_n = ts_node_child_by_field_name(pair, TS_FIELD("value"));
2147+
if (ts_node_is_null(key_n) || ts_node_is_null(val_n)) {
2148+
continue;
2149+
}
2150+
const char *key = strip_quotes(ctx->arena, cbm_node_text(ctx->arena, key_n, ctx->source));
2151+
if (key && strcmp(key, "url") == 0) {
2152+
return extract_string_value(ctx, val_n);
2153+
}
2154+
}
2155+
return NULL;
2156+
}
2157+
21082158
/* Extract all arguments from a call expression into call->args[]. */
21092159
static void extract_call_args(CBMExtractCtx *ctx, TSNode args, CBMCall *call) {
21102160
uint32_t argc = ts_node_named_child_count(args);
@@ -2147,6 +2197,10 @@ static void extract_call_args(CBMExtractCtx *ctx, TSNode args, CBMCall *call) {
21472197
ca->value = cbm_template_string_text(ctx->arena, arg_node, ctx->source);
21482198
} else if (strcmp(ak, "identifier") == 0 && ca->expr) {
21492199
ca->value = lookup_string_constant(ctx, ca->expr);
2200+
} else if (strcmp(ak, "object") == 0) {
2201+
/* Request-config object: its `url` is what the arg-url
2202+
* heuristic reads for a local fetch wrapper (#2235). */
2203+
ca->value = js_config_object_url(ctx, arg_node, call->callee_name);
21502204
} else if (strcmp(ak, "call_expression") == 0) {
21512205
/* URL-builder helper call (issue #1009): resolve
21522206
* client(buildPath(id)) through the per-file builder map. */
@@ -2411,7 +2465,8 @@ static const char *extract_positional_url(CBMExtractCtx *ctx, TSNode arg, const
24112465
}
24122466

24132467
// Extract URL/topic from keyword or positional args.
2414-
static const char *extract_url_or_topic_arg(CBMExtractCtx *ctx, TSNode args) {
2468+
static const char *extract_url_or_topic_arg(CBMExtractCtx *ctx, TSNode args,
2469+
const char *callee_name) {
24152470
uint32_t nc = ts_node_named_child_count(args);
24162471
for (uint32_t ai = 0; ai < nc; ai++) {
24172472
TSNode arg = ts_node_named_child(args, ai);
@@ -2439,6 +2494,15 @@ static const char *extract_url_or_topic_arg(CBMExtractCtx *ctx, TSNode args) {
24392494
continue;
24402495
}
24412496

2497+
/* JS/TS request-config object: `axios({url: '/orders'})` (#2235). */
2498+
if (strcmp(ak, "object") == 0) {
2499+
const char *val = js_config_object_url(ctx, arg, callee_name);
2500+
if (val) {
2501+
return val;
2502+
}
2503+
continue;
2504+
}
2505+
24422506
/* Cloud SDK dispatch via input struct: the queue/topic target is a field
24432507
* of a composite literal (Go `&sqs.SendMessageInput{QueueUrl: ...}`), not
24442508
* a bare string arg. Recover it so the async edge forms. */
@@ -3876,7 +3940,7 @@ CBMInvocationDescriptor handle_calls(CBMExtractCtx *ctx, TSNode node, const CBML
38763940
args = swift_call_args(node);
38773941
}
38783942
if (!ts_node_is_null(args)) {
3879-
call.first_string_arg = extract_url_or_topic_arg(ctx, args);
3943+
call.first_string_arg = extract_url_or_topic_arg(ctx, args, call.callee_name);
38803944
/* #952: routes registered inside Laravel `prefix()->group()`
38813945
* closures must carry the composed path — the resolve passes
38823946
* 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
@@ -5146,6 +5146,62 @@ TEST(extract_ts_template_string_url_issue1006) {
51465146
PASS();
51475147
}
51485148

5149+
/* Issue #2235: a request-config object argument carries the URL in its `url`
5150+
* property (Orval/axios style: `http({url: `/x/${id}`, method: 'GET'})`).
5151+
* The property's template literal flattens to the canonical "{}" form both on
5152+
* the argument (read by the arg-url heuristic for local helpers) and as the
5153+
* call's URL (read by HTTP-client classification, e.g. `axios({url})`). Other
5154+
* keys never stand in for the URL, and a route registration keeps its own
5155+
* (object-free) handling. */
5156+
TEST(extract_ts_config_object_url_issue2235) {
5157+
CBMFileResult *r =
5158+
extract("export const get1 = (id: string) => {\n"
5159+
" return http<Customer>({url: `/api/customers/${id}`, method: 'GET'});\n"
5160+
"};\n"
5161+
"export const load = () => axios({method: 'POST', 'url': '/api/orders'});\n"
5162+
"export const other = () => http({path: '/api/ignored/x'});\n"
5163+
"export const reg = () => fastify.route({url: '/srv/x', handler: h});\n"
5164+
"export const u = () => axiosInstance.getUri({url: `/pets`});\n",
5165+
CBM_LANG_TYPESCRIPT, "t", "client.ts");
5166+
ASSERT_NOT_NULL(r);
5167+
ASSERT_FALSE(r->has_error);
5168+
const CBMCall *c = NULL;
5169+
const CBMCall *o = NULL;
5170+
for (int i = 0; i < r->calls.count; i++) {
5171+
const CBMCall *k = &r->calls.items[i];
5172+
if (k->callee_name && strcmp(k->callee_name, "http") == 0 && k->arg_count > 0) {
5173+
if (k->args[0].expr && strstr(k->args[0].expr, "customers")) {
5174+
c = k;
5175+
} else {
5176+
o = k;
5177+
}
5178+
}
5179+
}
5180+
ASSERT_NOT_NULL(c);
5181+
ASSERT_NOT_NULL(c->args[0].value);
5182+
ASSERT_STR_EQ(c->args[0].value, "/api/customers/{}");
5183+
ASSERT_NOT_NULL(c->first_string_arg);
5184+
ASSERT_STR_EQ(c->first_string_arg, "/api/customers/{}");
5185+
const CBMCall *a = find_call_by_callee(r, "axios");
5186+
ASSERT_NOT_NULL(a);
5187+
ASSERT_NOT_NULL(a->first_string_arg);
5188+
ASSERT_STR_EQ(a->first_string_arg, "/api/orders");
5189+
ASSERT_NOT_NULL(o);
5190+
ASSERT_NULL(o->args[0].value);
5191+
ASSERT_NULL(o->first_string_arg);
5192+
const CBMCall *g = find_call_by_callee(r, "fastify.route");
5193+
ASSERT_NOT_NULL(g);
5194+
ASSERT_NULL(g->first_string_arg);
5195+
ASSERT(g->arg_count == 0 || g->args[0].value == NULL);
5196+
/* getUri only formats the URL; it sends no request. */
5197+
const CBMCall *gu = find_call_by_callee(r, "axiosInstance.getUri");
5198+
ASSERT_NOT_NULL(gu);
5199+
ASSERT_NULL(gu->first_string_arg);
5200+
ASSERT(gu->arg_count == 0 || gu->args[0].value == NULL);
5201+
cbm_free_result(r);
5202+
PASS();
5203+
}
5204+
51495205
/* Issue #1249: a mux route built as `configVar + "/literal"` (Go's idiomatic
51505206
* configurable-base-path pattern) must index the literal suffix, both for a
51515207
* route registration and for an outbound URL built the same way. A real BFF
@@ -9087,6 +9143,7 @@ SUITE(extraction) {
90879143
RUN_TEST(extract_razor_page_directive_routes_cshtml_view);
90889144
RUN_TEST(extract_razor_layout_without_page_has_no_route);
90899145
RUN_TEST(extract_ts_template_string_url_issue1006);
9146+
RUN_TEST(extract_ts_config_object_url_issue2235);
90909147
RUN_TEST(extract_go_binary_concat_url_issue1249);
90919148
RUN_TEST(extract_go_binary_concat_url_no_literal_suffix_issue1249);
90929149
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
@@ -7364,6 +7364,72 @@ TEST(pipeline_arg_url_rejects_non_http_slash_arguments) {
73647364
PASS();
73657365
}
73667366

7367+
/* #2235: an Orval-style generated client passes its URL inside a request
7368+
* config object to a LOCAL helper -- `http({url: `/api/customers/${id}`,
7369+
* method: 'GET'})`. The template literal itself already canonicalizes to
7370+
* "/api/customers/{}"; what was lost is the object wrapper: the arg-url
7371+
* heuristic only read bare string/template arguments, so the call produced
7372+
* no HTTP_CALLS edge and cross-repo matching had nothing to join. Parallel
7373+
* resolver (> 50 files), where the arg-url heuristic lives. A config object
7374+
* without a `url` key must not mint a route. */
7375+
TEST(pipeline_ts_config_object_url_http_calls_issue2235) {
7376+
char tmp[256];
7377+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_i2235_XXXXXX");
7378+
if (!cbm_mkdtemp(tmp)) {
7379+
FAIL("tmpdir");
7380+
}
7381+
write_temp_file(tmp, "src/http.ts",
7382+
"export const http = <T,>(config: { url: string; method: string }): "
7383+
"Promise<T> => {\n"
7384+
" return fetch(config.url, { method: config.method }).then((r) => r.json());\n"
7385+
"};\n");
7386+
write_temp_file(
7387+
tmp, "src/gen/customer-controller.ts",
7388+
"import { http } from '../http';\n"
7389+
"export const get1 = (id: string, signal?: AbortSignal) => {\n"
7390+
" return http<unknown>({url: `/api/customers/${id}`, method: 'GET', signal});\n"
7391+
"};\n"
7392+
"export const noUrl = () => {\n"
7393+
" return http<unknown>({path: '/api/ignored/x', method: 'GET'});\n"
7394+
"};\n");
7395+
for (int i = 0; i < 52; i++) {
7396+
char name[64];
7397+
char body[128];
7398+
snprintf(name, sizeof(name), "src/filler%d.ts", i);
7399+
snprintf(body, sizeof(body), "export function filler%d(): number { return %d; }\n", i, i);
7400+
write_temp_file(tmp, name, body);
7401+
}
7402+
7403+
char *old_workers = getenv("CBM_WORKERS");
7404+
char *saved = old_workers ? strdup(old_workers) : NULL;
7405+
cbm_setenv("CBM_WORKERS", "4", 1);
7406+
7407+
char db_path[512];
7408+
snprintf(db_path, sizeof(db_path), "%s/i2235.db", tmp);
7409+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
7410+
ASSERT_NOT_NULL(p);
7411+
ASSERT_EQ(cbm_pipeline_run(p), 0);
7412+
const char *project = cbm_pipeline_project_name(p);
7413+
cbm_store_t *s = cbm_store_open_path(db_path);
7414+
ASSERT_NOT_NULL(s);
7415+
7416+
/* RED before the fix: the url inside the config object was never read. */
7417+
ASSERT_EQ(named_edge_count(s, project, "HTTP_CALLS", "get1", "/api/customers/{}"), 1);
7418+
/* Only the `url` key names the request URL. */
7419+
ASSERT_EQ(count_nodes_named(s, project, "/api/ignored/x"), 0);
7420+
7421+
cbm_store_close(s);
7422+
cbm_pipeline_free(p);
7423+
if (saved) {
7424+
cbm_setenv("CBM_WORKERS", saved, 1);
7425+
free(saved);
7426+
} else {
7427+
cbm_unsetenv("CBM_WORKERS");
7428+
}
7429+
th_rmtree(tmp);
7430+
PASS();
7431+
}
7432+
73677433
/* Native `fetch()` (#856), sequential path (< 50 files → pass_calls.c). A bare
73687434
* unqualified call to the global fetch API has no import and no local
73697435
* definition anywhere in this project, so registry resolution comes back
@@ -16205,6 +16271,7 @@ SUITE(pipeline) {
1620516271
RUN_TEST(pipeline_parallel_python_cross_only_dunder_gets_synthetic_carrier);
1620616272
RUN_TEST(pipeline_parallel_rust_cross_only_macro_hidden_gets_synthetic_carrier);
1620716273
RUN_TEST(pipeline_arg_url_rejects_non_http_slash_arguments);
16274+
RUN_TEST(pipeline_ts_config_object_url_http_calls_issue2235);
1620816275
RUN_TEST(pipeline_native_fetch_classified_as_http_calls);
1620916276
RUN_TEST(pipeline_swift_nested_url_makes_route_issue1892);
1621016277
RUN_TEST(pipeline_swift_http_call_makes_route_issue1892);

0 commit comments

Comments
 (0)