Skip to content

Commit ee54f39

Browse files
author
sheller
committed
Handle JAXRS Path annotation processes without slashes
1 parent 2c0c109 commit ee54f39

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

jaxrs/src/main/java/feign/jaxrs/JAXRSModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public MethodMetadata parseAndValidatateMetadata(Method method) {
5757
if (path != null) {
5858
String pathValue = emptyToNull(path.value());
5959
checkState(pathValue != null, "Path.value() was empty on type %s", method.getDeclaringClass().getName());
60+
if (!pathValue.startsWith("/")) {
61+
pathValue = "/" + pathValue;
62+
}
6063
md.template().insert(0, pathValue);
6164
}
6265
return md;
@@ -74,7 +77,11 @@ protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodA
7477
} else if (annotationType == Path.class) {
7578
String pathValue = emptyToNull(Path.class.cast(methodAnnotation).value());
7679
checkState(pathValue != null, "Path.value() was empty on method %s", method.getName());
77-
data.template().append(Path.class.cast(methodAnnotation).value());
80+
String methodAnnotationValue = Path.class.cast(methodAnnotation).value();
81+
if (!methodAnnotationValue.startsWith("/") && !data.template().toString().endsWith("/")) {
82+
methodAnnotationValue = "/" + methodAnnotationValue;
83+
}
84+
data.template().append(methodAnnotationValue);
7885
} else if (annotationType == Produces.class) {
7986
String[] serverProduces = ((Produces) methodAnnotation).value();
8087
String clientAccepts = serverProduces.length == 0 ? null: emptyToNull(serverProduces[0]);

jaxrs/src/test/java/feign/jaxrs/JAXRSContractTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,34 @@ interface HeaderParams {
342342
public void emptyHeaderParam() throws Exception {
343343
contract.parseAndValidatateMetadata(HeaderParams.class.getDeclaredMethod("emptyHeaderParam", String.class));
344344
}
345+
346+
@Path("base")
347+
interface PathsWithoutAnySlashes {
348+
@GET @Path("specific") Response get();
349+
}
350+
351+
@Test public void pathsWithoutSlashesParseCorrectly() throws Exception {
352+
MethodMetadata md = contract.parseAndValidatateMetadata(PathsWithoutAnySlashes.class.getDeclaredMethod("get"));
353+
assertEquals(md.template().url(), "/base/specific");
354+
}
355+
356+
@Path("/base")
357+
interface PathsWithSomeSlashes {
358+
@GET @Path("specific") Response get();
359+
}
360+
361+
@Test public void pathsWithSomeSlashesParseCorrectly() throws Exception {
362+
MethodMetadata md = contract.parseAndValidatateMetadata(PathsWithSomeSlashes.class.getDeclaredMethod("get"));
363+
assertEquals(md.template().url(), "/base/specific");
364+
}
365+
366+
@Path("base")
367+
interface PathsWithSomeOtherSlashes {
368+
@GET @Path("/specific") Response get();
369+
}
370+
371+
@Test public void pathsWithSomeOtherSlashesParseCorrectly() throws Exception {
372+
MethodMetadata md = contract.parseAndValidatateMetadata(PathsWithSomeOtherSlashes.class.getDeclaredMethod("get"));
373+
assertEquals(md.template().url(), "/base/specific");
374+
}
345375
}

0 commit comments

Comments
 (0)