Skip to content

Commit

Permalink
Fix UriPathEncoder bug to improve performance
Browse files Browse the repository at this point in the history
Fix `isAllowed` check and write test to ensure that additional object
instances are not created unnecessarily.

See spring-projectsgh-40615
  • Loading branch information
philwebb committed May 3, 2024
1 parent d0ce4da commit 8a72e55
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private UriPathEncoder() {
static String encode(String path) {
byte[] bytes = path.getBytes(StandardCharsets.UTF_8);
for (byte b : bytes) {
if (isAllowed(b)) {
if (!isAllowed(b)) {
return encode(bytes);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ void encodePath() {
assertThat(UriPathEncoder.encode("/Z\u00fcrich")).isEqualTo("/Z%C3%BCrich");
}

@Test
void encodePathWhenNoEncodingIsRequiredReturnsSameInstance() {
String path = "/foo/bar";
assertThat(UriPathEncoder.encode(path)).isSameAs(path);
}

}

0 comments on commit 8a72e55

Please sign in to comment.