Allow null contextPath in ServerHttpRequest.Builder

The builder method required a non-null contextPath while the underlying
field, MutatedServerHttpRequest constructor, and RequestPath.parse all
accept null and treat it the same as an empty string. Relax the method
parameter to @Nullable so callers can clear the context path directly.

Closes gh-37099

Signed-off-by: Garvit Joshi <garvitjoshi9@gmail.com>
This commit is contained in:
Garvit Joshi
2026-08-24 17:18:17 +01:00
committed by rstoyanchev
parent 82cf15c60f
commit 8d4208f030
3 changed files with 14 additions and 2 deletions
@@ -103,7 +103,7 @@ class DefaultServerHttpRequestBuilder implements ServerHttpRequest.Builder {
}
@Override
public ServerHttpRequest.Builder contextPath(String contextPath) {
public ServerHttpRequest.Builder contextPath(@Nullable String contextPath) {
this.contextPath = contextPath;
return this;
}
@@ -146,8 +146,10 @@ public interface ServerHttpRequest extends HttpRequest, ReactiveHttpInputMessage
* contextPath} and it must match the start of the path of the URI of
* the request. That means changing the contextPath, implies also
* changing the path via {@link #path(String)}.
* <p>The given value may be {@code null} or empty to indicate there
* is no context path.
*/
Builder contextPath(String contextPath);
Builder contextPath(@Nullable String contextPath);
/**
* Set or override the specified header values under the given name.
@@ -194,6 +194,16 @@ class ServerHttpRequestTests {
assertThat(mutated.getURI().getRawPath()).isEqualTo("/other/path");
}
@Test
void mutateContextPathToNull() throws Exception {
ServerHttpRequest request = createRequest("/context/path", "/context");
ServerHttpRequest mutated = request.mutate().contextPath(null).build();
assertThat(mutated.getPath().contextPath().value()).isEmpty();
assertThat(mutated.getPath().pathWithinApplication().value()).isEqualTo("/context/path");
assertThat(mutated.getURI().getRawPath()).isEqualTo("/context/path");
}
@Test
void mutateContextPathWithoutUpdatingPathShouldFail() throws Exception {
ServerHttpRequest request = createRequest("/context/path", "/context");