From ee54f394c4262fd0b56e149cd21839092c770a95 Mon Sep 17 00:00:00 2001 From: sheller Date: Wed, 4 Jun 2014 14:23:40 -0400 Subject: [PATCH] Handle JAXRS Path annotation processes without slashes --- .../main/java/feign/jaxrs/JAXRSModule.java | 9 +++++- .../java/feign/jaxrs/JAXRSContractTest.java | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/jaxrs/src/main/java/feign/jaxrs/JAXRSModule.java b/jaxrs/src/main/java/feign/jaxrs/JAXRSModule.java index dc84c8e2..1560058f 100644 --- a/jaxrs/src/main/java/feign/jaxrs/JAXRSModule.java +++ b/jaxrs/src/main/java/feign/jaxrs/JAXRSModule.java @@ -57,6 +57,9 @@ public final class JAXRSModule { if (path != null) { String pathValue = emptyToNull(path.value()); checkState(pathValue != null, "Path.value() was empty on type %s", method.getDeclaringClass().getName()); + if (!pathValue.startsWith("/")) { + pathValue = "/" + pathValue; + } md.template().insert(0, pathValue); } return md; @@ -74,7 +77,11 @@ public final class JAXRSModule { } else if (annotationType == Path.class) { String pathValue = emptyToNull(Path.class.cast(methodAnnotation).value()); checkState(pathValue != null, "Path.value() was empty on method %s", method.getName()); - data.template().append(Path.class.cast(methodAnnotation).value()); + String methodAnnotationValue = Path.class.cast(methodAnnotation).value(); + if (!methodAnnotationValue.startsWith("/") && !data.template().toString().endsWith("/")) { + methodAnnotationValue = "/" + methodAnnotationValue; + } + data.template().append(methodAnnotationValue); } else if (annotationType == Produces.class) { String[] serverProduces = ((Produces) methodAnnotation).value(); String clientAccepts = serverProduces.length == 0 ? null: emptyToNull(serverProduces[0]); diff --git a/jaxrs/src/test/java/feign/jaxrs/JAXRSContractTest.java b/jaxrs/src/test/java/feign/jaxrs/JAXRSContractTest.java index 7a573e00..9a16e6c9 100644 --- a/jaxrs/src/test/java/feign/jaxrs/JAXRSContractTest.java +++ b/jaxrs/src/test/java/feign/jaxrs/JAXRSContractTest.java @@ -342,4 +342,34 @@ public class JAXRSContractTest { public void emptyHeaderParam() throws Exception { contract.parseAndValidatateMetadata(HeaderParams.class.getDeclaredMethod("emptyHeaderParam", String.class)); } + + @Path("base") + interface PathsWithoutAnySlashes { + @GET @Path("specific") Response get(); + } + + @Test public void pathsWithoutSlashesParseCorrectly() throws Exception { + MethodMetadata md = contract.parseAndValidatateMetadata(PathsWithoutAnySlashes.class.getDeclaredMethod("get")); + assertEquals(md.template().url(), "/base/specific"); + } + + @Path("/base") + interface PathsWithSomeSlashes { + @GET @Path("specific") Response get(); + } + + @Test public void pathsWithSomeSlashesParseCorrectly() throws Exception { + MethodMetadata md = contract.parseAndValidatateMetadata(PathsWithSomeSlashes.class.getDeclaredMethod("get")); + assertEquals(md.template().url(), "/base/specific"); + } + + @Path("base") + interface PathsWithSomeOtherSlashes { + @GET @Path("/specific") Response get(); + } + + @Test public void pathsWithSomeOtherSlashesParseCorrectly() throws Exception { + MethodMetadata md = contract.parseAndValidatateMetadata(PathsWithSomeOtherSlashes.class.getDeclaredMethod("get")); + assertEquals(md.template().url(), "/base/specific"); + } }