Skip to content

Commit 34867d9

Browse files
authored
Merge pull request DeusData#1007 from CharlesQueiroz/fix/jaxrs-path-composition
fix: compose JAX-RS method-level @path with class-level prefix
2 parents feadbe1 + 041eb99 commit 34867d9

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
@@ -1317,7 +1317,8 @@ static const char *annotation_route_method(const char *name) {
13171317
}
13181318
/* JAX-RS bare-verb annotations (@GET/@POST/...) — path comes from @Path. */
13191319
if (strcmp(name, "GET") == 0 || strcmp(name, "POST") == 0 || strcmp(name, "PUT") == 0 ||
1320-
strcmp(name, "DELETE") == 0 || strcmp(name, "PATCH") == 0) {
1320+
strcmp(name, "DELETE") == 0 || strcmp(name, "PATCH") == 0 || strcmp(name, "HEAD") == 0 ||
1321+
strcmp(name, "OPTIONS") == 0) {
13211322
return name;
13221323
}
13231324
return NULL;
@@ -1626,35 +1627,85 @@ static bool try_route_from_annotation(CBMArena *a, TSNode annotation, const char
16261627
return true;
16271628
}
16281629

1629-
/* Scan the annotation nodes nested in a JVM/C# `modifiers`/`attribute_list`
1630-
* wrapper (and direct children) for a route-mapping annotation. Java/Kotlin
1631-
* Spring annotations (@GetMapping, @RequestMapping, ...) live here rather than
1632-
* as prev-siblings, so the prev-sibling decorator walk never sees them. */
1633-
static bool extract_route_from_annotations(CBMArena *a, TSNode func_node, const char *source,
1634-
const CBMLangSpec *spec, const char **out_path,
1635-
const char **out_method) {
1636-
TSNode modifiers = find_jvm_modifiers(func_node, spec->language);
1630+
/* Scan ALL annotation nodes nested in a JVM/C# `modifiers`/`attribute_list`
1631+
* wrapper (and direct children), collecting route information from the whole
1632+
* set instead of stopping at the first mapping annotation. Java/Kotlin Spring
1633+
* annotations (@GetMapping, @RequestMapping, ...) live here rather than as
1634+
* prev-siblings, so the prev-sibling decorator walk never sees them.
1635+
*
1636+
* JAX-RS splits the route across two annotations: the verb comes from a bare
1637+
* @GET/@POST/... and the path from a sibling @Path("..."). Returning on the
1638+
* first mapping annotation therefore dropped every method-level @Path
1639+
* (the @GET matched first, defaulted the path to "/", and @Path was never
1640+
* read), and class-level @Path prefixes were never recognized at all. */
1641+
static void scan_route_annotations(CBMArena *a, TSNode owner, const char *source,
1642+
const CBMLangSpec *spec, const char **out_map_path,
1643+
const char **out_method, const char **out_jax_path) {
1644+
*out_map_path = NULL;
1645+
*out_method = NULL;
1646+
*out_jax_path = NULL;
1647+
1648+
TSNode wrappers[2];
1649+
int wn = 0;
1650+
TSNode modifiers = find_jvm_modifiers(owner, spec->language);
16371651
if (!ts_node_is_null(modifiers)) {
1638-
uint32_t mc = ts_node_child_count(modifiers);
1639-
for (uint32_t mi = 0; mi < mc; mi++) {
1640-
TSNode mchild = ts_node_child(modifiers, mi);
1641-
if (cbm_kind_in_set(mchild, spec->decorator_node_types) &&
1642-
try_route_from_annotation(a, mchild, source, out_path, out_method)) {
1643-
return true;
1644-
}
1645-
}
1652+
wrappers[wn++] = modifiers;
16461653
}
16471654
/* Direct-child annotations (some grammars attach the annotation as a child
16481655
* of the method node rather than under `modifiers`). */
1649-
uint32_t cc = ts_node_child_count(func_node);
1650-
for (uint32_t ci = 0; ci < cc; ci++) {
1651-
TSNode child = ts_node_child(func_node, ci);
1652-
if (cbm_kind_in_set(child, spec->decorator_node_types) &&
1653-
try_route_from_annotation(a, child, source, out_path, out_method)) {
1654-
return true;
1656+
wrappers[wn++] = owner;
1657+
1658+
for (int w = 0; w < wn; w++) {
1659+
uint32_t cc = ts_node_child_count(wrappers[w]);
1660+
for (uint32_t ci = 0; ci < cc; ci++) {
1661+
TSNode child = ts_node_child(wrappers[w], ci);
1662+
if (!cbm_kind_in_set(child, spec->decorator_node_types)) {
1663+
continue;
1664+
}
1665+
TSNode name_node = annotation_name_node(child);
1666+
if (ts_node_is_null(name_node)) {
1667+
continue;
1668+
}
1669+
char *name = cbm_node_text(a, name_node, source);
1670+
if (!name) {
1671+
continue;
1672+
}
1673+
if (!*out_jax_path && strcmp(name, "Path") == 0) {
1674+
TSNode args = annotation_args_node(child);
1675+
if (!ts_node_is_null(args)) {
1676+
*out_jax_path = extract_route_path_from_args(a, args, source);
1677+
}
1678+
continue;
1679+
}
1680+
if (!*out_method) {
1681+
const char *method = annotation_route_method(name);
1682+
if (method) {
1683+
*out_method = method;
1684+
TSNode args = annotation_args_node(child);
1685+
if (!ts_node_is_null(args)) {
1686+
*out_map_path = extract_route_path_from_args(a, args, source);
1687+
}
1688+
}
1689+
}
16551690
}
16561691
}
1657-
return false;
1692+
}
1693+
1694+
static bool extract_route_from_annotations(CBMArena *a, TSNode func_node, const char *source,
1695+
const CBMLangSpec *spec, const char **out_path,
1696+
const char **out_method) {
1697+
const char *map_path = NULL;
1698+
const char *method = NULL;
1699+
const char *jax_path = NULL;
1700+
scan_route_annotations(a, func_node, source, spec, &map_path, &method, &jax_path);
1701+
/* Method-level routes still require a verb/mapping annotation; a lone
1702+
* @Path (JAX-RS sub-resource locator) is not an endpoint by itself. */
1703+
if (!method) {
1704+
return false;
1705+
}
1706+
*out_method = method;
1707+
*out_path = map_path ? map_path : (jax_path ? jax_path : "/");
1708+
return true;
16581709
}
16591710

16601711
static void extract_route_from_decorators(CBMArena *a, TSNode func_node, const char *source,
@@ -1723,10 +1774,18 @@ static const char *join_route_paths(CBMArena *a, const char *prefix, const char
17231774

17241775
static const char *spring_class_route_prefix(CBMArena *a, TSNode class_node, const char *source,
17251776
const CBMLangSpec *spec) {
1726-
const char *prefix = NULL;
1777+
const char *map_path = NULL;
17271778
const char *method = NULL;
1728-
if (extract_route_from_annotations(a, class_node, source, spec, &prefix, &method)) {
1729-
return prefix;
1779+
const char *jax_path = NULL;
1780+
scan_route_annotations(a, class_node, source, spec, &map_path, &method, &jax_path);
1781+
if (map_path) {
1782+
return map_path; /* @RequestMapping("/api") and friends */
1783+
}
1784+
if (jax_path) {
1785+
return jax_path; /* JAX-RS class-level @Path("/api") carries no verb */
1786+
}
1787+
if (method) {
1788+
return "/"; /* mapping annotation without a path argument */
17301789
}
17311790
return NULL;
17321791
}

‎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.
@@ -1488,7 +1513,7 @@ TEST(override_go_interface) {
14881513
* ══════════════════════════════════════════════════════════════════ */
14891514

14901515
SUITE(edge_types_probe) {
1491-
/* HANDLES — route→handler across web frameworks (8 frameworks) */
1516+
/* HANDLES — route→handler across web frameworks */
14921517
RUN_TEST(handles_flask_python);
14931518
RUN_TEST(handles_fastapi_python);
14941519
RUN_TEST(handles_drf_action_python);
@@ -1497,6 +1522,7 @@ SUITE(edge_types_probe) {
14971522
RUN_TEST(handles_gin_go);
14981523
RUN_TEST(handles_spring_java);
14991524
RUN_TEST(handles_spring_kotlin);
1525+
RUN_TEST(handles_jaxrs_java);
15001526
RUN_TEST(handles_aspnet_csharp);
15011527
RUN_TEST(handles_laravel_php);
15021528
RUN_TEST(handles_laravel_facade_routes_issue952);

‎tests/test_extraction.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,6 +2738,38 @@ TEST(extract_java_method_annotations_issue382) {
27382738
PASS();
27392739
}
27402740

2741+
/* Issue #1005: JAX-RS splits a route across two annotations (@GET carries the
2742+
* verb, a sibling @Path carries the path). Returning on the first mapping
2743+
* annotation dropped every method-level @Path, and the class-level @Path
2744+
* prefix was never recognized at all. */
2745+
TEST(extract_java_jaxrs_path_composition_issue1005) {
2746+
CBMFileResult *r = extract("import jakarta.ws.rs.GET;\n"
2747+
"import jakarta.ws.rs.Path;\n"
2748+
"@Path(\"/api/v1/widgets\")\n"
2749+
"public class WidgetResource {\n"
2750+
" @GET\n"
2751+
" public String list() { return \"\"; }\n"
2752+
" @GET\n"
2753+
" @Path(\"/count\")\n"
2754+
" public String count() { return \"\"; }\n"
2755+
"}\n",
2756+
CBM_LANG_JAVA, "t", "WidgetResource.java");
2757+
ASSERT_NOT_NULL(r);
2758+
ASSERT_FALSE(r->has_error);
2759+
const CBMDefinition *list = find_def_by_name(r, "list");
2760+
ASSERT_NOT_NULL(list);
2761+
ASSERT_NOT_NULL(list->route_path);
2762+
ASSERT_STR_EQ(list->route_path, "/api/v1/widgets");
2763+
ASSERT_STR_EQ(list->route_method, "GET");
2764+
const CBMDefinition *count = find_def_by_name(r, "count");
2765+
ASSERT_NOT_NULL(count);
2766+
ASSERT_NOT_NULL(count->route_path);
2767+
ASSERT_STR_EQ(count->route_path, "/api/v1/widgets/count");
2768+
ASSERT_STR_EQ(count->route_method, "GET");
2769+
cbm_free_result(r);
2770+
PASS();
2771+
}
2772+
27412773
/* Find an in-body call by its raw callee text; returns the call or NULL. */
27422774
static const CBMCall *find_call_by_callee(CBMFileResult *r, const char *callee) {
27432775
for (int i = 0; i < r->calls.count; i++) {
@@ -4625,6 +4657,7 @@ SUITE(extraction) {
46254657
RUN_TEST(js_index_module_qn_not_collide_with_folder);
46264658
RUN_TEST(python_regular_module_qn_unchanged);
46274659
RUN_TEST(extract_java_method_annotations_issue382);
4660+
RUN_TEST(extract_java_jaxrs_path_composition_issue1005);
46284661
RUN_TEST(extract_java_no_double_class_qn);
46294662
RUN_TEST(extract_go_no_filename_in_module_qn);
46304663
RUN_TEST(extract_large_ts_has_functions_issue213);

0 commit comments

Comments
 (0)