diff --git a/README.adoc b/README.adoc index fcf46c26..3577a351 100644 --- a/README.adoc +++ b/README.adoc @@ -71,7 +71,7 @@ The refresh endpoint reports that the "sample" property changed. [[building]] = Building -:jdkversion: 17 +:jdkversion: 1.8 [[basic-compile-and-test]] == Basic Compile and Test diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java index 07f04fb5..fdf91d4a 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java @@ -51,7 +51,6 @@ import org.springframework.http.MediaType; 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; @@ -81,8 +80,7 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator } /** - * 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. @@ -90,10 +88,6 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator private List combineProfiles(ConfigClientProperties properties, org.springframework.core.env.Environment environment) { List 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 finalCombinedProfiles = combinedProfiles; List filteredActiveProfiles = Stream.of(environment.getActiveProfiles()) @@ -110,7 +104,9 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator @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()); diff --git a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java index 9515bc6c..60731cba 100644 --- a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java +++ b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocatorTests.java @@ -118,6 +118,17 @@ public class ConfigServicePropertySourceLocatorTests { 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"); @@ -571,6 +582,12 @@ public class ConfigServicePropertySourceLocatorTests { 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),