Skip to content

Commit 4853754

Browse files
authored
Error in RequestTemplate#uri when there're both query and fragment (OpenFeign#2367)
1 parent 05cc7ef commit 4853754

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

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

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

462+
int fragmentIndex = uri.indexOf('#');
463+
if (fragmentIndex > -1) {
464+
fragment = uri.substring(fragmentIndex);
465+
uri = uri.substring(0, fragmentIndex);
466+
}
467+
462468
/*
463469
* templates may provide query parameters. since we want to manage those explicity, we will need
464470
* to extract those out, leaving the uriTemplate with only the path to deal with.
@@ -474,12 +480,6 @@ public RequestTemplate uri(String uri, boolean append) {
474480
uri = uri.substring(0, queryMatcher.start());
475481
}
476482

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

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,15 @@ void fragmentShouldNotBeEncodedInTarget() {
524524
assertThat(template.url()).isEqualTo("https://example.com/path?key1=value1#fragment");
525525
}
526526

527+
@Test
528+
void fragmentShouldBeExtractedWhenQueryParamsExist() {
529+
RequestTemplate template =
530+
new RequestTemplate().method(HttpMethod.GET).uri("/path?query=queryValue#fragment", true);
531+
532+
assertThat(template.url()).isEqualTo("/path?query=queryValue#fragment");
533+
assertThat(template.queryLine()).isEqualTo("?query=queryValue");
534+
}
535+
527536
@Test
528537
void urlEncodingRemainsInPlace() {
529538
RequestTemplate template =

0 commit comments

Comments
 (0)