Skip to content

Commit

Permalink
Prevent indirect standard profile-specific imports
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philwebb committed Jun 4, 2021
1 parent ad99aa2 commit d1b256a
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ private Set<StandardConfigDataReference> getReferences(ConfigDataLocationResolve
@Override
public List<StandardConfigDataResource> resolveProfileSpecific(ConfigDataLocationResolverContext context,
ConfigDataLocation location, Profiles profiles) {
if (context.getParent() != null) {
return null;
}
return resolve(getProfileSpecificReferences(context, location, profiles));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,15 +614,17 @@ void runWhenImportWithProfileVariantOrdersPropertySourcesCorrectly() {
this.application.setAdditionalProfiles("dev");
ConfigurableApplicationContext context = this.application
.run("--spring.config.location=classpath:application-import-with-profile-variant.properties");
assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported-dev");
assertThat(context.getEnvironment().getProperty("my.value"))
.isEqualTo("application-import-with-profile-variant-dev");
}

@Test
void runWhenImportWithProfileVariantAndDirectProfileImportOrdersPropertySourcesCorrectly() {
this.application.setAdditionalProfiles("dev");
ConfigurableApplicationContext context = this.application.run(
"--spring.config.location=classpath:application-import-with-profile-variant-and-direct-profile-import.properties");
assertThat(context.getEnvironment().getProperty("my.value")).isEqualTo("iwasimported-dev");
assertThat(context.getEnvironment().getProperty("my.value"))
.isEqualTo("application-import-with-profile-variant-imported-dev");
}

@Test
Expand Down Expand Up @@ -746,6 +748,19 @@ void runWhenHasProfileSpecificFileWithActiveOnProfileProperty() {
assertThat(environment.getProperty("test2")).isEqualTo("test2");
}

@Test // gh-26752
void runWhenHasProfileSpecificImportWithImportDoesNotImportSecondProfileSpecificFile() {
ConfigurableApplicationContext context = this.application
.run("--spring.config.name=application-profile-specific-import-with-import");
ConfigurableEnvironment environment = context.getEnvironment();
assertThat(environment.containsProperty("application-profile-specific-import-with-import")).isTrue();
assertThat(environment.containsProperty("application-profile-specific-import-with-import-p1")).isTrue();
assertThat(environment.containsProperty("application-profile-specific-import-with-import-p2")).isFalse();
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import")).isTrue();
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import-p1")).isFalse();
assertThat(environment.containsProperty("application-profile-specific-import-with-import-import-p2")).isFalse();
}

private Condition<ConfigurableEnvironment> matchingPropertySource(final String sourceName) {
return new Condition<ConfigurableEnvironment>("environment containing property source " + sourceName) {

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

0 comments on commit d1b256a

Please sign in to comment.