Skip to content

Commit 858c2bd

Browse files
committed
Polishing contribution
Closes gh-34783
1 parent 124582d commit 858c2bd

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -451,11 +451,10 @@ private MultiValueMap<String, String> expandQueryParams(UriTemplateVariables var
451451
UriTemplateVariables queryVariables = new QueryUriTemplateVariables(variables);
452452
this.queryParams.forEach((key, values) -> {
453453
String name = expandUriComponent(key, queryVariables, this.variableEncoder);
454-
List<String> expandedValues = result.getOrDefault(name, new ArrayList<>(values.size()));
454+
List<String> expandedValues = result.computeIfAbsent(name, k -> new ArrayList<>(values.size()));
455455
for (String value : values) {
456456
expandedValues.add(expandUriComponent(value, queryVariables, this.variableEncoder));
457457
}
458-
result.put(name, expandedValues);
459458
});
460459
return CollectionUtils.unmodifiableMultiValueMap(result);
461460
}

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

+6-9
Original file line numberDiff line numberDiff line change
@@ -629,18 +629,15 @@ void parseBuildAndExpandHierarchical(ParserType parserType) {
629629
assertThat(uri.toString()).isEqualTo("ws://example.org:7777/path?q=1#foo");
630630
}
631631

632-
@ParameterizedTest
632+
@ParameterizedTest // gh-34783
633633
@EnumSource
634-
void parseBuildAndExpandHierarchicalWithDuplicateQueryKeys(ParserType parserType) {
635-
UriComponents result = UriComponentsBuilder.fromUriString("/?{pk1}={pv1}&{pk2}={pv2}", parserType)
634+
void parseBuildAndExpandQueryParamWithSameName(ParserType parserType) {
635+
UriComponents result = UriComponentsBuilder
636+
.fromUriString("/?{pk1}={pv1}&{pk2}={pv2}", parserType)
636637
.buildAndExpand("k1", "v1", "k1", "v2");
637-
assertThat(result.getQuery()).isEqualTo("k1=v1&k1=v2");
638-
assertThat(result.getQueryParams().get("k1")).containsExactly("v1", "v2");
639638

640-
UriComponents result2 = UriComponentsBuilder.fromUriString("/?{pk1}={pv1}&{pk2}={pv2}", parserType)
641-
.buildAndExpand(Map.of("pk1", "k1", "pv1", "v1", "pk2", "k1", "pv2", "v2"));
642-
assertThat(result2.getQuery()).isEqualTo("k1=v1&k1=v2");
643-
assertThat(result.getQueryParams().get("k1")).containsExactly("v1", "v2");
639+
assertThat(result.getQuery()).isEqualTo("k1=v1&k1=v2");
640+
assertThat(result.getQueryParams()).containsExactly(Map.entry("k1", List.of("v1", "v2")));
644641
}
645642

646643
@ParameterizedTest

0 commit comments

Comments
 (0)