Skip to content

Commit 715bc97

Browse files
committed
support resource methods without Path annotation in rest client
1 parent a2494d6 commit 715bc97

5 files changed

Lines changed: 47 additions & 4 deletions

File tree

dropwizard-guicey/src/main/java/ru/vyarus/dropwizard/guice/url/resource/ResourceAnalyzer.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ public static String getResourcePath(final Class<?> resource) {
8989
* @throws java.lang.IllegalStateException if annotation not found
9090
*/
9191
public static String getMethodPath(final Method method) {
92+
final Path path = method.getAnnotation(Path.class);
93+
if (path == null) {
94+
// method might miss path annotation if the path is the same as resource path
95+
return "/";
96+
}
9297
return PathUtils.normalizeAbsolutePath(findAnnotatedMethod(method).getAnnotation(Path.class).value());
9398
}
9499

@@ -192,18 +197,18 @@ public static Method findMethod(final Class<?> resource, final String method) {
192197
*/
193198
public static Method findAnnotatedMethod(final Method method) {
194199
Method res = null;
195-
// searching for annotated method
196-
if (method.getAnnotation(Path.class) == null) {
200+
// searching for annotated method (could be Path or method annotation)
201+
if (!isJerseyAnnotated(method)) {
197202
// try to search in superclasses and interfaces (for declaring class!)
198203
for (Class<?> type : GenericsResolver.resolve(method.getDeclaringClass())
199204
.getGenericsInfo().getComposingTypes()) {
200205
for (Method cand : type.getDeclaredMethods()) {
201206
// searching same method, but annotated
202207
if (cand.getName().equals(method.getName())
203208
&& cand.getParameterTypes().length == method.getParameterTypes().length
204-
&& cand.getAnnotation(Path.class) != null
205209
// not count possible type differences
206-
&& Arrays.equals(cand.getParameterTypes(), method.getParameterTypes())) {
210+
&& Arrays.equals(cand.getParameterTypes(), method.getParameterTypes())
211+
&& isJerseyAnnotated(cand)) {
207212
res = cand;
208213
break;
209214
}
@@ -220,6 +225,21 @@ public static Method findAnnotatedMethod(final Method method) {
220225
return res;
221226
}
222227

228+
/**
229+
* Check if provided method is annotated with jersey annotations. Http methods must have http method
230+
* annotation (like {@link jakarta.ws.rs.GET}), but may lack {@link jakarta.ws.rs.Path} annotation.
231+
* Sub-resource lookup method must have {@link Path annotation}. So target method must be checked to contain
232+
* one of possible annotations.
233+
* <p>
234+
* Note that it is impossible to have Path and http method annotation on different methods (jersey requirement).
235+
*
236+
* @param method method to check
237+
* @return true if method contains jersey annotations
238+
*/
239+
public static boolean isJerseyAnnotated(final Method method) {
240+
return method.getAnnotation(Path.class) != null || getHttpMethod(method).isPresent();
241+
}
242+
223243
/**
224244
* Resolve http method by searching for method annotations like {@link jakarta.ws.rs.GET} or
225245
* {@link jakarta.ws.rs.POST}.

dropwizard-guicey/src/test/groovy/ru/vyarus/dropwizard/guice/url/resource/ResourceAnalyzerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ void testMethodPath() throws Exception {
4646

4747
assertThat(ResourceAnalyzer.getMethodPath(DirectResource.class.getMethod("get", MappedBean.class)))
4848
.isEqualTo("/{sm}/2");
49+
50+
// NO @Path on method
51+
assertThat(ResourceAnalyzer.getMethodPath(DirectResource.class, "nopath")).isEqualTo("/");
52+
assertThat(ResourceAnalyzer.getMethodPath(InterfaceResource.class, "nopath")).isEqualTo("/");
4953
}
5054

5155
@Test
@@ -106,6 +110,12 @@ void testMethodCallAnalysis() throws Exception {
106110
assertThat(info.getHeaderParams()).isEmpty();
107111
assertThat(info.getCookieParams()).isEmpty();
108112
assertThat(info.getMatrixParams()).isEmpty();
113+
114+
// WHEN method without path annotation
115+
info = ResourceAnalyzer
116+
.analyzeMethodCall(DirectResource.class, DirectResource::nopath);
117+
assertThat(info.getHttpMethod()).isEqualTo("GET");
118+
assertThat(info.getPath()).isEqualTo("/");
109119
}
110120

111121
@Test

dropwizard-guicey/src/test/groovy/ru/vyarus/dropwizard/guice/url/resource/support/DirectResource.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public Response get(@PathParam("sm") String sm,
3939
return Response.ok().build();
4040
}
4141

42+
@GET
43+
public String nopath() {
44+
return "nopath";
45+
}
46+
4247
@GET
4348
@Path("/{sm}/2")
4449
public Response get(@BeanParam MappedBean bean) {

dropwizard-guicey/src/test/groovy/ru/vyarus/dropwizard/guice/url/resource/support/InterfaceResource.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public Response get(String sm,
2020
return Response.ok().build();
2121
}
2222

23+
@Override
24+
public String nopath() {
25+
return "nopath";
26+
}
27+
2328
@Override
2429
public Response get(MappedBean bean) {
2530
return Response.ok().build();

dropwizard-guicey/src/test/groovy/ru/vyarus/dropwizard/guice/url/resource/support/ResourceDeclaration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Response get(@PathParam("sm") String sm,
3232
@HeaderParam("HH") String hh,
3333
@CookieParam("cc") String cc);
3434

35+
@GET
36+
String nopath();
37+
3538
@Path("/{sm}/2")
3639
Response get(MappedBean bean);
3740

0 commit comments

Comments
 (0)