Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(routes): accept relative JAX-RS @Path templates

Hubert Tarnacki452485f73e48
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index b49147dc6..b94c0b4c2 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -1416,7 +1416,8 @@ static bool is_route_string_kind(const char *kind) {
strcmp(kind, "interpreted_string_literal") == 0;
}
-static const char *route_path_from_string_node(CBMArena *a, TSNode node, const char *source) {
+static const char *route_path_from_string_node(CBMArena *a, TSNode node, const char *source,
+ bool allow_relative) {
if (!is_route_string_kind(ts_node_type(node))) {
return NULL;
}
@@ -1428,21 +1429,28 @@ static const char *route_path_from_string_node(CBMArena *a, TSNode node, const c
if (plen >= PAIR_CHARS && (path[0] == '"' || path[0] == '\'')) {
path = cbm_arena_strndup(a, path + SKIP_CHAR, (size_t)(plen - PAIR_CHARS));
}
- return (path && path[0] == '/') ? path : NULL;
+ if (!path || path[0] == '/' || !allow_relative) {
+ return (path && path[0] == '/') ? path : NULL;
+ }
+ /* JAX-RS @Path values are relative URI templates; a leading slash is
+ * optional and ignored by the framework. Route nodes use absolute-looking
+ * paths consistently, so normalize a non-empty relative value here. */
+ return path[0] ? cbm_arena_sprintf(a, "/%s", path) : path;
}
static const char *find_route_path_literal(CBMArena *a, TSNode node, const char *source,
- int max_depth) {
+ int max_depth, bool allow_relative) {
if (ts_node_is_null(node) || max_depth < 0) {
return NULL;
}
- const char *path = route_path_from_string_node(a, node, source);
+ const char *path = route_path_from_string_node(a, node, source, allow_relative);
if (path || max_depth == 0) {
return path;
}
uint32_t nc = ts_node_named_child_count(node);
for (uint32_t i = 0; i < nc && i < DECORATOR_SCAN_LIMIT; i++) {
- path = find_route_path_literal(a, ts_node_named_child(node, i), source, max_depth - 1);
+ path = find_route_path_literal(a, ts_node_named_child(node, i), source, max_depth - 1,
+ allow_relative);
if (path) {
return path;
}
@@ -1450,8 +1458,10 @@ static const char *find_route_path_literal(CBMArena *a, TSNode node, const char
return NULL;
}
-// Extract route path from decorator arguments (first string that starts with /).
-static const char *extract_route_path_from_args(CBMArena *a, TSNode args, const char *source) {
+// Extract route path from decorator arguments. Generic mappings keep only
+// slash-prefixed strings; JAX-RS @Path additionally accepts relative templates.
+static const char *extract_route_path_from_args(CBMArena *a, TSNode args, const char *source,
+ bool allow_relative) {
/* Every argument is checked. Java and Kotlin put no order on annotation
* attributes, so `path` can sit anywhere in the list. Stopping early left
* 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
* @GetMapping(path = {"/orders"})
* Walk a bounded subtree and keep the first string literal that is
* path-shaped, while ignoring non-route literals such as media types. */
- const char *path = find_route_path_literal(a, arg, source, CBM_DESCENDANT_MAX_DEPTH);
+ const char *path =
+ find_route_path_literal(a, arg, source, CBM_DESCENDANT_MAX_DEPTH, allow_relative);
if (path) {
return path;
}
@@ -1610,7 +1621,7 @@ static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char
TSNode args = find_decorator_args(dchild);
if (!ts_node_is_null(args)) {
- const char *path = extract_route_path_from_args(a, args, source);
+ const char *path = extract_route_path_from_args(a, args, source, false);
if (path) {
*out_path = path;
*out_method = method;
@@ -1690,7 +1701,7 @@ static bool try_route_from_annotation(CBMArena *a, TSNode annotation, const char
TSNode args = annotation_args_node(annotation);
const char *path = NULL;
if (!ts_node_is_null(args)) {
- path = extract_route_path_from_args(a, args, source);
+ path = extract_route_path_from_args(a, args, source, false);
}
*out_path = path ? path : "/";
*out_method = method;
@@ -1740,7 +1751,7 @@ static void scan_route_annotations(CBMArena *a, TSNode owner, const char *source
if (!*out_jax_path && strcmp(name, "Path") == 0) {
TSNode args = annotation_args_node(child);
if (!ts_node_is_null(args)) {
- *out_jax_path = extract_route_path_from_args(a, args, source);
+ *out_jax_path = extract_route_path_from_args(a, args, source, true);
}
continue;
}
@@ -1750,7 +1761,7 @@ static void scan_route_annotations(CBMArena *a, TSNode owner, const char *source
*out_method = method;
TSNode args = annotation_args_node(child);
if (!ts_node_is_null(args)) {
- *out_map_path = extract_route_path_from_args(a, args, source);
+ *out_map_path = extract_route_path_from_args(a, args, source, false);
}
}
}
diff --git a/tests/test_edge_types_probe.c b/tests/test_edge_types_probe.c
index b330e05e8..5469e19c8 100644
--- a/tests/test_edge_types_probe.c
+++ b/tests/test_edge_types_probe.c
@@ -486,6 +486,48 @@ TEST(handles_jaxrs_java) {
PASS();
}
+/* JAX-RS (Java): @Path values are URI templates relative to the enclosing
+ * resource, and the leading slash is optional. The slash-less spelling must
+ * produce the same rooted Route set as the fixture above. */
+TEST(handles_jaxrs_java_relative_path) {
+ static const char *routes[] = {"/api/v1/widgets", "/api/v1/widgets/count", NULL};
+ static const EtFile f[] = {
+ {"WidgetResource.java",
+ "package com.example;\n\n"
+ "import jakarta.ws.rs.GET;\n"
+ "import jakarta.ws.rs.Path;\n\n"
+ "@Path(\"api/v1/widgets\")\npublic class WidgetResource {\n"
+ " @GET\n"
+ " public String list() {\n"
+ " return \"widgets\";\n }\n\n"
+ " @GET\n"
+ " @Path(\"count\")\n"
+ " public String count() {\n"
+ " return \"42\";\n }\n}\n"}};
+ ASSERT_TRUE(et_edge_present(f, 1, "HANDLES", 2));
+ ASSERT_TRUE(et_routes_exact(f, 1, routes));
+ PASS();
+}
+
+/* Negative control: the relative-template acceptance is scoped to JAX-RS
+ * @Path. A slash-less string on a Spring mapping annotation is still not read
+ * as a route path, so the handler keeps the "/" fallback and no "/api/orders"
+ * Route node appears. */
+TEST(handles_spring_java_relative_string_not_route) {
+ static const char *routes[] = {"/", NULL};
+ static const EtFile f[] = {
+ {"OrderController.java",
+ "package com.example;\n\n"
+ "import org.springframework.web.bind.annotation.RequestMapping;\n"
+ "import org.springframework.web.bind.annotation.GetMapping;\n\n"
+ "@RequestMapping(\"api\")\npublic class OrderController {\n"
+ " @GetMapping(\"orders\")\n"
+ " public String listOrders() {\n"
+ " return \"orders\";\n }\n}\n"}};
+ ASSERT_TRUE(et_routes_exact(f, 1, routes));
+ PASS();
+}
+
/* ASP.NET Minimal API (C#) — route registration via static MapGet/MapPost calls
* with identifier handlers, under a Microsoft/AspNetCore path so the resolved
* callee QN carries the "MapGet"/"Microsoft.AspNetCore" route-reg substrings.
@@ -1636,6 +1678,8 @@ SUITE(edge_types_probe) {
RUN_TEST(handles_spring_java_path_attribute_fourth);
RUN_TEST(handles_spring_kotlin);
RUN_TEST(handles_jaxrs_java);
+ RUN_TEST(handles_jaxrs_java_relative_path);
+ RUN_TEST(handles_spring_java_relative_string_not_route);
RUN_TEST(handles_aspnet_csharp);
RUN_TEST(handles_laravel_php);
RUN_TEST(handles_laravel_facade_routes_issue952);
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index cb01dcf14..b967ad50a 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -4173,6 +4173,70 @@ TEST(extract_java_jaxrs_path_composition_issue1005) {
PASS();
}
+/* JAX-RS @Path values are URI templates relative to the enclosing resource;
+ * the leading slash is optional and ignored by the framework. Relative class
+ * and method templates must compose to the same rooted routes as the
+ * slash-prefixed spelling above. */
+TEST(extract_java_jaxrs_relative_path_templates) {
+ CBMFileResult *r = extract("import jakarta.ws.rs.GET;\n"
+ "import jakarta.ws.rs.Path;\n"
+ "@Path(\"api/v1/widgets\")\n"
+ "public class WidgetResource {\n"
+ " @GET\n"
+ " public String list() { return \"\"; }\n"
+ " @GET\n"
+ " @Path(\"count\")\n"
+ " public String count() { return \"\"; }\n"
+ " @GET\n"
+ " @Path(\"{id}/tags\")\n"
+ " public String tags() { return \"\"; }\n"
+ "}\n",
+ CBM_LANG_JAVA, "t", "WidgetResource.java");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ const CBMDefinition *list = find_def_by_name(r, "list");
+ ASSERT_NOT_NULL(list);
+ ASSERT_NOT_NULL(list->route_path);
+ ASSERT_STR_EQ(list->route_path, "/api/v1/widgets");
+ ASSERT_STR_EQ(list->route_method, "GET");
+ const CBMDefinition *count = find_def_by_name(r, "count");
+ ASSERT_NOT_NULL(count);
+ ASSERT_NOT_NULL(count->route_path);
+ ASSERT_STR_EQ(count->route_path, "/api/v1/widgets/count");
+ ASSERT_STR_EQ(count->route_method, "GET");
+ const CBMDefinition *tags = find_def_by_name(r, "tags");
+ ASSERT_NOT_NULL(tags);
+ ASSERT_NOT_NULL(tags->route_path);
+ ASSERT_STR_EQ(tags->route_path, "/api/v1/widgets/{id}/tags");
+ ASSERT_STR_EQ(tags->route_method, "GET");
+ cbm_free_result(r);
+ PASS();
+}
+
+/* Negative control: relative templates are accepted for JAX-RS @Path only.
+ * A slash-less string on a non-JAX-RS mapping annotation must keep the
+ * previous behaviour (the literal is not read as a route path), so the
+ * relaxation cannot broaden route extraction for other frameworks. */
+TEST(extract_java_spring_relative_string_not_route_path) {
+ CBMFileResult *r = extract("import org.springframework.web.bind.annotation.GetMapping;\n"
+ "import org.springframework.web.bind.annotation.RequestMapping;\n"
+ "@RequestMapping(\"api\")\n"
+ "public class OrderController {\n"
+ " @GetMapping(\"orders\")\n"
+ " public String listOrders() { return \"\"; }\n"
+ "}\n",
+ CBM_LANG_JAVA, "t", "OrderController.java");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ const CBMDefinition *list = find_def_by_name(r, "listOrders");
+ ASSERT_NOT_NULL(list);
+ ASSERT_NOT_NULL(list->route_path);
+ ASSERT_STR_EQ(list->route_path, "/");
+ ASSERT_STR_EQ(list->route_method, "GET");
+ cbm_free_result(r);
+ PASS();
+}
+
/* Return the file's Module definition (extraction pushes it first), or NULL. */
static const CBMDefinition *find_module_def(CBMFileResult *r) {
for (int i = 0; i < r->defs.count; i++) {
@@ -7453,6 +7517,8 @@ SUITE(extraction) {
RUN_TEST(arkts_lazy_import);
RUN_TEST(arkts_ts_compat);
RUN_TEST(extract_java_jaxrs_path_composition_issue1005);
+ RUN_TEST(extract_java_jaxrs_relative_path_templates);
+ RUN_TEST(extract_java_spring_relative_string_not_route_path);
RUN_TEST(extract_blazor_page_directive_routes_component);
RUN_TEST(extract_blazor_component_without_page_has_no_route);
RUN_TEST(extract_razor_page_directive_routes_cshtml_view);