Skip to content

Commit 0c02443

Browse files
committed
Consistently support no value params in query string
Closes gh-35329
1 parent 268706a commit 0c02443

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/request/AbstractMockHttpServletRequestBuilder.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -911,10 +911,12 @@ private void updatePathRequestProperties(MockHttpServletRequest request, String
911911
}
912912

913913
private void addRequestParams(MockHttpServletRequest request, MultiValueMap<String, String> map) {
914-
map.forEach((key, values) -> values.forEach(value -> {
915-
value = (value != null ? UriUtils.decode(value, StandardCharsets.UTF_8) : null);
916-
request.addParameter(UriUtils.decode(key, StandardCharsets.UTF_8), value);
917-
}));
914+
map.forEach((key, values) ->
915+
request.addParameter(
916+
UriUtils.decode(key, StandardCharsets.UTF_8),
917+
values.stream()
918+
.map(value -> value != null ? UriUtils.decode(value, StandardCharsets.UTF_8) : null)
919+
.toArray(String[]::new)));
918920
}
919921

920922
private byte[] writeFormData(MediaType mediaType, Charset charset) {

spring-test/src/test/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilderTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,15 @@ void queryParameterWithoutValues() {
311311
assertThat(request.getParameterMap().get("foo")).containsExactly();
312312
}
313313

314+
@Test // gh-35210
315+
void queryStringWithoutValues() {
316+
this.builder = new MockHttpServletRequestBuilder(GET).uri("/path?foo");
317+
MockHttpServletRequest request = this.builder.buildRequest(this.servletContext);
318+
319+
assertThat(request.getQueryString()).isEqualTo("foo");
320+
assertThat(request.getParameterMap().get("foo")).containsExactly((String) null);
321+
}
322+
314323
@Test
315324
void formField() {
316325
this.builder = new MockHttpServletRequestBuilder(POST).uri("/");

0 commit comments

Comments
 (0)