Merge branch '4.0.x'

This commit is contained in:
Ryan Baxter
2023-11-21 15:29:21 -05:00
3 changed files with 22 additions and 9 deletions

View File

@@ -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

View File

@@ -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<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())
@@ -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());

View File

@@ -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),