Skip to content

Commit 315067b

Browse files
committed
Merge branch '2.4.x'
Closes gh-24505
2 parents edf67e5 + d1f2aab commit 315067b

File tree

8 files changed

+74
-19
lines changed

8 files changed

+74
-19
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public final class ConfigData {
4545

4646
private final Set<Option> options;
4747

48+
/**
49+
* A {@link ConfigData} instance that contains no data.
50+
*/
51+
public static final ConfigData EMPTY = new ConfigData(Collections.emptySet());
52+
4853
/**
4954
* Create a new {@link ConfigData} instance.
5055
* @param propertySources the config data property sources in ascending priority

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,15 @@ static ConfigDataEnvironmentContributor ofUnboundImport(ConfigDataLocation locat
300300
configurationPropertySource, null, ignoreImports, null);
301301
}
302302

303+
/**
304+
* Factory method to create an {@link Kind#EMPTY_LOCATION empty location} contributor.
305+
* @param location the location of this contributor
306+
* @return a new {@link ConfigDataEnvironmentContributor} instance
307+
*/
308+
static ConfigDataEnvironmentContributor ofEmptyLocation(ConfigDataLocation location) {
309+
return new ConfigDataEnvironmentContributor(Kind.EMPTY_LOCATION, location, null, null, null, null, true, null);
310+
}
311+
303312
/**
304313
* The various kinds of contributor.
305314
*/
@@ -330,7 +339,12 @@ enum Kind {
330339
* A contributor with {@link ConfigData} imported from another contributor that
331340
* has been.
332341
*/
333-
BOUND_IMPORT;
342+
BOUND_IMPORT,
343+
344+
/**
345+
* A valid location that contained noething to load.
346+
*/
347+
EMPTY_LOCATION;
334348

335349
}
336350

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,15 @@ private List<ConfigDataEnvironmentContributor> asContributors(
161161
Map<ConfigDataResolutionResult, ConfigData> imported) {
162162
List<ConfigDataEnvironmentContributor> contributors = new ArrayList<>(imported.size() * 5);
163163
imported.forEach((resolutionResult, data) -> {
164-
for (int i = data.getPropertySources().size() - 1; i >= 0; i--) {
165-
ConfigDataLocation location = resolutionResult.getLocation();
166-
ConfigDataResource resource = resolutionResult.getResource();
167-
contributors.add(ConfigDataEnvironmentContributor.ofUnboundImport(location, resource, data, i));
164+
ConfigDataLocation location = resolutionResult.getLocation();
165+
ConfigDataResource resource = resolutionResult.getResource();
166+
if (data.getPropertySources().isEmpty()) {
167+
contributors.add(ConfigDataEnvironmentContributor.ofEmptyLocation(location));
168+
}
169+
else {
170+
for (int i = data.getPropertySources().size() - 1; i >= 0; i--) {
171+
contributors.add(ConfigDataEnvironmentContributor.ofUnboundImport(location, resource, data, i));
172+
}
168173
}
169174
});
170175
return Collections.unmodifiableList(contributors);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public class StandardConfigDataLoader implements ConfigDataLoader<StandardConfig
3636
@Override
3737
public ConfigData load(ConfigDataLoaderContext context, StandardConfigDataResource resource)
3838
throws IOException, ConfigDataNotFoundException {
39+
if (resource.isEmptyDirectory()) {
40+
return ConfigData.EMPTY;
41+
}
3942
ConfigDataResourceNotFoundException.throwIfDoesNotExist(resource, resource.getResource());
4043
StandardConfigDataReference reference = resource.getReference();
4144
Resource originTrackedResource = OriginTrackedResource.of(resource.getResource(),

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.context.config;
1818

1919
import java.util.ArrayList;
20+
import java.util.Collection;
2021
import java.util.Collections;
2122
import java.util.LinkedHashSet;
2223
import java.util.List;
@@ -220,25 +221,26 @@ private List<StandardConfigDataResource> resolve(Set<StandardConfigDataReference
220221
resolved.addAll(resolve(reference));
221222
}
222223
if (resolved.isEmpty()) {
223-
assertNonOptionalDirectories(references);
224+
resolved.addAll(resolveEmptyDirectories(references));
224225
}
225226
return resolved;
226227
}
227228

228-
private void assertNonOptionalDirectories(Set<StandardConfigDataReference> references) {
229+
private Collection<StandardConfigDataResource> resolveEmptyDirectories(
230+
Set<StandardConfigDataReference> references) {
231+
Set<StandardConfigDataResource> empty = new LinkedHashSet<>();
229232
for (StandardConfigDataReference reference : references) {
230-
if (reference.isNonOptionalDirectory()) {
231-
assertDirectoryExists(reference);
233+
if (reference.isMandatoryDirectory()) {
234+
Resource resource = this.resourceLoader.getResource(reference.getDirectory());
235+
if (resource instanceof ClassPathResource) {
236+
continue;
237+
}
238+
StandardConfigDataResource configDataResource = new StandardConfigDataResource(reference, resource);
239+
ConfigDataResourceNotFoundException.throwIfDoesNotExist(configDataResource, resource);
240+
empty.add(new StandardConfigDataResource(reference, resource, true));
232241
}
233242
}
234-
}
235-
236-
private void assertDirectoryExists(StandardConfigDataReference reference) {
237-
Resource resource = this.resourceLoader.getResource(reference.getDirectory());
238-
if (!(resource instanceof ClassPathResource)) {
239-
StandardConfigDataResource configDataResource = new StandardConfigDataResource(reference, resource);
240-
ConfigDataResourceNotFoundException.throwIfDoesNotExist(configDataResource, resource);
241-
}
243+
return empty;
242244
}
243245

244246
private List<StandardConfigDataResource> resolve(StandardConfigDataReference reference) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ String getResourceLocation() {
6666
return this.resourceLocation;
6767
}
6868

69-
boolean isNonOptionalDirectory() {
69+
boolean isMandatoryDirectory() {
7070
return !this.configDataLocation.isOptional() && this.directory != null;
7171
}
7272

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,29 @@ public class StandardConfigDataResource extends ConfigDataResource {
3636

3737
private final Resource resource;
3838

39+
private boolean emptyDirectory;
40+
3941
/**
4042
* Create a new {@link StandardConfigDataResource} instance.
4143
* @param reference the resource reference
4244
* @param resource the underlying resource
4345
*/
4446
StandardConfigDataResource(StandardConfigDataReference reference, Resource resource) {
47+
this(reference, resource, false);
48+
}
49+
50+
/**
51+
* Create a new {@link StandardConfigDataResource} instance.
52+
* @param reference the resource reference
53+
* @param resource the underlying resource
54+
* @param emptyDirectory if the resource is an empty directory that we know exists
55+
*/
56+
StandardConfigDataResource(StandardConfigDataReference reference, Resource resource, boolean emptyDirectory) {
4557
Assert.notNull(reference, "Reference must not be null");
4658
Assert.notNull(resource, "Resource must not be null");
4759
this.reference = reference;
4860
this.resource = resource;
61+
this.emptyDirectory = emptyDirectory;
4962
}
5063

5164
StandardConfigDataReference getReference() {
@@ -56,6 +69,10 @@ Resource getResource() {
5669
return this.resource;
5770
}
5871

72+
boolean isEmptyDirectory() {
73+
return this.emptyDirectory;
74+
}
75+
5976
@Override
6077
public boolean equals(Object obj) {
6178
if (this == obj) {
@@ -65,7 +82,7 @@ public boolean equals(Object obj) {
6582
return false;
6683
}
6784
StandardConfigDataResource other = (StandardConfigDataResource) obj;
68-
return this.resource.equals(other.resource);
85+
return this.resource.equals(other.resource) && this.emptyDirectory == other.emptyDirectory;
6986
}
7087

7188
@Override

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import static org.assertj.core.api.Assertions.assertThat;
6262
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
6363
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
64+
import static org.assertj.core.api.Assertions.assertThatNoException;
6465

6566
/**
6667
* Integration tests for {@link ConfigDataEnvironmentPostProcessor}.
@@ -535,6 +536,14 @@ void runWhenConfigLocationHasNonOptionalMissingClasspathDirectoryThrowsLocationN
535536
.isThrownBy(() -> this.application.run("--spring.config.location=" + location));
536537
}
537538

539+
@Test
540+
void runWhenConfigLocationHasNonOptionalEmptyFileDirectoryDoesNotThrowException() {
541+
File location = new File(this.temp, "application.empty");
542+
location.mkdirs();
543+
assertThatNoException().isThrownBy(() -> this.application
544+
.run("--spring.config.location=" + StringUtils.cleanPath(location.getAbsolutePath()) + "/"));
545+
}
546+
538547
@Test
539548
@Disabled("Disabled until spring.profiles suppport is dropped")
540549
void runWhenUsingInvalidPropertyThrowsException() {

0 commit comments

Comments
 (0)