-
Notifications
You must be signed in to change notification settings - Fork 40.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Additional profiles are processed too late when legacy processing is used #25817
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,6 +147,7 @@ | |
* @author Brian Clozel | ||
* @author Artsiom Yudovin | ||
* @author Marten Deinum | ||
* @author Nguyen Bao Sach | ||
*/ | ||
@ExtendWith(OutputCaptureExtension.class) | ||
class SpringApplicationTests { | ||
|
@@ -604,6 +605,17 @@ void addProfilesOrder() { | |
assertThat(environment.getActiveProfiles()).containsExactly("bar", "spam", "foo"); | ||
} | ||
|
||
@Test | ||
void includeProfilesOrder() { | ||
SpringApplication application = new SpringApplication(ExampleConfig.class); | ||
application.setWebApplicationType(WebApplicationType.NONE); | ||
ConfigurableEnvironment environment = new StandardEnvironment(); | ||
application.setEnvironment(environment); | ||
this.context = application.run("--spring.profiles.active=bar,spam", "--spring.profiles.include=foo"); | ||
// Since Boot 2.4 included profiles should always be last | ||
assertThat(environment.getActiveProfiles()).containsExactly("bar", "spam", "foo"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test also seems unrelated to this change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As my above reply, Could I add this test to another PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we already cover the active and include case in this test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mbhave |
||
|
||
@Test | ||
void addProfilesOrderWithProperties() { | ||
SpringApplication application = new SpringApplication(ExampleConfig.class); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
* | ||
* @author Phillip Webb | ||
* @author Madhura Bhave | ||
* @author Nguyen Bao Sach | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
class ConfigDataEnvironmentPostProcessorTests { | ||
|
@@ -82,6 +83,7 @@ void postProcessEnvironmentWhenNoLoaderCreatesDefaultLoaderInstance() { | |
verify(this.postProcessor).getConfigDataEnvironment(any(), this.resourceLoaderCaptor.capture(), any()); | ||
verify(this.configDataEnvironment).processAndApply(); | ||
assertThat(this.resourceLoaderCaptor.getValue()).isInstanceOf(DefaultResourceLoader.class); | ||
assertThat(this.environment.getActiveProfiles()).isEmpty(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nguyensach What is the reason for adding this (and similiar ones below) assertion here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In It adds the additional profiles to the environment's active profiles, so I want to check the environment's active profiles here |
||
} | ||
|
||
@Test | ||
|
@@ -93,6 +95,7 @@ void postProcessEnvironmentWhenCustomLoaderUsesSpecifiedLoaderInstance() { | |
verify(this.postProcessor).getConfigDataEnvironment(any(), this.resourceLoaderCaptor.capture(), any()); | ||
verify(this.configDataEnvironment).processAndApply(); | ||
assertThat(this.resourceLoaderCaptor.getValue()).isSameAs(resourceLoader); | ||
assertThat(this.environment.getActiveProfiles()).isEmpty(); | ||
} | ||
|
||
@Test | ||
|
@@ -103,6 +106,7 @@ void postProcessEnvironmentWhenHasAdditionalProfilesOnSpringApplicationUsesAddit | |
verify(this.postProcessor).getConfigDataEnvironment(any(), any(), this.additionalProfilesCaptor.capture()); | ||
verify(this.configDataEnvironment).processAndApply(); | ||
assertThat(this.additionalProfilesCaptor.getValue()).containsExactly("dev"); | ||
assertThat(this.environment.getActiveProfiles()).isEmpty(); | ||
} | ||
|
||
@Test | ||
|
@@ -115,6 +119,21 @@ void postProcessEnvironmentWhenUseLegacyProcessingSwitchesToLegacyMethod() { | |
this.postProcessor.postProcessEnvironment(this.environment, this.application); | ||
verifyNoInteractions(this.configDataEnvironment); | ||
verify(legacyListener).addPropertySources(eq(this.environment), any(DefaultResourceLoader.class)); | ||
assertThat(this.environment.getActiveProfiles()).isEmpty(); | ||
} | ||
|
||
@Test | ||
void postProcessEnvironmentWhenHasAdditionalProfilesViaProgrammaticallySettingAndUseLegacyProcessing() { | ||
this.application.setAdditionalProfiles("dev"); | ||
ConfigDataEnvironmentPostProcessor.LegacyConfigFileApplicationListener legacyListener = mock( | ||
ConfigDataEnvironmentPostProcessor.LegacyConfigFileApplicationListener.class); | ||
willThrow(new UseLegacyConfigProcessingException(null)).given(this.postProcessor) | ||
.getConfigDataEnvironment(any(), any(), any()); | ||
willReturn(legacyListener).given(this.postProcessor).getLegacyListener(); | ||
this.postProcessor.postProcessEnvironment(this.environment, this.application); | ||
verifyNoInteractions(this.configDataEnvironment); | ||
verify(legacyListener).addPropertySources(eq(this.environment), any(DefaultResourceLoader.class)); | ||
assertThat(this.environment.getActiveProfiles()).containsExactly("dev"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of just checking if the environment contains the additional profiles (which it does even today because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the scope of
In order to check it, I added two below tests. |
||
} | ||
|
||
@Test | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -33,6 +33,7 @@ | |||
* | ||||
* @author Phillip Webb | ||||
* @author Dave Syer | ||||
* @author Nguyen Bao Sach | ||||
*/ | ||||
@ExtendWith(UseLegacyProcessing.class) | ||||
class ConfigFileApplicationListenerLegacyReproTests { | ||||
|
@@ -167,6 +168,17 @@ void reverseOrderOfProfilesWithYamlAndNoOverride() { | |||
assertVersionProperty(this.context, "A", "C", "A"); | ||||
} | ||||
|
||||
@Test | ||||
void additionalProfilesViaProgrammaticallySetting() { | ||||
// gh-25704 | ||||
SpringApplication application = new SpringApplication(Config.class); | ||||
application.setWebApplicationType(WebApplicationType.NONE); | ||||
application.setAdditionalProfiles("dev"); | ||||
this.context = application.run(); | ||||
assertThat(this.context.getEnvironment().acceptsProfiles(Profiles.of("dev"))).isTrue(); | ||||
assertThat(this.context.getEnvironment().getProperty("my.property")).isEqualTo("fromdevpropertiesfile"); | ||||
} | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this test as it is essentially a duplicate of the test below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 32 in 0893df4
According to the above comment, this test reproduces reported issues. Like you commented, it is essentially a duplicate of another test, so I will remove it. |
||||
|
||||
private void assertVersionProperty(ConfigurableApplicationContext context, String expectedVersion, | ||||
String... expectedActiveProfiles) { | ||||
assertThat(context.getEnvironment().getActiveProfiles()).isEqualTo(expectedActiveProfiles); | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ | |
* @author Eddú Meléndez | ||
* @author Madhura Bhave | ||
* @author Scott Frederick | ||
* @author Nguyen Bao Sach | ||
*/ | ||
@Deprecated | ||
@ExtendWith({ OutputCaptureExtension.class, UseLegacyProcessing.class }) | ||
|
@@ -1150,6 +1151,46 @@ void locationsWithWildcardFilesShouldIgnoreHiddenDirectories() { | |
assertThat(this.environment.getProperty("fourth.property")).isNull(); | ||
} | ||
|
||
@Test | ||
void additionalProfilesCanBeIncludedFromProgrammaticallySetting() { | ||
// gh-25704 | ||
SpringApplication application = new SpringApplication(Config.class); | ||
application.setWebApplicationType(WebApplicationType.NONE); | ||
application.setAdditionalProfiles("dev"); | ||
this.context = application.run(); | ||
// Active profile should win over default | ||
assertThat(this.context.getEnvironment().getProperty("my.property")).isEqualTo("fromdevpropertiesfile"); | ||
} | ||
|
||
@Test | ||
void twoAdditionalProfilesCanBeIncludedFromProgrammaticallySetting() { | ||
// gh-25704 | ||
SpringApplication application = new SpringApplication(Config.class); | ||
application.setWebApplicationType(WebApplicationType.NONE); | ||
application.setAdditionalProfiles("other", "dev"); | ||
this.context = application.run(); | ||
assertThat(this.context.getEnvironment().getProperty("my.property")).isEqualTo("fromdevpropertiesfile"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test doesn't seem to be giving us much either and I don't think it is required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I only want to test when there are many additional profiles rather than one. Maybe this test isn't related to the bug. Could I add this test to another PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case, we don't need the test for the single additional profile and we can keep this one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You don't need the test for the multiple additional profiles, right? |
||
} | ||
|
||
@Test | ||
void includeProfilesOrder() { | ||
SpringApplication application = new SpringApplication(Config.class); | ||
application.setWebApplicationType(WebApplicationType.NONE); | ||
this.context = application.run("--spring.profiles.active=bar,spam", "--spring.profiles.include=foo"); | ||
// Before Boot 2.4 included profiles should always be first | ||
assertThat(this.context.getEnvironment().getActiveProfiles()).containsExactly("foo", "bar", "spam"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test doesn't seem to be related to the bug that this issue is trying to address. The issue is about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test isn't related to the bug. However, I think that we are lacking test cases of the order included profiles and active profiles. So, Could I add this test to another PR? Because of lacking test cases of the order additional profiles and active profiles, it results in #26189 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to be testing active and additional profiles though. It's testing active and included profiles. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mbhave |
||
} | ||
|
||
@Test | ||
void addProfilesOrder() { | ||
SpringApplication application = new SpringApplication(Config.class); | ||
application.setWebApplicationType(WebApplicationType.NONE); | ||
application.setAdditionalProfiles("foo"); | ||
this.context = application.run("--spring.profiles.active=bar,spam"); | ||
// Before Boot 2.4 additional profiles should always be first | ||
assertThat(this.context.getEnvironment().getActiveProfiles()).containsExactly("foo", "bar", "spam"); | ||
} | ||
|
||
private Condition<ConfigurableEnvironment> matchingPropertySource(final String sourceName) { | ||
return new Condition<ConfigurableEnvironment>("environment containing property source " + sourceName) { | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The additional profiles are included in the Environment's active profiles at ConfigDataEnvironmentPostProcessor when legacy processing is used, or at ConfigDataEnvironment as below
spring-boot/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java
Line 348 in 4ad87a8
Therefore,
configureAdditionalProfiles
becomes unnecessary.