diff --git a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java index cd11cf9ea5..45521b4d7d 100644 --- a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java +++ b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,7 +48,6 @@ import org.springframework.util.StringUtils; * @author Rossen Stoyanchev * @author Phillip Webb * @author Sam Brannen - * @author Mengqi Xu * @since 3.1.3 * @see Hierarchical URIs */ @@ -1092,8 +1091,8 @@ final class HierarchicalUriComponents extends UriComponents { if (ObjectUtils.isArray(value)) { value = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(value)); } - if (value instanceof Collection coll) { - value = StringUtils.collectionToCommaDelimitedString(coll); + else if (value instanceof Collection collection) { + value = StringUtils.collectionToCommaDelimitedString(collection); } return value; } diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java index d7d3fefd6d..3cb083e156 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import java.net.URI; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -157,22 +158,22 @@ class UriComponentsTests { @Test void expandQueryParamWithArray() { - UriComponents uri = UriComponentsBuilder.fromPath("/hello") + String uri = UriComponentsBuilder.fromPath("/hello") .queryParam("name", "{name}") - .build(); - uri = uri.expand(Collections.singletonMap("name", new String[]{"foo", "bar"})); + .buildAndExpand(Map.of("name", new String[] {"foo", "bar"})) + .toString(); - assertThat(uri.toString()).hasToString("/hello?name=foo,bar"); + assertThat(uri).isEqualTo("/hello?name=foo,bar"); } @Test void expandQueryParamWithList() { - UriComponents uri = UriComponentsBuilder.fromPath("/hello") + String uri = UriComponentsBuilder.fromPath("/hello") .queryParam("name", "{name}") - .build(); - uri = uri.expand(Collections.singletonMap("name", List.of("foo", "bar"))); + .buildAndExpand(Map.of("name", List.of("foo", "bar"))) + .toString(); - assertThat(uri.toString()).hasToString("/hello?name=foo,bar"); + assertThat(uri).isEqualTo("/hello?name=foo,bar"); } @ParameterizedTest // SPR-12123