diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 139b9cd364..df1bf12fd2 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -14,4 +14,4 @@ - \ No newline at end of file + 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 e5f9db7c44..3b31403299 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 @@ -228,6 +228,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 d7430085b0..15709fb2c1 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,6 +43,7 @@ import static org.mockito.Mockito.mock; * * @author Madhura Bhave * @author Phillip Webb + * @author Moritz Halbritter */ class StandardConfigDataLocationResolverTests { @@ -263,6 +264,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) + "]"; }