Only use spring.cloud.config.profiles if not 'default'.
In ConfigData, the Profiles object takes care of resolving the default profile. So only prefer spring.cloud.config.profiles if it has been set to something other than 'default'. Fixes gh-1777
This commit is contained in:
@@ -65,6 +65,11 @@ public class ConfigClientProperties {
|
||||
*/
|
||||
public static final String AUTHORIZATION = "authorization";
|
||||
|
||||
/**
|
||||
* Default profile value.
|
||||
*/
|
||||
public static final String DEFAULT_PROFILE = "default";
|
||||
|
||||
/**
|
||||
* Flag to say that remote configuration is enabled. Default true;
|
||||
*/
|
||||
@@ -74,7 +79,7 @@ public class ConfigClientProperties {
|
||||
* The default profile to use when fetching remote configuration (comma-separated).
|
||||
* Default is "default".
|
||||
*/
|
||||
private String profile = "default";
|
||||
private String profile = DEFAULT_PROFILE;
|
||||
|
||||
/**
|
||||
* Name of application used to fetch remote properties.
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.config.client;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.springframework.boot.context.config.ConfigDataResource;
|
||||
@@ -46,10 +47,12 @@ public class ConfigServerConfigDataResource extends ConfigDataResource {
|
||||
}
|
||||
|
||||
public String getProfiles() {
|
||||
if (StringUtils.hasText(properties.getProfile())) {
|
||||
List<String> accepted = profiles.getAccepted();
|
||||
if (StringUtils.hasText(properties.getProfile())
|
||||
&& !properties.getProfile().equals(ConfigClientProperties.DEFAULT_PROFILE)) {
|
||||
return properties.getProfile();
|
||||
}
|
||||
return StringUtils.collectionToCommaDelimitedString(profiles.getAccepted());
|
||||
return StringUtils.collectionToCommaDelimitedString(accepted);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.config.client;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -80,9 +81,22 @@ public class ConfigServerConfigDataLocationResolverTests {
|
||||
assertThat(resource.getProfiles()).isEqualTo("myprofile");
|
||||
}
|
||||
|
||||
@Test
|
||||
void configClientSpringProfilesActiveOverridesDefaultClientProfiles() {
|
||||
ConfigServerConfigDataResource resource = testResolveProvileSpecific("myactiveprofile");
|
||||
assertThat(resource.getProfiles()).isEqualTo("myactiveprofile");
|
||||
}
|
||||
|
||||
private ConfigServerConfigDataResource testResolveProvileSpecific() {
|
||||
return testResolveProvileSpecific("default");
|
||||
}
|
||||
|
||||
private ConfigServerConfigDataResource testResolveProvileSpecific(String activeProfile) {
|
||||
when(context.getBootstrapContext()).thenReturn(mock(ConfigurableBootstrapContext.class));
|
||||
Profiles profiles = mock(Profiles.class);
|
||||
if (activeProfile != null) {
|
||||
when(profiles.getAccepted()).thenReturn(Collections.singletonList(activeProfile));
|
||||
}
|
||||
|
||||
List<ConfigServerConfigDataResource> resources = this.resolver.resolveProfileSpecific(context,
|
||||
ConfigDataLocation.of("configserver:"), profiles);
|
||||
|
||||
Reference in New Issue
Block a user