From 303a8f0825e4142644ee64f0771bfd2bba84c560 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Mon, 25 Sep 2023 10:53:27 +0200 Subject: [PATCH 1/2] Disable 'LombokGetterMayBeUsed' inspection --- .idea/inspectionProfiles/Project_Default.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 1ec9e6600dc..7f1766dd1b6 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -1,6 +1,7 @@ \ No newline at end of file From b266ade00ccba1d89d17f67b4599d6848abaa998 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Mon, 25 Sep 2023 11:39:04 +0200 Subject: [PATCH 2/2] Prevent exception when loading optional config data locations Closes gh-35683 --- .../StandardConfigDataLocationResolver.java | 3 +++ ...andardConfigDataLocationResolverTests.java | 25 +++++++++++++++++-- 2 files changed, 26 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 fb7e6e56e1a..3aefc5151fe 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 @@ -226,6 +226,9 @@ public class StandardConfigDataLocationResolver return Collections.singleton(reference); } } + if (configDataLocation.isOptional()) { + return Collections.emptySet(); + } throw new IllegalStateException("File extension is not known to any PropertySourceLoader. " + "If the location is meant to reference a directory, it must end in '/' or File.separator"); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/StandardConfigDataLocationResolverTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/StandardConfigDataLocationResolverTests.java index ef5c877799c..78c6f1ccaaa 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/StandardConfigDataLocationResolverTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/StandardConfigDataLocationResolverTests.java @@ -43,18 +43,19 @@ import static org.mockito.Mockito.mock; * * @author Madhura Bhave * @author Phillip Webb + * @author Moritz Halbritter */ class StandardConfigDataLocationResolverTests { private StandardConfigDataLocationResolver resolver; - private ConfigDataLocationResolverContext context = mock(ConfigDataLocationResolverContext.class); + private final ConfigDataLocationResolverContext context = mock(ConfigDataLocationResolverContext.class); private MockEnvironment environment; private Binder environmentBinder; - private ResourceLoader resourceLoader = new DefaultResourceLoader(); + private final ResourceLoader resourceLoader = new DefaultResourceLoader(); @BeforeEach void setup() { @@ -260,6 +261,26 @@ class StandardConfigDataLocationResolverTests { assertThat(locations).isEmpty(); } + @Test + void resolveWhenOptionalAndLoaderIsUnknownShouldNotFail() { + ConfigDataLocation location = ConfigDataLocation.of("optional:some-unknown-loader:dummy.properties"); + assertThatNoException().isThrownBy(() -> this.resolver.resolve(this.context, location)); + } + + @Test + void resolveWhenOptionalAndLoaderIsUnknownAndExtensionIsUnknownShouldNotFail() { + ConfigDataLocation location = ConfigDataLocation + .of("optional:some-unknown-loader:dummy.some-unknown-extension"); + List locations = this.resolver.resolve(this.context, location); + assertThatNoException().isThrownBy(() -> this.resolver.resolve(this.context, location)); + } + + @Test + void resolveWhenOptionalAndExtensionIsUnknownShouldNotFail() { + ConfigDataLocation location = ConfigDataLocation.of("optional:file:dummy.some-unknown-extension"); + assertThatNoException().isThrownBy(() -> this.resolver.resolve(this.context, location)); + } + private String filePath(String... components) { return "file [" + String.join(File.separator, components) + "]"; }