From 1b25a1506a4b1f2c3cf2d38dc79866c89ffc11fe Mon Sep 17 00:00:00 2001 From: Kasper Bisgaard Date: Wed, 13 Mar 2024 10:20:27 +0100 Subject: [PATCH 1/2] Allow UriTemplate to be built with an empty template See gh-32432 --- .../org/springframework/web/util/UriTemplate.java | 2 +- .../springframework/web/util/UriTemplateTests.java | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java index 770c6ad7498..da7ba406d80 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java @@ -66,7 +66,7 @@ public class UriTemplate implements Serializable { * @param uriTemplate the URI template string */ public UriTemplate(String uriTemplate) { - Assert.hasText(uriTemplate, "'uriTemplate' must not be null"); + Assert.notNull(uriTemplate, "'uriTemplate' must not be null"); this.uriTemplate = uriTemplate; this.uriComponents = UriComponentsBuilder.fromUriString(uriTemplate).build(); diff --git a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java index 49044594fc2..9e68d7cd406 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java @@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatNoException; /** * @author Arjen Poutsma @@ -218,4 +219,14 @@ class UriTemplateTests { assertThat(uri.toString()).isEqualTo("http://localhost/query=foo@bar"); } + @Test + void emptyPathDoesNotThrowException() { + assertThatNoException().isThrownBy(() -> new UriTemplate("")); + } + + @Test + void emptyPathThrowsException() { + assertThatIllegalArgumentException().isThrownBy(() -> new UriTemplate(null)); + } + } From a34ceb405c4dc64860fcb0d289630e4110bce9e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Wed, 13 Mar 2024 17:21:46 +0100 Subject: [PATCH 2/2] Polish "Allow UriTemplate to be built with an empty template" See gh-32432 --- .../springframework/web/util/UriTemplate.java | 2 +- .../web/util/UriTemplateTests.java | 22 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java index da7ba406d80..94dfb9de617 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2024 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. diff --git a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java index 9e68d7cd406..723aa83a99f 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2024 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. @@ -36,6 +36,16 @@ import static org.assertj.core.api.Assertions.assertThatNoException; */ class UriTemplateTests { + @Test + void emptyPathDoesNotThrowException() { + assertThatNoException().isThrownBy(() -> new UriTemplate("")); + } + + @Test + void nullPathThrowsException() { + assertThatIllegalArgumentException().isThrownBy(() -> new UriTemplate(null)); + } + @Test void getVariableNames() { UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); @@ -219,14 +229,4 @@ class UriTemplateTests { assertThat(uri.toString()).isEqualTo("http://localhost/query=foo@bar"); } - @Test - void emptyPathDoesNotThrowException() { - assertThatNoException().isThrownBy(() -> new UriTemplate("")); - } - - @Test - void emptyPathThrowsException() { - assertThatIllegalArgumentException().isThrownBy(() -> new UriTemplate(null)); - } - }