Skip to content

Commit 06d3b89

Browse files
committed
Jakarta REST client: do not produce '//' when base URI ends in '/'
RestInvocationHandler appends the resolved resource path after the base URI's path segments. A base URI ending in '/' has an empty last segment, so the append produced a double slash. Drop that trailing empty segment before appending, the same way ResourceIfaceScanner#join already does when joining a resource class's path with its method's. URIBuilder itself is unchanged: an empty path segment is meaningful per RFC 3986 and this is specific to how the REST client joins two already-resolved paths, not to path segment manipulation in general. Follow-up to apache/httpcomponents-core#745, which traced the bug to this exact join but put the fix in the wrong layer (URIBuilder).
1 parent c7e084f commit 06d3b89

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎httpclient5-jakarta-rest-client/src/main/java/org/apache/hc/client5/http/rest/RestInvocationHandler.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ private Object executeRequest(final ResourceMethod rm,
157157
pathSegments.add(pathTemplate.getSegment());
158158
}
159159
}
160+
if (!pathSegments.isEmpty()) {
161+
// A base URI ending in '/' (as many are written) has an empty last path
162+
// segment. Appending after it would produce '//'; drop it first, exactly
163+
// as ResourceIfaceScanner#join already does when joining a resource class's
164+
// path with its method's path.
165+
final List<String> baseSegments = uriBuilder.getPathSegments();
166+
if (!baseSegments.isEmpty() && baseSegments.get(baseSegments.size() - 1).isEmpty()) {
167+
uriBuilder.setPathSegments(baseSegments.subList(0, baseSegments.size() - 1));
168+
}
169+
}
160170
uriBuilder.appendPathSegments(pathSegments);
161171
uriBuilder.addParameters(queryParams);
162172
requestUri = uriBuilder.build();

‎httpclient5-jakarta-rest-client/src/test/java/org/apache/hc/client5/http/rest/RestClientIntegrationTest.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ void readsFormUrlEncodedResponseAsNameValuePairs() throws Exception {
160160
assertThat(pairs.get(1).getValue()).isEqualTo("en");
161161
}
162162

163+
@Test
164+
void getBook_withTrailingSlashBaseUri_doesNotProduceDoubleSlash() throws Exception {
165+
// A base URI ending in '/', as many are written, must not combine with the resource
166+
// path to produce '//books/...': the server only has a context registered at
167+
// "/books", so a leading double slash would not match it and the call would fail.
168+
final URI trailingSlashBaseUri = new URI(baseUri.toString() + "/");
169+
final BookResource client = RestClientBuilder.newBuilder()
170+
.baseUri(trailingSlashBaseUri)
171+
.httpClient(httpClient)
172+
.build(BookResource.class);
173+
174+
final Book fetched = client.getBook("abc-123", "en", "token-1");
175+
176+
assertThat(fetched).isNotNull();
177+
assertThat(fetched.id).isEqualTo("abc-123");
178+
assertThat(fetched.lang).isEqualTo("en");
179+
}
180+
163181
@Test
164182
void rejectsNonFormResponseForNameValuePairReturnType() {
165183
final BookResource client = RestClientBuilder.newBuilder()

0 commit comments

Comments
 (0)