Skip to content

Commit 697b1c3

Browse files
authored
Error in RequestTemplate#uri when there're both query and fragment (#2367)
1 parent efe6356 commit 697b1c3

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

core/src/main/java/feign/RequestTemplate.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,12 @@ public RequestTemplate uri(String uri, boolean append) {
457457
uri = "/" + uri;
458458
}
459459

460+
int fragmentIndex = uri.indexOf('#');
461+
if (fragmentIndex > -1) {
462+
fragment = uri.substring(fragmentIndex);
463+
uri = uri.substring(0, fragmentIndex);
464+
}
465+
460466
/*
461467
* templates may provide query parameters. since we want to manage those explicity, we will need
462468
* to extract those out, leaving the uriTemplate with only the path to deal with.
@@ -472,12 +478,6 @@ public RequestTemplate uri(String uri, boolean append) {
472478
uri = uri.substring(0, queryMatcher.start());
473479
}
474480

475-
int fragmentIndex = uri.indexOf('#');
476-
if (fragmentIndex > -1) {
477-
fragment = uri.substring(fragmentIndex);
478-
uri = uri.substring(0, fragmentIndex);
479-
}
480-
481481
/* replace the uri template */
482482
if (append && this.uriTemplate != null) {
483483
this.uriTemplate = UriTemplate.append(this.uriTemplate, uri);

core/src/test/java/feign/RequestTemplateTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,16 @@ void fragmentShouldNotBeEncodedInTarget() {
462462
assertThat(template.url()).isEqualTo("https://example.com/path?key1=value1#fragment");
463463
}
464464

465+
@Test
466+
void fragmentShouldBeExtractedWhenQueryParamsExist() {
467+
RequestTemplate template =
468+
new RequestTemplate().method(HttpMethod.GET).uri("/path?query=queryValue#fragment",
469+
true);
470+
471+
assertThat(template.url()).isEqualTo("/path?query=queryValue#fragment");
472+
assertThat(template.queryLine()).isEqualTo("?query=queryValue");
473+
}
474+
465475
@Test
466476
void urlEncodingRemainsInPlace() {
467477
RequestTemplate template = new RequestTemplate().method(HttpMethod.GET)

0 commit comments

Comments
 (0)