Skip to content

Commit 4b0b5f2

Browse files
authored
Merge pull request DeusData#2158 from htarnacki/fix/jaxrs-relative-path
fix(routes): accept relative JAX-RS @path templates and compose Scala class-level @path (DeusData#2147)
2 parents 64c23fa + d772b1e commit 4b0b5f2

3 files changed

Lines changed: 198 additions & 13 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 27 additions & 13 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,41 @@ 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. An
1438+
* empty @Path("") means "the class path itself": leave it unset so the
1439+
* caller falls back exactly as it does for a method without @Path. */
1440+
return path[0] ? cbm_arena_sprintf(a, "/%s", path) : NULL;
14321441
}
14331442

14341443
static const char *find_route_path_literal(CBMArena *a, TSNode node, const char *source,
1435-
int max_depth) {
1444+
int max_depth, bool allow_relative) {
14361445
if (ts_node_is_null(node) || max_depth < 0) {
14371446
return NULL;
14381447
}
1439-
const char *path = route_path_from_string_node(a, node, source);
1448+
const char *path = route_path_from_string_node(a, node, source, allow_relative);
14401449
if (path || max_depth == 0) {
14411450
return path;
14421451
}
14431452
uint32_t nc = ts_node_named_child_count(node);
14441453
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);
1454+
path = find_route_path_literal(a, ts_node_named_child(node, i), source, max_depth - 1,
1455+
allow_relative);
14461456
if (path) {
14471457
return path;
14481458
}
14491459
}
14501460
return NULL;
14511461
}
14521462

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) {
1463+
// Extract route path from decorator arguments. Generic mappings keep only
1464+
// slash-prefixed strings; JAX-RS @Path additionally accepts relative templates.
1465+
static const char *extract_route_path_from_args(CBMArena *a, TSNode args, const char *source,
1466+
bool allow_relative) {
14551467
/* Every argument is checked. Java and Kotlin put no order on annotation
14561468
* attributes, so `path` can sit anywhere in the list. Stopping early left
14571469
* a real route unread and formed no Route node. Each argument's own
@@ -1464,7 +1476,8 @@ static const char *extract_route_path_from_args(CBMArena *a, TSNode args, const
14641476
* @GetMapping(path = {"/orders"})
14651477
* Walk a bounded subtree and keep the first string literal that is
14661478
* 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);
1479+
const char *path =
1480+
find_route_path_literal(a, arg, source, CBM_DESCENDANT_MAX_DEPTH, allow_relative);
14681481
if (path) {
14691482
return path;
14701483
}
@@ -1610,7 +1623,7 @@ static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char
16101623

16111624
TSNode args = find_decorator_args(dchild);
16121625
if (!ts_node_is_null(args)) {
1613-
const char *path = extract_route_path_from_args(a, args, source);
1626+
const char *path = extract_route_path_from_args(a, args, source, false);
16141627
if (path) {
16151628
*out_path = path;
16161629
*out_method = method;
@@ -1690,7 +1703,7 @@ static bool try_route_from_annotation(CBMArena *a, TSNode annotation, const char
16901703
TSNode args = annotation_args_node(annotation);
16911704
const char *path = NULL;
16921705
if (!ts_node_is_null(args)) {
1693-
path = extract_route_path_from_args(a, args, source);
1706+
path = extract_route_path_from_args(a, args, source, false);
16941707
}
16951708
*out_path = path ? path : "/";
16961709
*out_method = method;
@@ -1740,7 +1753,7 @@ static void scan_route_annotations(CBMArena *a, TSNode owner, const char *source
17401753
if (!*out_jax_path && strcmp(name, "Path") == 0) {
17411754
TSNode args = annotation_args_node(child);
17421755
if (!ts_node_is_null(args)) {
1743-
*out_jax_path = extract_route_path_from_args(a, args, source);
1756+
*out_jax_path = extract_route_path_from_args(a, args, source, true);
17441757
}
17451758
continue;
17461759
}
@@ -1750,7 +1763,7 @@ static void scan_route_annotations(CBMArena *a, TSNode owner, const char *source
17501763
*out_method = method;
17511764
TSNode args = annotation_args_node(child);
17521765
if (!ts_node_is_null(args)) {
1753-
*out_map_path = extract_route_path_from_args(a, args, source);
1766+
*out_map_path = extract_route_path_from_args(a, args, source, false);
17541767
}
17551768
}
17561769
}
@@ -5147,7 +5160,8 @@ static void push_method_def(CBMExtractCtx *ctx, TSNode child, TSNode class_node,
51475160

51485161
def.decorators = extract_decorators(a, child, ctx->source, ctx->language, spec);
51495162
extract_route_from_decorators(a, child, ctx->source, spec, &def.route_path, &def.route_method);
5150-
if (def.route_path && (ctx->language == CBM_LANG_JAVA || ctx->language == CBM_LANG_KOTLIN)) {
5163+
if (def.route_path && (ctx->language == CBM_LANG_JAVA || ctx->language == CBM_LANG_KOTLIN ||
5164+
ctx->language == CBM_LANG_SCALA)) {
51515165
const char *prefix = spring_class_route_prefix(a, class_node, ctx->source, spec);
51525166
def.route_path = join_route_paths(a, prefix, def.route_path);
51535167
}

‎tests/test_edge_types_probe.c‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,71 @@ 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+
531+
/* JAX-RS (Scala): class-level @Path must be composed with both an empty
532+
* method path and a non-empty one. Without the Scala branch in
533+
* push_method_def, unrelated resources collapse onto the global verb/root
534+
* Route node. */
535+
TEST(handles_jaxrs_scala) {
536+
static const char *routes[] = {"/billingAccount", "/billingAccount/{id}",
537+
"/billingAccount/{id}/attachment/{attachmentId}/content", NULL};
538+
static const EtFile f[] = {
539+
{"BillingAccountApiController.scala",
540+
"package com.example\n\n"
541+
"import jakarta.ws.rs.{GET, POST, Path}\n\n"
542+
"@Path(\"/billingAccount\")\nclass BillingAccountApiController {\n"
543+
" @POST\n @Path(\"\")\n"
544+
" def createBillingAccount(): String = \"created\"\n\n"
545+
" @GET\n @Path(\"{id}\")\n"
546+
" def retrieveBillingAccount(): String = \"account\"\n\n"
547+
" @GET\n @Path(\"{id}/attachment/{attachmentId}/content\")\n"
548+
" def retrieveAttachment(): String = \"attachment\"\n}\n"}};
549+
ASSERT_TRUE(et_edge_present(f, 1, "HANDLES", 3));
550+
ASSERT_TRUE(et_routes_exact(f, 1, routes));
551+
PASS();
552+
}
553+
489554
/* ASP.NET Minimal API (C#) — route registration via static MapGet/MapPost calls
490555
* with identifier handlers, under a Microsoft/AspNetCore path so the resolved
491556
* callee QN carries the "MapGet"/"Microsoft.AspNetCore" route-reg substrings.
@@ -1636,6 +1701,9 @@ SUITE(edge_types_probe) {
16361701
RUN_TEST(handles_spring_java_path_attribute_fourth);
16371702
RUN_TEST(handles_spring_kotlin);
16381703
RUN_TEST(handles_jaxrs_java);
1704+
RUN_TEST(handles_jaxrs_java_relative_path);
1705+
RUN_TEST(handles_spring_java_relative_string_not_route);
1706+
RUN_TEST(handles_jaxrs_scala);
16391707
RUN_TEST(handles_aspnet_csharp);
16401708
RUN_TEST(handles_laravel_php);
16411709
RUN_TEST(handles_laravel_facade_routes_issue952);

‎tests/test_extraction.c‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4390,6 +4390,106 @@ TEST(extract_java_jaxrs_path_composition_issue1005) {
43904390
PASS();
43914391
}
43924392

4393+
/* JAX-RS @Path values are URI templates relative to the enclosing resource;
4394+
* the leading slash is optional and ignored by the framework. Relative class
4395+
* and method templates must compose to the same rooted routes as the
4396+
* slash-prefixed spelling above. */
4397+
TEST(extract_java_jaxrs_relative_path_templates) {
4398+
CBMFileResult *r = extract("import jakarta.ws.rs.GET;\n"
4399+
"import jakarta.ws.rs.Path;\n"
4400+
"@Path(\"api/v1/widgets\")\n"
4401+
"public class WidgetResource {\n"
4402+
" @GET\n"
4403+
" public String list() { return \"\"; }\n"
4404+
" @GET\n"
4405+
" @Path(\"count\")\n"
4406+
" public String count() { return \"\"; }\n"
4407+
" @GET\n"
4408+
" @Path(\"{id}/tags\")\n"
4409+
" public String tags() { return \"\"; }\n"
4410+
"}\n",
4411+
CBM_LANG_JAVA, "t", "WidgetResource.java");
4412+
ASSERT_NOT_NULL(r);
4413+
ASSERT_FALSE(r->has_error);
4414+
const CBMDefinition *list = find_def_by_name(r, "list");
4415+
ASSERT_NOT_NULL(list);
4416+
ASSERT_NOT_NULL(list->route_path);
4417+
ASSERT_STR_EQ(list->route_path, "/api/v1/widgets");
4418+
ASSERT_STR_EQ(list->route_method, "GET");
4419+
const CBMDefinition *count = find_def_by_name(r, "count");
4420+
ASSERT_NOT_NULL(count);
4421+
ASSERT_NOT_NULL(count->route_path);
4422+
ASSERT_STR_EQ(count->route_path, "/api/v1/widgets/count");
4423+
ASSERT_STR_EQ(count->route_method, "GET");
4424+
const CBMDefinition *tags = find_def_by_name(r, "tags");
4425+
ASSERT_NOT_NULL(tags);
4426+
ASSERT_NOT_NULL(tags->route_path);
4427+
ASSERT_STR_EQ(tags->route_path, "/api/v1/widgets/{id}/tags");
4428+
ASSERT_STR_EQ(tags->route_method, "GET");
4429+
cbm_free_result(r);
4430+
PASS();
4431+
}
4432+
4433+
/* @Path("") is legal JAX-RS and means "the class path itself". Accepting
4434+
* relative templates must not turn it into an empty route path: the method
4435+
* has to behave exactly like one without @Path (fallback to "/", then
4436+
* class-level composition). The class without a class-level @Path is the
4437+
* binding case: an empty method path would otherwise drop the route. */
4438+
TEST(extract_java_jaxrs_empty_path_means_class_path) {
4439+
CBMFileResult *r = extract("import jakarta.ws.rs.GET;\n"
4440+
"import jakarta.ws.rs.Path;\n"
4441+
"@Path(\"api/v1/widgets\")\n"
4442+
"class WidgetResource {\n"
4443+
" @GET\n"
4444+
" @Path(\"\")\n"
4445+
" public String root() { return \"\"; }\n"
4446+
"}\n"
4447+
"class RootResource {\n"
4448+
" @GET\n"
4449+
" @Path(\"\")\n"
4450+
" public String index() { return \"\"; }\n"
4451+
"}\n",
4452+
CBM_LANG_JAVA, "t", "Resources.java");
4453+
ASSERT_NOT_NULL(r);
4454+
ASSERT_FALSE(r->has_error);
4455+
const CBMDefinition *root = find_def_by_name(r, "root");
4456+
ASSERT_NOT_NULL(root);
4457+
ASSERT_NOT_NULL(root->route_path);
4458+
ASSERT_STR_EQ(root->route_path, "/api/v1/widgets");
4459+
ASSERT_STR_EQ(root->route_method, "GET");
4460+
const CBMDefinition *index = find_def_by_name(r, "index");
4461+
ASSERT_NOT_NULL(index);
4462+
ASSERT_NOT_NULL(index->route_path);
4463+
ASSERT_STR_EQ(index->route_path, "/");
4464+
ASSERT_STR_EQ(index->route_method, "GET");
4465+
cbm_free_result(r);
4466+
PASS();
4467+
}
4468+
4469+
/* Negative control: relative templates are accepted for JAX-RS @Path only.
4470+
* A slash-less string on a non-JAX-RS mapping annotation must keep the
4471+
* previous behaviour (the literal is not read as a route path), so the
4472+
* relaxation cannot broaden route extraction for other frameworks. */
4473+
TEST(extract_java_spring_relative_string_not_route_path) {
4474+
CBMFileResult *r = extract("import org.springframework.web.bind.annotation.GetMapping;\n"
4475+
"import org.springframework.web.bind.annotation.RequestMapping;\n"
4476+
"@RequestMapping(\"api\")\n"
4477+
"public class OrderController {\n"
4478+
" @GetMapping(\"orders\")\n"
4479+
" public String listOrders() { return \"\"; }\n"
4480+
"}\n",
4481+
CBM_LANG_JAVA, "t", "OrderController.java");
4482+
ASSERT_NOT_NULL(r);
4483+
ASSERT_FALSE(r->has_error);
4484+
const CBMDefinition *list = find_def_by_name(r, "listOrders");
4485+
ASSERT_NOT_NULL(list);
4486+
ASSERT_NOT_NULL(list->route_path);
4487+
ASSERT_STR_EQ(list->route_path, "/");
4488+
ASSERT_STR_EQ(list->route_method, "GET");
4489+
cbm_free_result(r);
4490+
PASS();
4491+
}
4492+
43934493
/* Return the file's Module definition (extraction pushes it first), or NULL. */
43944494
static const CBMDefinition *find_module_def(CBMFileResult *r) {
43954495
for (int i = 0; i < r->defs.count; i++) {
@@ -8600,6 +8700,9 @@ SUITE(extraction) {
86008700
RUN_TEST(arkts_lazy_import);
86018701
RUN_TEST(arkts_ts_compat);
86028702
RUN_TEST(extract_java_jaxrs_path_composition_issue1005);
8703+
RUN_TEST(extract_java_jaxrs_relative_path_templates);
8704+
RUN_TEST(extract_java_jaxrs_empty_path_means_class_path);
8705+
RUN_TEST(extract_java_spring_relative_string_not_route_path);
86038706
RUN_TEST(extract_blazor_page_directive_routes_component);
86048707
RUN_TEST(extract_blazor_component_without_page_has_no_route);
86058708
RUN_TEST(extract_razor_page_directive_routes_cshtml_view);

0 commit comments

Comments
 (0)