Skip to content

Commit a848520

Browse files
author
NiMv1
committed
Add support for empty path prefixes
Signed-off-by: NiMv1 <[email protected]>
1 parent 9689187 commit a848520

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

spring-cloud-gateway-server-webflux/src/main/java/org/springframework/cloud/gateway/filter/factory/PrefixPathGatewayFilterFactory.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public List<String> shortcutFieldOrder() {
6464
@Override
6565
public GatewayFilter apply(Config config) {
6666
return new GatewayFilter() {
67-
final UriTemplate uriTemplate = new UriTemplate(
68-
Objects.requireNonNull(config.prefix, "prefix must not be null"));
67+
final String prefix = config.prefix != null ? config.prefix : "";
68+
final UriTemplate uriTemplate = prefix.isEmpty() ? null : new UriTemplate(prefix);
6969

7070
@Override
7171
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
@@ -78,11 +78,16 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
7878
ServerHttpRequest req = exchange.getRequest();
7979
addOriginalRequestUrl(exchange, req.getURI());
8080

81-
Map<String, String> uriVariables = getUriTemplateVariables(exchange);
82-
URI uri = uriTemplate.expand(uriVariables);
83-
84-
String newPath = uri.getRawPath() + req.getURI().getRawPath();
85-
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri);
81+
String newPath;
82+
if (uriTemplate != null) {
83+
Map<String, String> uriVariables = getUriTemplateVariables(exchange);
84+
URI uri = uriTemplate.expand(uriVariables);
85+
newPath = uri.getRawPath() + req.getURI().getRawPath();
86+
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri);
87+
}
88+
else {
89+
newPath = req.getURI().getRawPath();
90+
}
8691
ServerHttpRequest request = req.mutate().path(newPath).build();
8792

8893
if (log.isTraceEnabled()) {

0 commit comments

Comments
 (0)