From a2087e05fb1e000cd34dac9ee006aa4cf059c429 Mon Sep 17 00:00:00 2001 From: spencergibb Date: Mon, 14 Dec 2020 17:17:10 -0500 Subject: [PATCH] Updates for boot 2.4.1 property source name. Fixes gh-1771 --- .../NativeEnvironmentRepository.java | 15 ++++++++++++-- .../NativeEnvironmentRepositoryTests.java | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java index 6961f784..1df63fc8 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java @@ -54,7 +54,7 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc private static final String[] DEFAULT_LOCATIONS = new String[] { "classpath:/", "classpath:/config/", "file:./", "file:./config/" }; - static final Pattern RESOURCE_PATTERN = Pattern.compile("Config resource '(.*?)' via location.*"); + static final Pattern RESOURCE_PATTERN = Pattern.compile("Config resource '(.*?)' via location '(.*)'"); private static Log logger = LogFactory.getLog(NativeEnvironmentRepository.class); @@ -226,12 +226,14 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc continue; } Matcher matcher = RESOURCE_PATTERN.matcher(name); + String location = null; if (matcher.find()) { name = matcher.group(1); + location = matcher.group(2); } // TODO: needed anymore? name = name.replace("applicationConfig: [", ""); - name = name.replace("file [", ""); + name = name.replace("file [", "file:"); name = name.replace("]", ""); if (this.searchLocations != null) { boolean matches = false; @@ -256,6 +258,15 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc matches = true; break; } + if (location.startsWith("file:")) { + location = StringUtils + .cleanPath(new File(location.substring("file:".length())).getAbsolutePath()) + "/"; + } + if (location != null && location.startsWith(pattern) + && !location.substring(pattern.length()).contains("/")) { + matches = true; + break; + } } if (!matches) { // Don't include this one: it wasn't matched by our search locations diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java index 9e37f3a9..60f094d6 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java @@ -102,6 +102,26 @@ public class NativeEnvironmentRepositoryTests { assertThat(environment.getPropertySources().get(0).getName().contains("application-dev.yml")); } + @Test + public void cleanBoot240Classpath() { + Environment environment = new Environment("application"); + environment.add(new PropertySource( + "Config resource 'classpath:/configs/application-myprofile.yml' via location 'classpath:/configs/' (document #0)", + Collections.singletonMap("foo", "bar"))); + assertThat(environment.getPropertySources().size()).isEqualTo(1); + assertThat(environment.getPropertySources().get(0).getName().contains("application-myprofile.yml")); + } + + @Test + public void cleanBoot241Classpath() { + Environment environment = new Environment("application"); + environment.add(new PropertySource( + "Config resource 'class path resource [configs/application.yml]' via location 'classpath:/configs/' (document #0)", + Collections.singletonMap("foo", "bar"))); + assertThat(environment.getPropertySources().size()).isEqualTo(1); + assertThat(environment.getPropertySources().get(0).getName().contains("application-myprofile.yml")); + } + @Test @Ignore // FIXME: configdata public void labelled() {