Skip to content

Commit

Permalink
Don't reverse contexts for ConfigData.
Browse files Browse the repository at this point in the history
This fixes an issue where the default had precedence over profile and app name specific.

Fixes spring-cloudgh-702
  • Loading branch information
spencergibb committed Jan 26, 2021
1 parent 695a8b0 commit 1b598e1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public List<ConsulConfigDataResource> resolveProfileSpecific(ConfigDataLocationR
ConsulPropertySources consulPropertySources = new ConsulPropertySources(properties, log);

List<String> contexts = (locationUri == null || CollectionUtils.isEmpty(locationUri.getPathSegments()))
? consulPropertySources.getAutomaticContexts(profiles.getAccepted())
? consulPropertySources.getAutomaticContexts(profiles.getAccepted(), false)
: getCustomContexts(locationUri, properties);

registerAndPromoteBean(resolverContext, ConsulConfigProperties.class, InstanceSupplier.of(properties));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public ConsulPropertySources(ConsulConfigProperties properties, Log log) {
}

public List<String> getAutomaticContexts(List<String> profiles) {
return getAutomaticContexts(profiles, true);
}

public List<String> getAutomaticContexts(List<String> profiles, boolean reverse) {
List<String> contexts = new ArrayList<>();
String prefix = properties.getPrefix();
String defaultContext = getContext(prefix, properties.getDefaultContext());
Expand All @@ -68,8 +72,10 @@ public List<String> getAutomaticContexts(List<String> profiles) {
for (String suffix : suffixes) {
addProfiles(contexts, baseContext, profiles, suffix);
}
// we build them backwards, first wins, so reverse
Collections.reverse(contexts);
if (reverse) {
// we build them backwards, first wins, so reverse
Collections.reverse(contexts);
}
return contexts;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ public class ConsulConfigDataIntegrationTests {

private static final String VALUE2 = "testPropVal2";

private static final String VALUE2_DEFAULT = "testPropVal2Default";

private static final String TEST_PROP2 = "testProp2";

private static final String TEST_PROP2_CANONICAL = "test-prop2";

private static final String KEY2 = ROOT + "/application/" + TEST_PROP2;

private static final String KEY2_APP_NAME = ROOT + "/" + APP_NAME + "/" + TEST_PROP2;

private static final String TEST_PROP3 = "testProp3";

private static final String TEST_PROP3_CANONICAL = "test-prop3";
Expand All @@ -85,13 +89,16 @@ public static void setup() {
client = ConsulTestcontainers.client();
client.deleteKVValues(PREFIX);
client.setKVValue(KEY1, VALUE1);
client.setKVValue(KEY2, VALUE2);
client.setKVValue(KEY2, VALUE2_DEFAULT);
client.setKVValue(KEY2_APP_NAME, VALUE2);

context = new SpringApplicationBuilder(Config.class).web(WebApplicationType.NONE).run(
"--logging.level.org.springframework.cloud.consul.config.ConfigWatch=TRACE",
"--spring.application.name=" + APP_NAME,
"--spring.config.import=consul:" + ConsulTestcontainers.getHost() + ":"
+ ConsulTestcontainers.getPort(),
"--spring.cloud.consul.config.prefix=" + ROOT, "--spring.cloud.consul.config.watch.delay=10");
"--spring.cloud.consul.config.prefix=" + ROOT, "--spring.cloud.consul.config.watch.delay=10",
"--spring.cloud.consul.config.watch.wait-time=1");

client = context.getBean(ConsulClient.class);
environment = context.getEnvironment();
Expand All @@ -107,8 +114,8 @@ public static void teardown() {

@Test
public void propertyLoaded() {
String testProp = environment.getProperty(TEST_PROP2_CANONICAL);
assertThat(testProp).as("testProp was wrong").isEqualTo(VALUE2);
String testProp2 = environment.getProperty(TEST_PROP2_CANONICAL);
assertThat(testProp2).as(TEST_PROP2 + " was wrong").isEqualTo(VALUE2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public void testResolveProfileSpecificWithAutomaticPaths() {
String location = "consul:myhost";
List<ConsulConfigDataResource> locations = testResolveProfileSpecific(location);
assertThat(locations).hasSize(4);
assertThat(toContexts(locations)).containsExactly("config/testapp,dev/", "config/testapp/",
"config/application,dev/", "config/application/");
assertThat(toContexts(locations)).containsExactly("config/application/", "config/application,dev/",
"config/testapp/", "config/testapp,dev/");
}

@Test
Expand Down

0 comments on commit 1b598e1

Please sign in to comment.