Merge branch '3.3.x'

Closes gh-41237
This commit is contained in:
Phillip Webb 2024-06-25 17:08:35 -07:00
commit 28e6af5652
3 changed files with 29 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -69,17 +69,17 @@ class ConfigDataLocationResolvers {
@SuppressWarnings("rawtypes")
private List<ConfigDataLocationResolver<?>> reorder(List<ConfigDataLocationResolver> resolvers) {
List<ConfigDataLocationResolver<?>> reordered = new ArrayList<>(resolvers.size());
StandardConfigDataLocationResolver resourceResolver = null;
ConfigDataLocationResolver<?> standardConfigDataLocationResolver = null;
for (ConfigDataLocationResolver<?> resolver : resolvers) {
if (resolver instanceof StandardConfigDataLocationResolver configDataLocationResolver) {
resourceResolver = configDataLocationResolver;
if (resolver instanceof StandardConfigDataLocationResolver) {
standardConfigDataLocationResolver = resolver;
}
else {
reordered.add(resolver);
}
}
if (resourceResolver != null) {
reordered.add(resourceResolver);
if (standardConfigDataLocationResolver != null) {
reordered.add(standardConfigDataLocationResolver);
}
return Collections.unmodifiableList(reordered);
}

View File

@ -45,6 +45,7 @@ import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.log.LogMessage;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
/**
@ -231,8 +232,17 @@ public class StandardConfigDataLocationResolver
if (configDataLocation.isOptional()) {
return Collections.emptySet();
}
throw new IllegalStateException("File extension is not known to any PropertySourceLoader. "
+ "If the location is meant to reference a directory, it must end in '/' or File.separator");
if (configDataLocation.hasPrefix(PREFIX) || configDataLocation.hasPrefix(ResourceUtils.FILE_URL_PREFIX)
|| configDataLocation.hasPrefix(ResourceUtils.CLASSPATH_URL_PREFIX)
|| configDataLocation.toString().indexOf(':') == -1) {
throw new IllegalStateException("File extension is not known to any PropertySourceLoader. "
+ "If the location is meant to reference a directory, it must end in '/' or File.separator");
}
throw new IllegalStateException(
"Incorrect ConfigDataLocationResolver chosen or file extension is not known to any PropertySourceLoader. "
+ "If the location is meant to reference a directory, it must end in '/' or File.separator. "
+ "The location is being resolved using the StandardConfigDataLocationResolver, "
+ "check the location prefix if a different resolver is expected");
}
private String getLoadableFileExtension(PropertySourceLoader loader, String file) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -99,6 +99,16 @@ class StandardConfigDataLocationResolverTests {
.satisfies((ex) -> assertThat(ex.getCause()).hasMessageStartingWith("File extension is not known"));
}
@Test
void resolveWhenLocationHasUnknownPrefixAndNoMatchingLoaderThrowsException() {
ConfigDataLocation location = ConfigDataLocation
.of("typo:src/test/resources/configdata/properties/application.unknown");
assertThatIllegalStateException().isThrownBy(() -> this.resolver.resolve(this.context, location))
.withMessageStartingWith("Unable to load config data from")
.satisfies((ex) -> assertThat(ex.getCause()).hasMessageStartingWith(
"Incorrect ConfigDataLocationResolver chosen or file extension is not known to any PropertySourceLoader"));
}
@Test
void resolveWhenLocationWildcardIsSpecifiedForClasspathLocationThrowsException() {
ConfigDataLocation location = ConfigDataLocation.of("classpath*:application.properties");