Commit de891543 authored by Phillip Webb's avatar Phillip Webb

Allow square bracket notation profiles properties

Update `ConfigFileApplicationListener` so that `spring.profiles.active`
and `spring.profiles.include` can use the square bracket list notation.

Prior to this commit, only comma-separated lists could be used for
those values.

Closes gh-21006
parent e3b84786
......@@ -358,8 +358,9 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
// The default profile for these purposes is represented as null. We add it
// first so that it is processed first and has lowest priority.
this.profiles.add(null);
Set<Profile> activatedViaProperty = getProfilesFromProperty(ACTIVE_PROFILES_PROPERTY);
Set<Profile> includedViaProperty = getProfilesFromProperty(INCLUDE_PROFILES_PROPERTY);
Binder binder = Binder.get(this.environment);
Set<Profile> activatedViaProperty = getProfiles(binder, ACTIVE_PROFILES_PROPERTY);
Set<Profile> includedViaProperty = getProfiles(binder, INCLUDE_PROFILES_PROPERTY);
List<Profile> otherActiveProfiles = getOtherActiveProfiles(activatedViaProperty, includedViaProperty);
this.profiles.addAll(otherActiveProfiles);
// Any pre-existing active profiles set via property sources (e.g.
......@@ -374,15 +375,6 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
}
}
private Set<Profile> getProfilesFromProperty(String profilesProperty) {
if (!this.environment.containsProperty(profilesProperty)) {
return Collections.emptySet();
}
Binder binder = Binder.get(this.environment);
Set<Profile> profiles = getProfiles(binder, profilesProperty);
return new LinkedHashSet<>(profiles);
}
private List<Profile> getOtherActiveProfiles(Set<Profile> activatedViaProperty,
Set<Profile> includedViaProperty) {
return Arrays.stream(this.environment.getActiveProfiles()).map(Profile::new).filter(
......@@ -595,8 +587,10 @@ public class ConfigFileApplicationListener implements EnvironmentPostProcessor,
return loaded.stream().map((propertySource) -> {
Binder binder = new Binder(ConfigurationPropertySources.from(propertySource),
this.placeholdersResolver);
return new Document(propertySource, binder.bind("spring.profiles", STRING_ARRAY).orElse(null),
getProfiles(binder, ACTIVE_PROFILES_PROPERTY), getProfiles(binder, INCLUDE_PROFILES_PROPERTY));
String[] profiles = binder.bind("spring.profiles", STRING_ARRAY).orElse(null);
Set<Profile> activeProfiles = getProfiles(binder, ACTIVE_PROFILES_PROPERTY);
Set<Profile> includeProfiles = getProfiles(binder, INCLUDE_PROFILES_PROPERTY);
return new Document(propertySource, profiles, activeProfiles, includeProfiles);
}).collect(Collectors.toList());
}
......
......@@ -471,6 +471,16 @@ class ConfigFileApplicationListenerTests {
validateProfilePreference(output, null, "other", "dev");
}
@Test
void profilesAddedToEnvironmentAndViaPropertyWithBracketNotation(CapturedOutput output) {
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, "spring.profiles.active[0]=dev",
"spring.profiles.active[1]=other");
this.initializer.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getActiveProfiles()).contains("dev", "other");
assertThat(this.environment.getProperty("my.property")).isEqualTo("fromotherpropertiesfile");
validateProfilePreference(output, null, "dev", "other");
}
@Test
void postProcessorsAreOrderedCorrectly() {
TestConfigFileApplicationListener testListener = new TestConfigFileApplicationListener();
......
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