Skip to content

Commit b27da0d

Browse files
authored
Preserve exact variable template pattern to reconstruct URI templates later on (#3019)
1 parent eed5de6 commit b27da0d

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ private static final class Variable extends UriChunk {
471471
private static final Pattern VARIABLE_PATTERN = Pattern.compile("(\\w[-\\w\\.]*[ ]*)(\\:(.+))?");
472472
private String name;
473473
private Pattern pattern;
474+
private String template;
474475

475476
private Variable() {
476477
// empty constructor
@@ -488,14 +489,16 @@ public static Variable create(String uriChunk) {
488489
return null;
489490
}
490491
if (CurlyBraceTokenizer.insideBraces(uriChunk)) {
491-
uriChunk = CurlyBraceTokenizer.stripBraces(uriChunk).trim();
492-
Matcher matcher = VARIABLE_PATTERN.matcher(uriChunk);
492+
String trimmedUriChunk = CurlyBraceTokenizer.stripBraces(uriChunk).trim();
493+
Matcher matcher = VARIABLE_PATTERN.matcher(trimmedUriChunk);
493494
if (matcher.matches()) {
494495
newVariable.name = matcher.group(1).trim();
495496
if (matcher.group(2) != null && matcher.group(3) != null) {
496497
String patternExpression = matcher.group(3).trim();
497498
newVariable.pattern = Pattern.compile(patternExpression);
498499
}
500+
// Store the exact variable template
501+
newVariable.template = uriChunk.trim();
499502
return newVariable;
500503
}
501504
}
@@ -526,10 +529,7 @@ public boolean matches(String value) {
526529

527530
@Override
528531
public String getValue() {
529-
if (pattern != null) {
530-
return "{" + name + ":" + pattern + "}";
531-
}
532-
return "{" + name + "}";
532+
return template;
533533
}
534534
}
535535

rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriInfoImplTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ public Response getTemplate() {
436436
return null;
437437
}
438438

439+
@GET
440+
@Path("one/{name: [a-zA-Z][a-zA-Z_0-9]*}")
441+
public Response getTemplateSpaces() {
442+
return null;
443+
}
444+
439445
@GET
440446
@Path("bar")
441447
public Response getSubMethod() {
@@ -584,6 +590,21 @@ public void testGetMatchedResourceTemplateIncludesApplicationPathAndTemplateVari
584590
assertEquals("/foo/one/{name:[a-zA-Z][a-zA-Z_0-9]*}", u.getMatchedResourceTemplate());
585591
}
586592

593+
@Test
594+
public void testGetMatchedResourceTemplatePreserveSpacesInTemplateVariables() throws Exception {
595+
Message m = mockMessage("http://localhost:8080/app", "/foo/one/abc");
596+
OperationResourceInfoStack oriStack = new OperationResourceInfoStack();
597+
ClassResourceInfo cri = getCri(RootResource.class, true);
598+
OperationResourceInfo ori = getOri(cri, "getTemplateSpaces");
599+
600+
MethodInvocationInfo miInfo = new MethodInvocationInfo(ori, RootResource.class, new ArrayList<String>());
601+
oriStack.push(miInfo);
602+
m.put(OperationResourceInfoStack.class, oriStack);
603+
604+
UriInfoImpl u = new UriInfoImpl(m);
605+
assertEquals("/foo/one/{name: [a-zA-Z][a-zA-Z_0-9]*}", u.getMatchedResourceTemplate());
606+
}
607+
587608
@Test
588609
public void testGetMatchedResourceTemplateIgnoresPathBeforeApplicationPath() throws Exception {
589610
Message m = mockMessage("http://localhost:8080/context/service", "/foo/bar");

0 commit comments

Comments
 (0)