From 38b9a833bd16b12a4fe73fe3595e15e2c19c4e64 Mon Sep 17 00:00:00 2001 From: izeye Date: Thu, 27 May 2021 10:15:57 +0900 Subject: [PATCH 1/2] Fix "Allow optional directories without sub-directories" Fix the update to `resolvePatternEmptyDirectories` introduced in commit 01dd68e29edc which was incorrectly checking `isOptional`. See gh-26677 --- .../context/config/StandardConfigDataLocationResolver.java | 2 +- .../ConfigDataEnvironmentPostProcessorIntegrationTests.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java index 8d956b334c..d00c91a24c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java @@ -267,7 +267,7 @@ public class StandardConfigDataLocationResolver private Set resolvePatternEmptyDirectories(StandardConfigDataReference reference) { Resource[] subdirectories = this.resourceLoader.getResources(reference.getDirectory(), ResourceType.DIRECTORY); ConfigDataLocation location = reference.getConfigDataLocation(); - if (location.isOptional() && ObjectUtils.isEmpty(subdirectories)) { + if (!location.isOptional() && ObjectUtils.isEmpty(subdirectories)) { String message = String.format("Config data location '%s' contains no subdirectories", location); throw new ConfigDataLocationNotFoundException(location, message, null); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java index d11631b0f5..4754b54f0e 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java @@ -715,7 +715,8 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests { void runWhenMandatoryWildcardLocationHasNoSubdirectories() { assertThatExceptionOfType(ConfigDataLocationNotFoundException.class).isThrownBy( () -> this.application.run("--spring.config.location=file:src/test/resources/config/0-empty/*/")) - .withMessage("Config data location 'file:src/test/resources/config/0-empty/*/' cannot be found"); + .withMessage( + "Config data location 'file:src/test/resources/config/0-empty/*/' contains no subdirectories"); } @Test From 3f858ed8a4b4f190885f7c4a5eff1c423f8546a9 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 26 May 2021 21:31:14 -0700 Subject: [PATCH 2/2] Test optional directories without sub-directories Add an additional test to ensure that optional wildcard directory patterns do not fail. See gh-26677 --- .../ConfigDataEnvironmentPostProcessorIntegrationTests.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java index 4754b54f0e..7372d3627f 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java @@ -719,6 +719,12 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests { "Config data location 'file:src/test/resources/config/0-empty/*/' contains no subdirectories"); } + @Test + void runWhenOptionalWildcardLocationHasNoSubdirectories() { + assertThatNoException().isThrownBy(() -> this.application + .run("--spring.config.location=optional:file:src/test/resources/config/0-empty/*/")); + } + @Test void runWhenHasMandatoryWildcardLocationThatDoesNotExist() { assertThatExceptionOfType(ConfigDataLocationNotFoundException.class)