Skip to content

Commit 42e556d

Browse files
committed
Handle file with name matching an optional wildcard location
Previously, the presence of a file with the same name as an optional wildcard location would cause a failure. With this change the pattern is resolved only if the resource is a directory. Additionally, if an optional wildcard search location that was a file would also fail with an exception. This commit fixes that so that those locations are not resolved. Fixes gh-27120 Fixes gh-27209
1 parent 84110a2 commit 42e556d

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,15 @@ Resource[] getResources(String location, ResourceType type) {
9595
validatePattern(location, type);
9696
String directoryPath = location.substring(0, location.indexOf("*/"));
9797
String fileName = location.substring(location.lastIndexOf("/") + 1);
98-
Resource directoryResource = getResource(directoryPath);
99-
if (!directoryResource.exists()) {
100-
return new Resource[] { directoryResource };
98+
Resource resource = getResource(directoryPath);
99+
if (!resource.exists()) {
100+
return EMPTY_RESOURCES;
101+
}
102+
File file = getFile(location, resource);
103+
if (!file.isDirectory()) {
104+
return EMPTY_RESOURCES;
101105
}
102-
File directory = getDirectory(location, directoryResource);
103-
File[] subDirectories = directory.listFiles(this::isVisibleDirectory);
106+
File[] subDirectories = file.listFiles(this::isVisibleDirectory);
104107
if (subDirectories == null) {
105108
return EMPTY_RESOURCES;
106109
}
@@ -131,11 +134,9 @@ private void validatePattern(String location, ResourceType type) {
131134
Assert.state(directoryPath.endsWith("*/"), () -> String.format("Location '%s' must end with '*/'", location));
132135
}
133136

134-
private File getDirectory(String patternLocation, Resource resource) {
137+
private File getFile(String patternLocation, Resource resource) {
135138
try {
136-
File directory = resource.getFile();
137-
Assert.state(directory.isDirectory(), () -> "'" + directory + "' is not a directory");
138-
return directory;
139+
return resource.getFile();
139140
}
140141
catch (Exception ex) {
141142
throw new IllegalStateException(

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ private Collection<StandardConfigDataResource> resolveEmptyDirectories(
257257
Set<StandardConfigDataReference> references) {
258258
Set<StandardConfigDataResource> empty = new LinkedHashSet<>();
259259
for (StandardConfigDataReference reference : references) {
260-
empty.addAll(resolveEmptyDirectories(reference));
260+
if (reference.getDirectory() != null) {
261+
empty.addAll(resolveEmptyDirectories(reference));
262+
}
261263
}
262264
return empty;
263265
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,18 @@ void runWhenHasWildcardLocationLoadsFromAllMatchingLocations() {
709709
assertThat(environment.getProperty("second.property")).isEqualTo("ball");
710710
}
711711

712+
@Test
713+
void runWhenOptionalWildcardLocationDoesNotExistDoesNotThrowException() {
714+
assertThatNoException().isThrownBy(() -> this.application.run(
715+
"--spring.config.location=optional:file:src/test/resources/nonexistent/*/testproperties.properties"));
716+
}
717+
718+
@Test
719+
void runWhenMandatoryWildcardLocationDoesNotExistThrowsException() {
720+
assertThatExceptionOfType(ConfigDataLocationNotFoundException.class).isThrownBy(() -> this.application
721+
.run("--spring.config.location=file:src/test/resources/nonexistent/*/testproperties.properties"));
722+
}
723+
712724
@Test
713725
void runWhenMandatoryWildcardLocationHasEmptyFileDirectory() {
714726
assertThatNoException()

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import static org.assertj.core.api.Assertions.assertThat;
3636
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
37+
import static org.assertj.core.api.Assertions.assertThatNoException;
3738
import static org.mockito.BDDMockito.given;
3839
import static org.mockito.Mockito.mock;
3940

@@ -151,6 +152,12 @@ void resolveWhenLocationIsWildcardDirectoriesSortsAlphabeticallyBasedOnAbsoluteP
151152
filePath("src", "test", "resources", "config", "2-second", "testproperties.properties"));
152153
}
153154

155+
@Test
156+
void resolveWhenLocationIsWildcardAndMatchingFilePresentShouldNotFail() {
157+
ConfigDataLocation location = ConfigDataLocation.of("optional:file:src/test/resources/a-file/*/");
158+
assertThatNoException().isThrownBy(() -> this.resolver.resolve(this.context, location));
159+
}
160+
154161
@Test
155162
void resolveWhenLocationIsWildcardFilesLoadsAllFilesThatMatch() {
156163
ConfigDataLocation location = ConfigDataLocation

spring-boot-project/spring-boot/src/test/resources/a-file

Whitespace-only changes.

0 commit comments

Comments
 (0)