Skip to content

Commit

Permalink
Restore order of additional and active profiles
Browse files Browse the repository at this point in the history
This commit restores the order of additional and active profiles
so that active profiles now take precedence.

Fixes gh-26189
  • Loading branch information
mbhave committed May 19, 2021
1 parent 64e76ba commit 80610fa
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,16 @@ private List<String> asReversedList(List<String> list) {
return reversed;
}

private List<String> asUniqueItemList(Collection<String> strings) {
return asUniqueItemList(strings, null);
private List<String> asUniqueItemList(Collection<String> profiles) {
return asUniqueItemList(profiles, null);
}

private List<String> asUniqueItemList(Collection<String> strings, Collection<String> additional) {
LinkedHashSet<String> uniqueItems = new LinkedHashSet<>(strings);
private List<String> asUniqueItemList(Collection<String> profiles, Collection<String> additional) {
LinkedHashSet<String> uniqueItems = new LinkedHashSet<>();
if (!CollectionUtils.isEmpty(additional)) {
uniqueItems.addAll(additional);
}
uniqueItems.addAll(profiles);
return Collections.unmodifiableList(new ArrayList<>(uniqueItems));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,15 +594,14 @@ void addProfiles() {
}

@Test
void addProfilesOrder() {
void additionalProfilesOrderedBeforeActiveProfiles() {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setAdditionalProfiles("foo");
ConfigurableEnvironment environment = new StandardEnvironment();
application.setEnvironment(environment);
this.context = application.run("--spring.profiles.active=bar,spam");
// Since Boot 2.4 additional should always be last
assertThat(environment.getActiveProfiles()).containsExactly("bar", "spam", "foo");
assertThat(environment.getActiveProfiles()).containsExactly("foo", "bar", "spam");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ void runWhenTwoProfilesSetProgrammaticallyLoadsWithPreservedProfileOrder() {
void runWhenProfilesPresentBeforeConfigFileProcessingAugmentsProfileActivatedByConfigFile() {
this.application.setAdditionalProfiles("other");
ConfigurableApplicationContext context = this.application.run("--spring.config.name=enableprofile");
assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("myprofile", "other");
assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("other", "myprofile");
String property = context.getEnvironment().getProperty("other.property");
assertThat(property).isEqualTo("fromotherpropertiesfile");
property = context.getEnvironment().getProperty("the.property");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ void getActiveWithProfileGroups() {
@Test
void getActiveWhenHasAdditionalIncludesAdditional() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.profiles.active", "a,b,c");
environment.setProperty("spring.profiles.active", "d,e,f");
Binder binder = Binder.get(environment);
Profiles profiles = new Profiles(environment, binder, Arrays.asList("d", "e", "f"));
Profiles profiles = new Profiles(environment, binder, Arrays.asList("a", "b", "c"));
assertThat(profiles.getActive()).containsExactly("a", "b", "c", "d", "e", "f");
}

Expand Down

0 comments on commit 80610fa

Please sign in to comment.