From 861ed6785e4a7eceacb123076062721e56ab0b40 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Tue, 21 Nov 2023 15:27:17 -0500 Subject: [PATCH] Do not combine profiles when spring.cloud.config.pofiles is set. (#2354) Fixes #2349 --- .../ConfigServicePropertySourceLocator.java | 12 ++++-------- ...ConfigServicePropertySourceLocatorTests.java | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) 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 5509a259e4..73ab806668 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 @@ -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; @@ -80,8 +79,7 @@ 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. @@ -89,10 +87,6 @@ public ConfigServicePropertySourceLocator(ConfigClientProperties defaultProperti 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()) @@ -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()); 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 39d61edba5..83bd5ff3a1 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 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"); @@ -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),