Skip to content

Commit 84aaae8

Browse files
Jacob MeachamAdrian Cole
authored andcommitted
Fix regex support when there are multiple @path regexes
1 parent 3e5d29b commit 84aaae8

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎jaxrs/src/main/java/feign/jaxrs/JAXRSContract.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@ protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodA
8181
if (!methodAnnotationValue.startsWith("/") && !data.template().toString().endsWith("/")) {
8282
methodAnnotationValue = "/" + methodAnnotationValue;
8383
}
84-
int regexIndex = methodAnnotationValue.indexOf(":");
85-
if (methodAnnotationValue.indexOf("{") != -1 && regexIndex != -1) {
86-
// The method annotation includes a regex, which should be stripped to get the true path parameter name.
87-
methodAnnotationValue = methodAnnotationValue.substring(0, regexIndex) + "}";
88-
}
84+
// jax-rs allows whitespace around the param name, as well as an optional regex. The contract should
85+
// strip these out appropriately.
86+
methodAnnotationValue = methodAnnotationValue.replaceAll("\\{\\s*(.+?)\\s*(:.+?)?\\}", "\\{$1\\}");
8987
data.template().append(methodAnnotationValue);
9088
} else if (annotationType == Produces.class) {
9189
String[] serverProduces = ((Produces) methodAnnotation).value();

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,22 @@ public void emptyPathParam() throws Exception {
248248
PathOnType.class.getDeclaredMethod("emptyPathParam", String.class));
249249
}
250250

251+
@Test
252+
public void pathParamWithSpaces() throws Exception {
253+
assertThat(contract.parseAndValidatateMetadata(
254+
PathOnType.class.getDeclaredMethod("pathParamWithSpaces", String.class)).template())
255+
.hasUrl("/base/{param}");
256+
}
257+
251258
@Test
252259
public void regexPathOnMethod() throws Exception {
253260
assertThat(contract.parseAndValidatateMetadata(
254261
PathOnType.class.getDeclaredMethod("pathParamWithRegex", String.class)).template())
255262
.hasUrl("/base/regex/{param}");
263+
264+
assertThat(contract.parseAndValidatateMetadata(
265+
PathOnType.class.getDeclaredMethod("pathParamWithMultipleRegex", String.class, String.class)).template())
266+
.hasUrl("/base/regex/{param1}/{param2}");
256267
}
257268

258269
@Test
@@ -509,9 +520,17 @@ interface PathOnType {
509520
@Path("/{param}")
510521
Response emptyPathParam(@PathParam("") String empty);
511522

523+
@GET
524+
@Path("/{ param }")
525+
Response pathParamWithSpaces(@PathParam("param") String path);
526+
512527
@GET
513528
@Path("regex/{param:.+}")
514529
Response pathParamWithRegex(@PathParam("param") String path);
530+
531+
@GET
532+
@Path("regex/{param1:[0-9]*}/{ param2 : .+}")
533+
Response pathParamWithMultipleRegex(@PathParam("param1") String param1, @PathParam("param2") String param2);
515534
}
516535

517536
interface WithURIParam {

0 commit comments

Comments
 (0)