Skip to content

Commit 041eb99

Browse files
fix: compose JAX-RS method-level @path with class-level prefix (DeusData#1005)
JAX-RS splits a route across two annotations: the verb comes from a bare @GET/@POST/... and the path from a sibling @path. The annotation scan returned on the first mapping annotation, so the @get matched, defaulted the path to "/" and the sibling @path was never read. Class-level @path was never recognized as a prefix either, because a lone @path carries no verb and the scan reported no route. scan_route_annotations() now collects the whole annotation set (mapping verb + optional inline path, and a separate JAX-RS @path), then: - method-level: verb required; path = inline mapping path, else @path, else "/" - class-level prefix: inline mapping path, else @path Also recognizes bare @HEAD/@options verbs. Spring behavior is unchanged (mapping annotations keep their inline path); covered by the existing test suite plus a new regression test. Signed-off-by: Charles Queiroz <fcqueiroz@liquibase.com>
1 parent 6b57db2 commit 041eb99

3 files changed

Lines changed: 146 additions & 28 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 86 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,8 @@ static const char *annotation_route_method(const char *name) {
12961296
}
12971297
/* JAX-RS bare-verb annotations (@GET/@POST/...) — path comes from @Path. */
12981298
if (strcmp(name, "GET") == 0 || strcmp(name, "POST") == 0 || strcmp(name, "PUT") == 0 ||
1299-
strcmp(name, "DELETE") == 0 || strcmp(name, "PATCH") == 0) {
1299+
strcmp(name, "DELETE") == 0 || strcmp(name, "PATCH") == 0 || strcmp(name, "HEAD") == 0 ||
1300+
strcmp(name, "OPTIONS") == 0) {
13001301
return name;
13011302
}
13021303
return NULL;
@@ -1605,35 +1606,85 @@ static bool try_route_from_annotation(CBMArena *a, TSNode annotation, const char
16051606
return true;
16061607
}
16071608

1608-
/* Scan the annotation nodes nested in a JVM/C# `modifiers`/`attribute_list`
1609-
* wrapper (and direct children) for a route-mapping annotation. Java/Kotlin
1610-
* Spring annotations (@GetMapping, @RequestMapping, ...) live here rather than
1611-
* as prev-siblings, so the prev-sibling decorator walk never sees them. */
1612-
static bool extract_route_from_annotations(CBMArena *a, TSNode func_node, const char *source,
1613-
const CBMLangSpec *spec, const char **out_path,
1614-
const char **out_method) {
1615-
TSNode modifiers = find_jvm_modifiers(func_node, spec->language);
1609+
/* Scan ALL annotation nodes nested in a JVM/C# `modifiers`/`attribute_list`
1610+
* wrapper (and direct children), collecting route information from the whole
1611+
* set instead of stopping at the first mapping annotation. Java/Kotlin Spring
1612+
* annotations (@GetMapping, @RequestMapping, ...) live here rather than as
1613+
* prev-siblings, so the prev-sibling decorator walk never sees them.
1614+
*
1615+
* JAX-RS splits the route across two annotations: the verb comes from a bare
1616+
* @GET/@POST/... and the path from a sibling @Path("..."). Returning on the
1617+
* first mapping annotation therefore dropped every method-level @Path
1618+
* (the @GET matched first, defaulted the path to "/", and @Path was never
1619+
* read), and class-level @Path prefixes were never recognized at all. */
1620+
static void scan_route_annotations(CBMArena *a, TSNode owner, const char *source,
1621+
const CBMLangSpec *spec, const char **out_map_path,
1622+
const char **out_method, const char **out_jax_path) {
1623+
*out_map_path = NULL;
1624+
*out_method = NULL;
1625+
*out_jax_path = NULL;
1626+
1627+
TSNode wrappers[2];
1628+
int wn = 0;
1629+
TSNode modifiers = find_jvm_modifiers(owner, spec->language);
16161630
if (!ts_node_is_null(modifiers)) {
1617-
uint32_t mc = ts_node_child_count(modifiers);
1618-
for (uint32_t mi = 0; mi < mc; mi++) {
1619-
TSNode mchild = ts_node_child(modifiers, mi);
1620-
if (cbm_kind_in_set(mchild, spec->decorator_node_types) &&
1621-
try_route_from_annotation(a, mchild, source, out_path, out_method)) {
1622-
return true;
1623-
}
1624-
}
1631+
wrappers[wn++] = modifiers;
16251632
}
16261633
/* Direct-child annotations (some grammars attach the annotation as a child
16271634
* of the method node rather than under `modifiers`). */
1628-
uint32_t cc = ts_node_child_count(func_node);
1629-
for (uint32_t ci = 0; ci < cc; ci++) {
1630-
TSNode child = ts_node_child(func_node, ci);
1631-
if (cbm_kind_in_set(child, spec->decorator_node_types) &&
1632-
try_route_from_annotation(a, child, source, out_path, out_method)) {
1633-
return true;
1635+
wrappers[wn++] = owner;
1636+
1637+
for (int w = 0; w < wn; w++) {
1638+
uint32_t cc = ts_node_child_count(wrappers[w]);
1639+
for (uint32_t ci = 0; ci < cc; ci++) {
1640+
TSNode child = ts_node_child(wrappers[w], ci);
1641+
if (!cbm_kind_in_set(child, spec->decorator_node_types)) {
1642+
continue;
1643+
}
1644+
TSNode name_node = annotation_name_node(child);
1645+
if (ts_node_is_null(name_node)) {
1646+
continue;
1647+
}
1648+
char *name = cbm_node_text(a, name_node, source);
1649+
if (!name) {
1650+
continue;
1651+
}
1652+
if (!*out_jax_path && strcmp(name, "Path") == 0) {
1653+
TSNode args = annotation_args_node(child);
1654+
if (!ts_node_is_null(args)) {
1655+
*out_jax_path = extract_route_path_from_args(a, args, source);
1656+
}
1657+
continue;
1658+
}
1659+
if (!*out_method) {
1660+
const char *method = annotation_route_method(name);
1661+
if (method) {
1662+
*out_method = method;
1663+
TSNode args = annotation_args_node(child);
1664+
if (!ts_node_is_null(args)) {
1665+
*out_map_path = extract_route_path_from_args(a, args, source);
1666+
}
1667+
}
1668+
}
16341669
}
16351670
}
1636-
return false;
1671+
}
1672+
1673+
static bool extract_route_from_annotations(CBMArena *a, TSNode func_node, const char *source,
1674+
const CBMLangSpec *spec, const char **out_path,
1675+
const char **out_method) {
1676+
const char *map_path = NULL;
1677+
const char *method = NULL;
1678+
const char *jax_path = NULL;
1679+
scan_route_annotations(a, func_node, source, spec, &map_path, &method, &jax_path);
1680+
/* Method-level routes still require a verb/mapping annotation; a lone
1681+
* @Path (JAX-RS sub-resource locator) is not an endpoint by itself. */
1682+
if (!method) {
1683+
return false;
1684+
}
1685+
*out_method = method;
1686+
*out_path = map_path ? map_path : (jax_path ? jax_path : "/");
1687+
return true;
16371688
}
16381689

16391690
static void extract_route_from_decorators(CBMArena *a, TSNode func_node, const char *source,
@@ -1702,10 +1753,18 @@ static const char *join_route_paths(CBMArena *a, const char *prefix, const char
17021753

17031754
static const char *spring_class_route_prefix(CBMArena *a, TSNode class_node, const char *source,
17041755
const CBMLangSpec *spec) {
1705-
const char *prefix = NULL;
1756+
const char *map_path = NULL;
17061757
const char *method = NULL;
1707-
if (extract_route_from_annotations(a, class_node, source, spec, &prefix, &method)) {
1708-
return prefix;
1758+
const char *jax_path = NULL;
1759+
scan_route_annotations(a, class_node, source, spec, &map_path, &method, &jax_path);
1760+
if (map_path) {
1761+
return map_path; /* @RequestMapping("/api") and friends */
1762+
}
1763+
if (jax_path) {
1764+
return jax_path; /* JAX-RS class-level @Path("/api") carries no verb */
1765+
}
1766+
if (method) {
1767+
return "/"; /* mapping annotation without a path argument */
17091768
}
17101769
return NULL;
17111770
}

‎tests/test_edge_types_probe.c‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,31 @@ TEST(handles_spring_kotlin) {
381381
PASS();
382382
}
383383

384+
/* JAX-RS (Java): the verb (@GET) and the path (@Path) are sibling
385+
* annotations, and the class-level @Path must prefix method-level paths.
386+
* Reproduce-first (#1005): the first-mapping-annotation scan dropped every
387+
* method-level @Path, collapsing all methods onto a single "/" Route node;
388+
* only the exact Route-name set catches that through the emission dedup. */
389+
TEST(handles_jaxrs_java) {
390+
static const char *routes[] = {"/api/v1/widgets", "/api/v1/widgets/count", NULL};
391+
static const EtFile f[] = {
392+
{"WidgetResource.java",
393+
"package com.example;\n\n"
394+
"import jakarta.ws.rs.GET;\n"
395+
"import jakarta.ws.rs.Path;\n\n"
396+
"@Path(\"/api/v1/widgets\")\npublic class WidgetResource {\n"
397+
" @GET\n"
398+
" public String list() {\n"
399+
" return \"widgets\";\n }\n\n"
400+
" @GET\n"
401+
" @Path(\"/count\")\n"
402+
" public String count() {\n"
403+
" return \"42\";\n }\n}\n"}};
404+
ASSERT_TRUE(et_edge_present(f, 1, "HANDLES", 2));
405+
ASSERT_TRUE(et_routes_exact(f, 1, routes));
406+
PASS();
407+
}
408+
384409
/* ASP.NET Minimal API (C#) — route registration via static MapGet/MapPost calls
385410
* with identifier handlers, under a Microsoft/AspNetCore path so the resolved
386411
* callee QN carries the "MapGet"/"Microsoft.AspNetCore" route-reg substrings.
@@ -1442,7 +1467,7 @@ TEST(override_go_interface) {
14421467
* ══════════════════════════════════════════════════════════════════ */
14431468

14441469
SUITE(edge_types_probe) {
1445-
/* HANDLES — route→handler across web frameworks (8 frameworks) */
1470+
/* HANDLES — route→handler across web frameworks */
14461471
RUN_TEST(handles_flask_python);
14471472
RUN_TEST(handles_fastapi_python);
14481473
RUN_TEST(handles_drf_action_python);
@@ -1451,6 +1476,7 @@ SUITE(edge_types_probe) {
14511476
RUN_TEST(handles_gin_go);
14521477
RUN_TEST(handles_spring_java);
14531478
RUN_TEST(handles_spring_kotlin);
1479+
RUN_TEST(handles_jaxrs_java);
14541480
RUN_TEST(handles_aspnet_csharp);
14551481
RUN_TEST(handles_laravel_php);
14561482
RUN_TEST(handles_rails_ruby);

‎tests/test_extraction.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,6 +2728,38 @@ TEST(extract_java_method_annotations_issue382) {
27282728
PASS();
27292729
}
27302730

2731+
/* Issue #1005: JAX-RS splits a route across two annotations (@GET carries the
2732+
* verb, a sibling @Path carries the path). Returning on the first mapping
2733+
* annotation dropped every method-level @Path, and the class-level @Path
2734+
* prefix was never recognized at all. */
2735+
TEST(extract_java_jaxrs_path_composition_issue1005) {
2736+
CBMFileResult *r = extract("import jakarta.ws.rs.GET;\n"
2737+
"import jakarta.ws.rs.Path;\n"
2738+
"@Path(\"/api/v1/widgets\")\n"
2739+
"public class WidgetResource {\n"
2740+
" @GET\n"
2741+
" public String list() { return \"\"; }\n"
2742+
" @GET\n"
2743+
" @Path(\"/count\")\n"
2744+
" public String count() { return \"\"; }\n"
2745+
"}\n",
2746+
CBM_LANG_JAVA, "t", "WidgetResource.java");
2747+
ASSERT_NOT_NULL(r);
2748+
ASSERT_FALSE(r->has_error);
2749+
const CBMDefinition *list = find_def_by_name(r, "list");
2750+
ASSERT_NOT_NULL(list);
2751+
ASSERT_NOT_NULL(list->route_path);
2752+
ASSERT_STR_EQ(list->route_path, "/api/v1/widgets");
2753+
ASSERT_STR_EQ(list->route_method, "GET");
2754+
const CBMDefinition *count = find_def_by_name(r, "count");
2755+
ASSERT_NOT_NULL(count);
2756+
ASSERT_NOT_NULL(count->route_path);
2757+
ASSERT_STR_EQ(count->route_path, "/api/v1/widgets/count");
2758+
ASSERT_STR_EQ(count->route_method, "GET");
2759+
cbm_free_result(r);
2760+
PASS();
2761+
}
2762+
27312763
/* Find an in-body call by its raw callee text; returns the call or NULL. */
27322764
static const CBMCall *find_call_by_callee(CBMFileResult *r, const char *callee) {
27332765
for (int i = 0; i < r->calls.count; i++) {
@@ -3720,6 +3752,7 @@ SUITE(extraction) {
37203752
RUN_TEST(js_index_module_qn_not_collide_with_folder);
37213753
RUN_TEST(python_regular_module_qn_unchanged);
37223754
RUN_TEST(extract_java_method_annotations_issue382);
3755+
RUN_TEST(extract_java_jaxrs_path_composition_issue1005);
37233756
RUN_TEST(extract_java_no_double_class_qn);
37243757
RUN_TEST(extract_go_no_filename_in_module_qn);
37253758
RUN_TEST(extract_large_ts_has_functions_issue213);

0 commit comments

Comments
 (0)