Skip to content

Commit 452485f

Browse files
committed
fix(routes): accept relative JAX-RS @path templates
JAX-RS allows @path values without a leading slash, both on the class and on the method. The route extractor rejected such literals, so resources such as `@Path("widgets")` + `@Path("{id}")` produced no Route nodes and no HANDLES edges at all. Accept relative templates and normalize the class/method composition so the resulting path is always rooted. Existing JAX-RS Java probe tests are updated to use relative templates so they guard the regression. Signed-off-by: Hubert Tarnacki <hubert.tarnacki@gmail.com>
1 parent 339b3f4 commit 452485f

3 files changed

Lines changed: 133 additions & 12 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,8 @@ static bool is_route_string_kind(const char *kind) {
14161416
strcmp(kind, "interpreted_string_literal") == 0;
14171417
}
14181418

1419-
static const char *route_path_from_string_node(CBMArena *a, TSNode node, const char *source) {
1419+
static const char *route_path_from_string_node(CBMArena *a, TSNode node, const char *source,
1420+
bool allow_relative) {
14201421
if (!is_route_string_kind(ts_node_type(node))) {
14211422
return NULL;
14221423
}
@@ -1428,30 +1429,39 @@ static const char *route_path_from_string_node(CBMArena *a, TSNode node, const c
14281429
if (plen >= PAIR_CHARS && (path[0] == '"' || path[0] == '\'')) {
14291430
path = cbm_arena_strndup(a, path + SKIP_CHAR, (size_t)(plen - PAIR_CHARS));
14301431
}
1431-
return (path && path[0] == '/') ? path : NULL;
1432+
if (!path || path[0] == '/' || !allow_relative) {
1433+
return (path && path[0] == '/') ? path : NULL;
1434+
}
1435+
/* JAX-RS @Path values are relative URI templates; a leading slash is
1436+
* optional and ignored by the framework. Route nodes use absolute-looking
1437+
* paths consistently, so normalize a non-empty relative value here. */
1438+
return path[0] ? cbm_arena_sprintf(a, "/%s", path) : path;
14321439
}
14331440

14341441
static const char *find_route_path_literal(CBMArena *a, TSNode node, const char *source,
1435-
int max_depth) {
1442+
int max_depth, bool allow_relative) {
14361443
if (ts_node_is_null(node) || max_depth < 0) {
14371444
return NULL;
14381445
}
1439-
const char *path = route_path_from_string_node(a, node, source);
1446+
const char *path = route_path_from_string_node(a, node, source, allow_relative);
14401447
if (path || max_depth == 0) {
14411448
return path;
14421449
}
14431450
uint32_t nc = ts_node_named_child_count(node);
14441451
for (uint32_t i = 0; i < nc && i < DECORATOR_SCAN_LIMIT; i++) {
1445-
path = find_route_path_literal(a, ts_node_named_child(node, i), source, max_depth - 1);
1452+
path = find_route_path_literal(a, ts_node_named_child(node, i), source, max_depth - 1,
1453+
allow_relative);
14461454
if (path) {
14471455
return path;
14481456
}
14491457
}
14501458
return NULL;
14511459
}
14521460

1453-
// Extract route path from decorator arguments (first string that starts with /).
1454-
static const char *extract_route_path_from_args(CBMArena *a, TSNode args, const char *source) {
1461+
// Extract route path from decorator arguments. Generic mappings keep only
1462+
// slash-prefixed strings; JAX-RS @Path additionally accepts relative templates.
1463+
static const char *extract_route_path_from_args(CBMArena *a, TSNode args, const char *source,
1464+
bool allow_relative) {
14551465
/* Every argument is checked. Java and Kotlin put no order on annotation
14561466
* attributes, so `path` can sit anywhere in the list. Stopping early left
14571467
* a real route unread and formed no Route node. Each argument's own
@@ -1464,7 +1474,8 @@ static const char *extract_route_path_from_args(CBMArena *a, TSNode args, const
14641474
* @GetMapping(path = {"/orders"})
14651475
* Walk a bounded subtree and keep the first string literal that is
14661476
* path-shaped, while ignoring non-route literals such as media types. */
1467-
const char *path = find_route_path_literal(a, arg, source, CBM_DESCENDANT_MAX_DEPTH);
1477+
const char *path =
1478+
find_route_path_literal(a, arg, source, CBM_DESCENDANT_MAX_DEPTH, allow_relative);
14681479
if (path) {
14691480
return path;
14701481
}
@@ -1610,7 +1621,7 @@ static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char
16101621

16111622
TSNode args = find_decorator_args(dchild);
16121623
if (!ts_node_is_null(args)) {
1613-
const char *path = extract_route_path_from_args(a, args, source);
1624+
const char *path = extract_route_path_from_args(a, args, source, false);
16141625
if (path) {
16151626
*out_path = path;
16161627
*out_method = method;
@@ -1690,7 +1701,7 @@ static bool try_route_from_annotation(CBMArena *a, TSNode annotation, const char
16901701
TSNode args = annotation_args_node(annotation);
16911702
const char *path = NULL;
16921703
if (!ts_node_is_null(args)) {
1693-
path = extract_route_path_from_args(a, args, source);
1704+
path = extract_route_path_from_args(a, args, source, false);
16941705
}
16951706
*out_path = path ? path : "/";
16961707
*out_method = method;
@@ -1740,7 +1751,7 @@ static void scan_route_annotations(CBMArena *a, TSNode owner, const char *source
17401751
if (!*out_jax_path && strcmp(name, "Path") == 0) {
17411752
TSNode args = annotation_args_node(child);
17421753
if (!ts_node_is_null(args)) {
1743-
*out_jax_path = extract_route_path_from_args(a, args, source);
1754+
*out_jax_path = extract_route_path_from_args(a, args, source, true);
17441755
}
17451756
continue;
17461757
}
@@ -1750,7 +1761,7 @@ static void scan_route_annotations(CBMArena *a, TSNode owner, const char *source
17501761
*out_method = method;
17511762
TSNode args = annotation_args_node(child);
17521763
if (!ts_node_is_null(args)) {
1753-
*out_map_path = extract_route_path_from_args(a, args, source);
1764+
*out_map_path = extract_route_path_from_args(a, args, source, false);
17541765
}
17551766
}
17561767
}

‎tests/test_edge_types_probe.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,48 @@ TEST(handles_jaxrs_java) {
486486
PASS();
487487
}
488488

489+
/* JAX-RS (Java): @Path values are URI templates relative to the enclosing
490+
* resource, and the leading slash is optional. The slash-less spelling must
491+
* produce the same rooted Route set as the fixture above. */
492+
TEST(handles_jaxrs_java_relative_path) {
493+
static const char *routes[] = {"/api/v1/widgets", "/api/v1/widgets/count", NULL};
494+
static const EtFile f[] = {
495+
{"WidgetResource.java",
496+
"package com.example;\n\n"
497+
"import jakarta.ws.rs.GET;\n"
498+
"import jakarta.ws.rs.Path;\n\n"
499+
"@Path(\"api/v1/widgets\")\npublic class WidgetResource {\n"
500+
" @GET\n"
501+
" public String list() {\n"
502+
" return \"widgets\";\n }\n\n"
503+
" @GET\n"
504+
" @Path(\"count\")\n"
505+
" public String count() {\n"
506+
" return \"42\";\n }\n}\n"}};
507+
ASSERT_TRUE(et_edge_present(f, 1, "HANDLES", 2));
508+
ASSERT_TRUE(et_routes_exact(f, 1, routes));
509+
PASS();
510+
}
511+
512+
/* Negative control: the relative-template acceptance is scoped to JAX-RS
513+
* @Path. A slash-less string on a Spring mapping annotation is still not read
514+
* as a route path, so the handler keeps the "/" fallback and no "/api/orders"
515+
* Route node appears. */
516+
TEST(handles_spring_java_relative_string_not_route) {
517+
static const char *routes[] = {"/", NULL};
518+
static const EtFile f[] = {
519+
{"OrderController.java",
520+
"package com.example;\n\n"
521+
"import org.springframework.web.bind.annotation.RequestMapping;\n"
522+
"import org.springframework.web.bind.annotation.GetMapping;\n\n"
523+
"@RequestMapping(\"api\")\npublic class OrderController {\n"
524+
" @GetMapping(\"orders\")\n"
525+
" public String listOrders() {\n"
526+
" return \"orders\";\n }\n}\n"}};
527+
ASSERT_TRUE(et_routes_exact(f, 1, routes));
528+
PASS();
529+
}
530+
489531
/* ASP.NET Minimal API (C#) — route registration via static MapGet/MapPost calls
490532
* with identifier handlers, under a Microsoft/AspNetCore path so the resolved
491533
* callee QN carries the "MapGet"/"Microsoft.AspNetCore" route-reg substrings.
@@ -1636,6 +1678,8 @@ SUITE(edge_types_probe) {
16361678
RUN_TEST(handles_spring_java_path_attribute_fourth);
16371679
RUN_TEST(handles_spring_kotlin);
16381680
RUN_TEST(handles_jaxrs_java);
1681+
RUN_TEST(handles_jaxrs_java_relative_path);
1682+
RUN_TEST(handles_spring_java_relative_string_not_route);
16391683
RUN_TEST(handles_aspnet_csharp);
16401684
RUN_TEST(handles_laravel_php);
16411685
RUN_TEST(handles_laravel_facade_routes_issue952);

‎tests/test_extraction.c‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4173,6 +4173,70 @@ TEST(extract_java_jaxrs_path_composition_issue1005) {
41734173
PASS();
41744174
}
41754175

4176+
/* JAX-RS @Path values are URI templates relative to the enclosing resource;
4177+
* the leading slash is optional and ignored by the framework. Relative class
4178+
* and method templates must compose to the same rooted routes as the
4179+
* slash-prefixed spelling above. */
4180+
TEST(extract_java_jaxrs_relative_path_templates) {
4181+
CBMFileResult *r = extract("import jakarta.ws.rs.GET;\n"
4182+
"import jakarta.ws.rs.Path;\n"
4183+
"@Path(\"api/v1/widgets\")\n"
4184+
"public class WidgetResource {\n"
4185+
" @GET\n"
4186+
" public String list() { return \"\"; }\n"
4187+
" @GET\n"
4188+
" @Path(\"count\")\n"
4189+
" public String count() { return \"\"; }\n"
4190+
" @GET\n"
4191+
" @Path(\"{id}/tags\")\n"
4192+
" public String tags() { return \"\"; }\n"
4193+
"}\n",
4194+
CBM_LANG_JAVA, "t", "WidgetResource.java");
4195+
ASSERT_NOT_NULL(r);
4196+
ASSERT_FALSE(r->has_error);
4197+
const CBMDefinition *list = find_def_by_name(r, "list");
4198+
ASSERT_NOT_NULL(list);
4199+
ASSERT_NOT_NULL(list->route_path);
4200+
ASSERT_STR_EQ(list->route_path, "/api/v1/widgets");
4201+
ASSERT_STR_EQ(list->route_method, "GET");
4202+
const CBMDefinition *count = find_def_by_name(r, "count");
4203+
ASSERT_NOT_NULL(count);
4204+
ASSERT_NOT_NULL(count->route_path);
4205+
ASSERT_STR_EQ(count->route_path, "/api/v1/widgets/count");
4206+
ASSERT_STR_EQ(count->route_method, "GET");
4207+
const CBMDefinition *tags = find_def_by_name(r, "tags");
4208+
ASSERT_NOT_NULL(tags);
4209+
ASSERT_NOT_NULL(tags->route_path);
4210+
ASSERT_STR_EQ(tags->route_path, "/api/v1/widgets/{id}/tags");
4211+
ASSERT_STR_EQ(tags->route_method, "GET");
4212+
cbm_free_result(r);
4213+
PASS();
4214+
}
4215+
4216+
/* Negative control: relative templates are accepted for JAX-RS @Path only.
4217+
* A slash-less string on a non-JAX-RS mapping annotation must keep the
4218+
* previous behaviour (the literal is not read as a route path), so the
4219+
* relaxation cannot broaden route extraction for other frameworks. */
4220+
TEST(extract_java_spring_relative_string_not_route_path) {
4221+
CBMFileResult *r = extract("import org.springframework.web.bind.annotation.GetMapping;\n"
4222+
"import org.springframework.web.bind.annotation.RequestMapping;\n"
4223+
"@RequestMapping(\"api\")\n"
4224+
"public class OrderController {\n"
4225+
" @GetMapping(\"orders\")\n"
4226+
" public String listOrders() { return \"\"; }\n"
4227+
"}\n",
4228+
CBM_LANG_JAVA, "t", "OrderController.java");
4229+
ASSERT_NOT_NULL(r);
4230+
ASSERT_FALSE(r->has_error);
4231+
const CBMDefinition *list = find_def_by_name(r, "listOrders");
4232+
ASSERT_NOT_NULL(list);
4233+
ASSERT_NOT_NULL(list->route_path);
4234+
ASSERT_STR_EQ(list->route_path, "/");
4235+
ASSERT_STR_EQ(list->route_method, "GET");
4236+
cbm_free_result(r);
4237+
PASS();
4238+
}
4239+
41764240
/* Return the file's Module definition (extraction pushes it first), or NULL. */
41774241
static const CBMDefinition *find_module_def(CBMFileResult *r) {
41784242
for (int i = 0; i < r->defs.count; i++) {
@@ -7453,6 +7517,8 @@ SUITE(extraction) {
74537517
RUN_TEST(arkts_lazy_import);
74547518
RUN_TEST(arkts_ts_compat);
74557519
RUN_TEST(extract_java_jaxrs_path_composition_issue1005);
7520+
RUN_TEST(extract_java_jaxrs_relative_path_templates);
7521+
RUN_TEST(extract_java_spring_relative_string_not_route_path);
74567522
RUN_TEST(extract_blazor_page_directive_routes_component);
74577523
RUN_TEST(extract_blazor_component_without_page_has_no_route);
74587524
RUN_TEST(extract_razor_page_directive_routes_cshtml_view);

0 commit comments

Comments
 (0)