Skip to content

Commit 3607f98

Browse files
committed
Refine null handling in UriComponentsBuilder#query
Closes gh-35629
1 parent 649bd3e commit 3607f98

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

spring-web/src/main/java/org/springframework/web/util/UriBuilder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,10 @@ public interface UriBuilder {
144144
UriBuilder pathSegment(String... pathSegments) throws IllegalArgumentException;
145145

146146
/**
147-
* Parse the given query string into query parameters where parameters are
148-
* separated with {@code '&'} and their values, if any, with {@code '='}.
149-
* The query may contain URI template variables.
147+
* Parse the given query string into query parameters, and append them to
148+
* the query string. Query parameters are separated with {@code '&'} while
149+
* their values, if any, are separated with {@code '='}. The query string
150+
* may contain URI template variables.
150151
* <p><strong>Note: </strong> please, review the Javadoc of
151152
* {@link #queryParam(String, Object...)} for further notes on the treatment
152153
* and encoding of individual query parameters.

spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,9 @@ private UriComponentsBuilder rfcUriRecord(RfcUriParser.UriRecord record) {
426426
if (record.path() != null) {
427427
path(record.path());
428428
}
429-
query(record.query());
429+
if (record.query() != null) {
430+
query(record.query());
431+
}
430432
}
431433
fragment(record.fragment());
432434
return this;
@@ -453,7 +455,9 @@ private UriComponentsBuilder whatWgUrlRecord(WhatWgUrlParser.UrlRecord record) {
453455
port(record.portString());
454456
}
455457
path(record.path().toString());
456-
query(record.query());
458+
if (record.query() != null) {
459+
query(record.query());
460+
}
457461
}
458462
if (StringUtils.hasText(record.fragment())) {
459463
fragment(record.fragment());
@@ -541,8 +545,8 @@ public UriComponentsBuilder replacePath(@Nullable String path) {
541545
}
542546

543547
@Override
544-
public UriComponentsBuilder query(@Nullable String query) {
545-
if (query != null) {
548+
public UriComponentsBuilder query(String query) {
549+
if (StringUtils.hasText(query)) {
546550
Matcher matcher = QUERY_PARAM_PATTERN.matcher(query);
547551
while (matcher.find()) {
548552
String name = matcher.group(1);

spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -479,20 +479,18 @@ void replacePath(ParserType parserType) {
479479

480480
@ParameterizedTest
481481
@EnumSource
482-
void query(final ParserType parserType) {
483-
final UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://example.com/foo?foo=bar", parserType)
484-
.query("baz=qux")
485-
.build();
486-
assertThat(uriComponents.getQueryParams()).isEqualTo(Map.of("foo", List.of("bar"), "baz", List.of("qux")));
482+
void query(ParserType parserType) {
483+
String url = "https://example.com/foo?foo=bar";
484+
UriComponents uric = UriComponentsBuilder.fromUriString(url, parserType).query("baz=qux").build();
485+
assertThat(uric.getQueryParams()).isEqualTo(Map.of("foo", List.of("bar"), "baz", List.of("qux")));
487486
}
488487

489488
@ParameterizedTest
490-
@EnumSource
491-
void queryWithNullDoesRetainQueryParameters(final ParserType parserType) {
492-
final UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://example.com/foo?foo=bar", parserType)
493-
.query(null)
494-
.build();
495-
assertThat(uriComponents.getQueryParams()).isEqualTo(Map.of("foo", List.of("bar")));
489+
@EnumSource // gh-35628
490+
void queryWithNull(ParserType parserType) {
491+
String url = "https://example.com/foo?foo=bar";
492+
UriComponents uric = UriComponentsBuilder.fromUriString(url, parserType).query(null).build();
493+
assertThat(uric.getQueryParams()).isEqualTo(Map.of("foo", List.of("bar")));
496494
}
497495

498496
@ParameterizedTest

0 commit comments

Comments
 (0)