diff --git a/org.springframework.web/src/main/java/org/springframework/web/util/UriComponents.java b/org.springframework.web/src/main/java/org/springframework/web/util/UriComponents.java index d1a396d130..114ef85bd1 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/util/UriComponents.java +++ b/org.springframework.web/src/main/java/org/springframework/web/util/UriComponents.java @@ -336,6 +336,8 @@ public final class UriComponents { } private UriComponents expandInternal(UriTemplateVariables uriVariables) { + Assert.state(!encoded, "Cannot expand an already encoded UriComponents object"); + String expandedScheme = expandUriComponent(this.scheme, uriVariables); String expandedUserInfo = expandUriComponent(this.userInfo, uriVariables); String expandedHost = expandUriComponent(this.host, uriVariables); diff --git a/org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsTests.java b/org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsTests.java index a9824caec0..b845432676 100644 --- a/org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsTests.java +++ b/org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsTests.java @@ -54,4 +54,9 @@ public class UriComponentsTests { assertEquals("http://example.com/1 2 3 4", uriComponents.toUriString()); } + @Test(expected = IllegalStateException.class) + public void expandEncoded() { + UriComponentsBuilder.fromPath("/{foo}").build().encode().expand("bar"); + } + }