Skip to content

Commit

Permalink
Do not combine profiles when spring.cloud.config.pofiles is set. (#2354)
Browse files Browse the repository at this point in the history
Fixes #2349
  • Loading branch information
ryanjbaxter authored Nov 21, 2023
1 parent 558545d commit 861ed67
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.retry.annotation.Retryable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
Expand Down Expand Up @@ -80,19 +79,14 @@ public ConfigServicePropertySourceLocator(ConfigClientProperties defaultProperti
}

/**
* Combine properties from the config client properties and the active profiles from
* the environment.
* Combine the active and default profiles from the environment.
* @param properties config client properties,
* @param environment application environment.
* @return A list of combined profiles.
*/
private List<String> combineProfiles(ConfigClientProperties properties,
org.springframework.core.env.Environment environment) {
List<String> combinedProfiles = new ArrayList<>();
if (!ObjectUtils.isEmpty(properties.getProfile())) {
combinedProfiles = Stream.of(properties.getProfile().split(",")).map(String::trim).filter(s -> !s.isEmpty())
.collect(Collectors.toList());
}
if (environment.getActiveProfiles().length > 0) {
List<String> finalCombinedProfiles = combinedProfiles;
List<String> filteredActiveProfiles = Stream.of(environment.getActiveProfiles())
Expand All @@ -109,7 +103,9 @@ else if (environment.getDefaultProfiles().length > 0 && combinedProfiles.isEmpty
@Retryable(interceptor = "configServerRetryInterceptor")
public org.springframework.core.env.PropertySource<?> locate(org.springframework.core.env.Environment environment) {
ConfigClientProperties properties = this.defaultProperties.override(environment);
properties.setProfile(String.join(",", combineProfiles(properties, environment)));
if (!StringUtils.hasText(properties.getProfile())) {
properties.setProfile(String.join(",", combineProfiles(properties, environment)));
}

if (StringUtils.startsWithIgnoreCase(properties.getName(), "application-")) {
InvalidApplicationNameException exception = new InvalidApplicationNameException(properties.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ public void sunnyDayWithLabel() {
assertThat(this.locator.locateCollection(this.environment)).isNotNull();
}

@Test
public void overrideProfile() {
Environment body = new Environment("app", "override-profile");
body.add(new PropertySource("p1", new HashMap<>()));
mockRequestResponseWithProfile(new ResponseEntity<>(body, HttpStatus.OK), "override-profile");
this.locator.setRestTemplate(this.restTemplate);
TestPropertyValues.of("spring.cloud.config.profile:override-profile", "spring.profiles.active: foo")
.applyTo(this.environment);
assertThat(this.locator.locateCollection(this.environment).size()).isEqualTo(2);
}

@Test
public void sunnyDayWithLabelThatContainsASlash() {
Environment body = new Environment("app", "master");
Expand Down Expand Up @@ -358,6 +369,12 @@ private void mockRequestResponseWithLabel(ResponseEntity<?> response, String lab
ArgumentMatchers.eq(label))).thenReturn(response);
}

private void mockRequestResponseWithProfile(ResponseEntity<?> response, String profiles) {
Mockito.when(this.restTemplate.exchange(Mockito.any(String.class), Mockito.any(HttpMethod.class),
Mockito.any(HttpEntity.class), Mockito.any(Class.class), anyString(), ArgumentMatchers.eq(profiles)))
.thenReturn(response);
}

@SuppressWarnings("unchecked")
private void mockRequestResponseWithoutLabel(ResponseEntity<?> response) {
Mockito.when(this.restTemplate.exchange(Mockito.any(String.class), Mockito.any(HttpMethod.class),
Expand Down

0 comments on commit 861ed67

Please sign in to comment.