Skip to content

Commit b9509d3

Browse files
committed
Initialize RequestPath on demand
Closes gh-33227
1 parent 30a64d6 commit b9509d3

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

spring-web/src/main/java/org/springframework/http/server/reactive/AbstractServerHttpRequest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,7 +50,11 @@ public abstract class AbstractServerHttpRequest implements ServerHttpRequest {
5050

5151
private final URI uri;
5252

53-
private final RequestPath path;
53+
@Nullable
54+
private final String contextPath;
55+
56+
@Nullable
57+
private RequestPath path;
5458

5559
private final HttpHeaders headers;
5660

@@ -92,7 +96,7 @@ public AbstractServerHttpRequest(HttpMethod method, URI uri, @Nullable String co
9296

9397
this.method = method;
9498
this.uri = uri;
95-
this.path = RequestPath.parse(uri, contextPath);
99+
this.contextPath = contextPath;
96100
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
97101
}
98102

@@ -140,6 +144,9 @@ public Map<String, Object> getAttributes() {
140144

141145
@Override
142146
public RequestPath getPath() {
147+
if (this.path == null) {
148+
this.path = RequestPath.parse(this.uri, this.contextPath);
149+
}
143150
return this.path;
144151
}
145152

spring-web/src/test/java/org/springframework/http/server/reactive/ServerHttpRequestTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ void mutateWithExistingContextPath() throws Exception {
198198
void mutateContextPathWithoutUpdatingPathShouldFail() throws Exception {
199199
ServerHttpRequest request = createRequest("/context/path", "/context");
200200

201-
assertThatThrownBy(() -> request.mutate().contextPath("/fail").build())
201+
assertThatThrownBy(() -> request.mutate().contextPath("/fail").build().getPath())
202202
.isInstanceOf(IllegalArgumentException.class)
203203
.hasMessage("Invalid contextPath '/fail': must match the start of requestPath: '/context/path'");
204204
}

0 commit comments

Comments
 (0)