Skip to content

Commit 01dd68e

Browse files
wilkinsonaphilwebb
andcommitted
Allow optional directories without sub-directories
Update `StandardConfigDataLocationResolver` so that directory resources are only required when the location is not optional. Closes gh-26627 Co-authored-by: Phillip Webb <[email protected]>
1 parent 14f578e commit 01dd68e

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataLocationNotFoundException.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,7 +44,18 @@ public ConfigDataLocationNotFoundException(ConfigDataLocation location) {
4444
* @param cause the exception cause
4545
*/
4646
public ConfigDataLocationNotFoundException(ConfigDataLocation location, Throwable cause) {
47-
super(getMessage(location), cause);
47+
this(location, getMessage(location), cause);
48+
}
49+
50+
/**
51+
* Create a new {@link ConfigDataLocationNotFoundException} instance.
52+
* @param location the location that could not be found
53+
* @param message the exception message
54+
* @param cause the exception cause
55+
* @since 2.4.7
56+
*/
57+
public ConfigDataLocationNotFoundException(ConfigDataLocation location, String message, Throwable cause) {
58+
super(message, cause);
4859
Assert.notNull(location, "Location must not be null");
4960
this.location = location;
5061
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.core.io.support.SpringFactoriesLoader;
4343
import org.springframework.core.log.LogMessage;
4444
import org.springframework.util.Assert;
45+
import org.springframework.util.ObjectUtils;
4546
import org.springframework.util.StringUtils;
4647

4748
/**
@@ -264,10 +265,13 @@ private Set<StandardConfigDataResource> resolveNonPatternEmptyDirectories(Standa
264265
}
265266

266267
private Set<StandardConfigDataResource> resolvePatternEmptyDirectories(StandardConfigDataReference reference) {
267-
Resource[] resources = this.resourceLoader.getResources(reference.getDirectory(), ResourceType.DIRECTORY);
268-
Assert.state(resources.length > 0,
269-
"No subdirectories found for mandatory directory location '" + reference.getDirectory() + "'.");
270-
return Arrays.stream(resources).filter(Resource::exists)
268+
Resource[] subdirectories = this.resourceLoader.getResources(reference.getDirectory(), ResourceType.DIRECTORY);
269+
ConfigDataLocation location = reference.getConfigDataLocation();
270+
if (location.isOptional() && ObjectUtils.isEmpty(subdirectories)) {
271+
String message = String.format("Config data location '%s' contains no subdirectories", location);
272+
throw new ConfigDataLocationNotFoundException(location, message, null);
273+
}
274+
return Arrays.stream(subdirectories).filter(Resource::exists)
271275
.map((resource) -> new StandardConfigDataResource(reference, resource, true))
272276
.collect(Collectors.toCollection(LinkedHashSet::new));
273277
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -713,10 +713,9 @@ void runWhenMandatoryWildcardLocationHasEmptyFileDirectory() {
713713

714714
@Test
715715
void runWhenMandatoryWildcardLocationHasNoSubdirectories() {
716-
assertThatIllegalStateException().isThrownBy(
716+
assertThatExceptionOfType(ConfigDataLocationNotFoundException.class).isThrownBy(
717717
() -> this.application.run("--spring.config.location=file:src/test/resources/config/0-empty/*/"))
718-
.withMessage(
719-
"No subdirectories found for mandatory directory location 'file:src/test/resources/config/0-empty/*/'.");
718+
.withMessage("Config data location 'file:src/test/resources/config/0-empty/*/' cannot be found");
720719
}
721720

722721
@Test
@@ -725,6 +724,18 @@ void runWhenHasMandatoryWildcardLocationThatDoesNotExist() {
725724
.isThrownBy(() -> this.application.run("--spring.config.location=file:invalid/*/"));
726725
}
727726

727+
@Test
728+
void runWhenHasOptionalWildcardLocationThatDoesNotExistDoesNotThrow() {
729+
assertThatNoException()
730+
.isThrownBy(() -> this.application.run("--spring.config.location=optional:file:invalid/*/"));
731+
}
732+
733+
@Test
734+
void runWhenOptionalWildcardLocationHasNoSubdirectoriesDoesNotThrow() {
735+
assertThatNoException().isThrownBy(() -> this.application
736+
.run("--spring.config.location=optional:file:src/test/resources/config/0-empty/*/"));
737+
}
738+
728739
@Test // gh-24990
729740
void runWhenHasProfileSpecificFileWithActiveOnProfileProperty() {
730741
ConfigurableApplicationContext context = this.application

0 commit comments

Comments
 (0)