Skip to content

Commit d1b256a

Browse files
committed
Prevent indirect standard profile-specific imports
Update `StandardConfigDataLocationResolver` so that profile-specific imports can only be used when there is no parent import. Prior to this commit, given the following application.properties file: spring.profiles.active=p1,p2 spring.config.import=other.properties We would attempt to import `other.properties`, `other-p1.properties` and `other-p2.properties`. This seems quite confusing and when we really only need to support profile-specific properties for the initial root set of locations. Fixes gh-26752
1 parent ad99aa2 commit d1b256a

13 files changed

+33
-8
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ private Set<StandardConfigDataReference> getReferences(ConfigDataLocationResolve
135135
@Override
136136
public List<StandardConfigDataResource> resolveProfileSpecific(ConfigDataLocationResolverContext context,
137137
ConfigDataLocation location, Profiles profiles) {
138+
if (context.getParent() != null) {
139+
return null;
140+
}
138141
return resolve(getProfileSpecificReferences(context, location, profiles));
139142
}
140143

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,15 +614,17 @@ void runWhenImportWithProfileVariantOrdersPropertySourcesCorrectly() {
614614
this.application.setAdditionalProfiles("dev");
615615
ConfigurableApplicationContext context = this.application
616616
.run("--spring.config.location=classpath:application-import-with-profile-variant.properties");
617-
assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported-dev");
617+
assertThat(context.getEnvironment().getProperty("my.value"))
618+
.isEqualTo("application-import-with-profile-variant-dev");
618619
}
619620

620621
@Test
621622
void runWhenImportWithProfileVariantAndDirectProfileImportOrdersPropertySourcesCorrectly() {
622623
this.application.setAdditionalProfiles("dev");
623624
ConfigurableApplicationContext context = this.application.run(
624625
"--spring.config.location=classpath:application-import-with-profile-variant-and-direct-profile-import.properties");
625-
assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported-dev");
626+
assertThat(context.getEnvironment().getProperty("my.value"))
627+
.isEqualTo("application-import-with-profile-variant-imported-dev");
626628
}
627629

628630
@Test
@@ -746,6 +748,19 @@ void runWhenHasProfileSpecificFileWithActiveOnProfileProperty() {
746748
assertThat(environment.getProperty("test2")).isEqualTo("test2");
747749
}
748750

751+
@Test // gh-26752
752+
void runWhenHasProfileSpecificImportWithImportDoesNotImportSecondProfileSpecificFile() {
753+
ConfigurableApplicationContext context = this.application
754+
.run("--spring.config.name=application-profile-specific-import-with-import");
755+
ConfigurableEnvironment environment = context.getEnvironment();
756+
assertThat(environment.containsProperty("application-profile-specific-import-with-import")).isTrue();
757+
assertThat(environment.containsProperty("application-profile-specific-import-with-import-p1")).isTrue();
758+
assertThat(environment.containsProperty("application-profile-specific-import-with-import-p2")).isFalse();
759+
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import")).isTrue();
760+
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import-p1")).isFalse();
761+
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import-p2")).isFalse();
762+
}
763+
749764
private Condition<ConfigurableEnvironment> matchingPropertySource(final String sourceName) {
750765
return new Condition<ConfigurableEnvironment>("environment containing property source " + sourceName) {
751766

Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
spring.config.import=classpath:application-import-with-profile-variant-imported-dev.properties
2-
my.value=notimported-dev
2+
my.value=application-import-with-profile-variant-and-direct-profile-import-dev
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
spring.config.import=classpath:application-import-with-profile-variant-imported.properties
2-
my.value=notimported
2+
my.value=application-import-with-profile-variant-and-direct-profile-import
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
my.value=notimported-dev
1+
my.value=application-import-with-profile-variant-dev
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
my.value=iwasimported-dev
1+
my.value=application-import-with-profile-variant-imported-dev
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
my.value=iwasimported
1+
my.value=application-import-with-profile-variant-imported
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
spring.config.import=classpath:application-import-with-profile-variant-imported.properties
2-
my.value=notimported
2+
my.value=application-import-with-profile-variant
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
application-profile-specific-import-with-import-import-p1=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
application-profile-specific-import-with-import-import-p2=true

0 commit comments

Comments
 (0)