Skip to content

Commit 246e419

Browse files
committed
fix(routes): keep @path("") unset instead of yielding an empty route path
Accepting relative JAX-RS templates made route_path_from_string_node return "" for @path(""), which is non-NULL and so replaced the "/" fallback with an empty method path. @path("") is legal JAX-RS and means "the class path itself"; on main the empty value was rejected and the method fell back to "/" like a method without @path. Return NULL for the empty case so that behaviour is unchanged. The regression covers both shapes: with a class-level @path the method composes to the class path, and without one the route stays "/" (with the empty string it was dropped entirely, because join_route_paths returns the NULL prefix for an empty path). Signed-off-by: Hubert Tarnacki <hubert.tarnacki@gmail.com>
1 parent 61cfb09 commit 246e419

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,8 +1434,10 @@ static const char *route_path_from_string_node(CBMArena *a, TSNode node, const c
14341434
}
14351435
/* JAX-RS @Path values are relative URI templates; a leading slash is
14361436
* 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;
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;
14391441
}
14401442

14411443
static const char *find_route_path_literal(CBMArena *a, TSNode node, const char *source,

‎tests/test_extraction.c‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4216,6 +4216,42 @@ TEST(extract_java_jaxrs_relative_path_templates) {
42164216
PASS();
42174217
}
42184218

4219+
/* @Path("") is legal JAX-RS and means "the class path itself". Accepting
4220+
* relative templates must not turn it into an empty route path: the method
4221+
* has to behave exactly like one without @Path (fallback to "/", then
4222+
* class-level composition). The class without a class-level @Path is the
4223+
* binding case: an empty method path would otherwise drop the route. */
4224+
TEST(extract_java_jaxrs_empty_path_means_class_path) {
4225+
CBMFileResult *r = extract("import jakarta.ws.rs.GET;\n"
4226+
"import jakarta.ws.rs.Path;\n"
4227+
"@Path(\"api/v1/widgets\")\n"
4228+
"class WidgetResource {\n"
4229+
" @GET\n"
4230+
" @Path(\"\")\n"
4231+
" public String root() { return \"\"; }\n"
4232+
"}\n"
4233+
"class RootResource {\n"
4234+
" @GET\n"
4235+
" @Path(\"\")\n"
4236+
" public String index() { return \"\"; }\n"
4237+
"}\n",
4238+
CBM_LANG_JAVA, "t", "Resources.java");
4239+
ASSERT_NOT_NULL(r);
4240+
ASSERT_FALSE(r->has_error);
4241+
const CBMDefinition *root = find_def_by_name(r, "root");
4242+
ASSERT_NOT_NULL(root);
4243+
ASSERT_NOT_NULL(root->route_path);
4244+
ASSERT_STR_EQ(root->route_path, "/api/v1/widgets");
4245+
ASSERT_STR_EQ(root->route_method, "GET");
4246+
const CBMDefinition *index = find_def_by_name(r, "index");
4247+
ASSERT_NOT_NULL(index);
4248+
ASSERT_NOT_NULL(index->route_path);
4249+
ASSERT_STR_EQ(index->route_path, "/");
4250+
ASSERT_STR_EQ(index->route_method, "GET");
4251+
cbm_free_result(r);
4252+
PASS();
4253+
}
4254+
42194255
/* Negative control: relative templates are accepted for JAX-RS @Path only.
42204256
* A slash-less string on a non-JAX-RS mapping annotation must keep the
42214257
* previous behaviour (the literal is not read as a route path), so the
@@ -8364,6 +8400,7 @@ SUITE(extraction) {
83648400
RUN_TEST(arkts_ts_compat);
83658401
RUN_TEST(extract_java_jaxrs_path_composition_issue1005);
83668402
RUN_TEST(extract_java_jaxrs_relative_path_templates);
8403+
RUN_TEST(extract_java_jaxrs_empty_path_means_class_path);
83678404
RUN_TEST(extract_java_spring_relative_string_not_route_path);
83688405
RUN_TEST(extract_blazor_page_directive_routes_component);
83698406
RUN_TEST(extract_blazor_component_without_page_has_no_route);

0 commit comments

Comments
 (0)