Commit 7e2494eb authored by Madhura Bhave's avatar Madhura Bhave

Set environment active profiles according to processing order

Fixes gh-13965
parent e4442f4b
...@@ -337,6 +337,7 @@ public class ConfigFileApplicationListener ...@@ -337,6 +337,7 @@ public class ConfigFileApplicationListener
addToLoaded(MutablePropertySources::addLast, false)); addToLoaded(MutablePropertySources::addLast, false));
this.processedProfiles.add(profile); this.processedProfiles.add(profile);
} }
resetEnvironmentProfiles(this.processedProfiles);
load(null, this::getNegativeProfileFilter, load(null, this::getNegativeProfileFilter,
addToLoaded(MutablePropertySources::addFirst, true)); addToLoaded(MutablePropertySources::addFirst, true));
addLoadedPropertySources(); addLoadedPropertySources();
...@@ -673,6 +674,21 @@ public class ConfigFileApplicationListener ...@@ -673,6 +674,21 @@ public class ConfigFileApplicationListener
return new LinkedHashSet<>(list); return new LinkedHashSet<>(list);
} }
/**
* This ensures that the order of active profiles in the {@link Environment}
* matches the order in which the profiles were processed.
* @param processedProfiles the processed profiles
*/
private void resetEnvironmentProfiles(List<Profile> processedProfiles) {
String[] names = processedProfiles.stream().filter((profile) -> {
if (profile != null && !profile.isDefaultProfile()) {
return true;
}
return false;
}).map(Profile::getName).toArray(String[]::new);
this.environment.setActiveProfiles(names);
}
private void addLoadedPropertySources() { private void addLoadedPropertySources() {
MutablePropertySources destination = this.environment.getPropertySources(); MutablePropertySources destination = this.environment.getPropertySources();
List<MutablePropertySources> loaded = new ArrayList<>(this.loaded.values()); List<MutablePropertySources> loaded = new ArrayList<>(this.loaded.values());
......
...@@ -406,7 +406,7 @@ public class ConfigFileApplicationListenerTests { ...@@ -406,7 +406,7 @@ public class ConfigFileApplicationListenerTests {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
"spring.profiles.active=dev", "spring.profiles.include=other"); "spring.profiles.active=dev", "spring.profiles.include=other");
this.initializer.postProcessEnvironment(this.environment, this.application); this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).contains("dev", "other"); assertThat(this.environment.getActiveProfiles()).containsExactly("other", "dev");
assertThat(this.environment.getProperty("my.property")) assertThat(this.environment.getProperty("my.property"))
.isEqualTo("fromdevpropertiesfile"); .isEqualTo("fromdevpropertiesfile");
validateProfilePrecedence(null, "other", "dev"); validateProfilePrecedence(null, "other", "dev");
......
...@@ -116,6 +116,20 @@ public class ConfigFileApplicationListenerYamlProfileNegationTests { ...@@ -116,6 +116,20 @@ public class ConfigFileApplicationListenerYamlProfileNegationTests {
assertThat(this.context.getEnvironment().getProperty("not-e")).isNull(); assertThat(this.context.getEnvironment().getProperty("not-e")).isNull();
} }
@Test
public void yamlProfileCascadingMultipleActiveProfilesViaPropertiesShouldPreserveOrder() {
SpringApplication application = new SpringApplication(Config.class);
application.setWebApplicationType(WebApplicationType.NONE);
String configName = "--spring.config.name=cascadingprofiles";
this.context = application.run(configName, "--spring.profiles.active=A,B");
assertVersionProperty(this.context, "D", "A", "C", "E", "B", "D");
assertThat(this.context.getEnvironment().getProperty("not-a")).isNull();
assertThat(this.context.getEnvironment().getProperty("not-b")).isNull();
assertThat(this.context.getEnvironment().getProperty("not-c")).isNull();
assertThat(this.context.getEnvironment().getProperty("not-d")).isNull();
assertThat(this.context.getEnvironment().getProperty("not-e")).isNull();
}
@Test @Test
public void yamlProfileCascadingOverrideProfilesB() { public void yamlProfileCascadingOverrideProfilesB() {
SpringApplication application = new SpringApplication(Config.class); SpringApplication application = new SpringApplication(Config.class);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment