Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,14 @@ public static String stripContextPath(ServerRequest request, String path) {
public static MultiValueMap<String, String> encodeQueryParams(MultiValueMap<String, String> params) {
MultiValueMap<String, String> encodedQueryParams = new LinkedMultiValueMap<>(params.size());
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
for (String value : entry.getValue()) {
encodedQueryParams.add(UriUtils.encode(entry.getKey(), StandardCharsets.UTF_8),
UriUtils.encode(value, StandardCharsets.UTF_8));
String key = UriUtils.encode(entry.getKey(), StandardCharsets.UTF_8);
if (CollectionUtils.isEmpty(entry.getValue())) {
encodedQueryParams.put(key, entry.getValue());
}
else {
for (String value : entry.getValue()) {
encodedQueryParams.add(key, UriUtils.encode(value, StandardCharsets.UTF_8));
}
}
}
return CollectionUtils.unmodifiableMultiValueMap(encodedQueryParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

import com.github.benmanes.caffeine.cache.Caffeine;
Expand Down Expand Up @@ -1023,6 +1024,44 @@ public void queryParamWithSpecialCharactersWorks() {
});
}

@Test
public void queryParamWithoutValueWorks() {
restClient.get()
.uri("/queryParamWithoutValue?myparam")
.exchange()
.expectStatus()
.isOk()
.expectBody(Map.class)
.consumeWith(result -> {
Map responseBody = result.getResponseBody();
assertThat(responseBody).containsKey("args");
Map args = getMap(responseBody, "args");
assertThat(args).containsKey("myparam");
assertThat(args.get("myparam")).isEqualTo("");
String url = (String) responseBody.get("url");
assertThat(url).endsWith("/get?myparam");
});
}

@Test
public void queryParamWithEmptyValueWorks() {
restClient.get()
.uri("/get?myparam=")
.exchange()
.expectStatus()
.isOk()
.expectBody(Map.class)
.consumeWith(result -> {
Map responseBody = result.getResponseBody();
assertThat(responseBody).containsKey("args");
Map args = getMap(responseBody, "args");
assertThat(args).containsKey("myparam");
assertThat(args.get("myparam")).isEqualTo("");
String url = (String) responseBody.get("url");
assertThat(url).endsWith("/get?myparam=");
});
}

@Test
public void clientResponseBodyAttributeWorks() {
restClient.get()
Expand Down Expand Up @@ -1690,6 +1729,30 @@ public RouterFunction<ServerResponse> gatewayRouterFunctionsReadResponseBody() {
// @formatter:on
}

@Bean
public RouterFunction<ServerResponse> gatewayRouterFunctionsEmptyQueryParamFilter() {
Function<ServerRequest, ServerRequest> transformQueryParamWithoutValue = request -> ServerRequest
.from(request)
.params(params -> {
// transform any query parameter with empty string value to not have
// any value at all
for (var param : params.entrySet()) {
if (param.getValue().size() == 1 && param.getValue().get(0).isEmpty()) {
params.put(param.getKey(), Collections.emptyList());
}
}
})
.build();
// @formatter:off
return route("testQueryParamWithoutValue")
.GET("/queryParamWithoutValue", http())
.before(transformQueryParamWithoutValue)
.filter(new HttpbinUriResolver())
.filter(setPath("/get"))
.build();
// @formatter:on
}

@Bean
public FilterRegistrationBean myFilter() {
FilterRegistrationBean<MyFilter> reg = new FilterRegistrationBean<>(new MyFilter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
package org.springframework.cloud.gateway.server.mvc.common;

import java.util.Collections;
import java.util.Map;

import org.junit.jupiter.api.Test;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.util.MultiValueMap;
import org.springframework.web.servlet.function.ServerRequest;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -62,4 +64,11 @@ void stripContextPathNoOpWithoutContextPath() {
assertThat(MvcUtils.stripContextPath(request, "/path")).isEqualTo("/path");
}

@Test
void encodeQueryParamsKeepsParamWithoutValue() {
var queryParams = MultiValueMap.<String, String>fromMultiValue(Map.of("test", Collections.emptyList()));
var encoded = MvcUtils.encodeQueryParams(queryParams);
assertThat(encoded).isEqualTo(queryParams);
}

}