Skip to content

Commit 004c46b

Browse files
committed
#1727 - Prevent premature base encoding in UriTemplate if host is templated.
1 parent 809b5ca commit 004c46b

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/main/java/org/springframework/hateoas/UriTemplate.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,14 @@ private static String prepareTemplate(String template, int index) {
357357

358358
String head = index == -1 ? template : template.substring(0, index);
359359
String tail = index == -1 ? "" : template.substring(index);
360-
String encodedBase = UriComponentsBuilder.fromUriString(head)
361-
.encode()
362-
.build()
363-
.toUriString();
360+
361+
// Encode head if it's more than just the scheme
362+
String encodedBase = head.endsWith("://") && tail.startsWith("{")
363+
? head
364+
: UriComponentsBuilder.fromUriString(head)
365+
.encode()
366+
.build()
367+
.toUriString();
364368

365369
head = encodedBase.length() > head.length() ? encodedBase : head;
366370

src/test/java/org/springframework/hateoas/UriTemplateUnitTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,13 @@ void adaptsRequestParamVariableToContinuationIfBaseUriContainsParameter() {
375375
assertThat(template.toString()).isEqualTo("/path/{bar}/foo.zip?type=foo{&foobar}");
376376
}
377377

378+
@Test // #1727
379+
void supportsVariableInHostName() {
380+
381+
assertThatCode(() -> UriTemplate.of("https://{somehost}/somepath"))
382+
.doesNotThrowAnyException();
383+
}
384+
378385
private static void assertVariables(UriTemplate template, TemplateVariable... variables) {
379386
assertVariables(template, Arrays.asList(variables));
380387
}

0 commit comments

Comments
 (0)