Skip to content

Commit 00a358b

Browse files
committed
Throw or warn for invalid config properties with list syntax
Fixes gh-25309
1 parent 6c0ec10 commit 00a358b

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/InvalidConfigDataPropertyException.java

+5
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,20 @@ public class InvalidConfigDataPropertyException extends ConfigDataException {
4343
Map<ConfigurationPropertyName, ConfigurationPropertyName> warnings = new LinkedHashMap<>();
4444
warnings.put(ConfigurationPropertyName.of("spring.profiles"),
4545
ConfigurationPropertyName.of("spring.config.activate.on-profile"));
46+
warnings.put(ConfigurationPropertyName.of("spring.profiles[0]"),
47+
ConfigurationPropertyName.of("spring.config.activate.on-profile"));
4648
WARNINGS = Collections.unmodifiableMap(warnings);
4749
}
4850

4951
private static final Set<ConfigurationPropertyName> PROFILE_SPECIFIC_ERRORS;
5052
static {
5153
Set<ConfigurationPropertyName> errors = new LinkedHashSet<>();
5254
errors.add(Profiles.INCLUDE_PROFILES);
55+
errors.add(Profiles.INCLUDE_PROFILES.append("[0]"));
5356
errors.add(ConfigurationPropertyName.of(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME));
57+
errors.add(ConfigurationPropertyName.of(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME + "[0]"));
5458
errors.add(ConfigurationPropertyName.of(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME));
59+
errors.add(ConfigurationPropertyName.of(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME + "[0]"));
5560
PROFILE_SPECIFIC_ERRORS = Collections.unmodifiableSet(errors);
5661
}
5762

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -642,9 +642,10 @@ void runWhenHasIncludedProfilesWithProfileSpecificDocumentThrowsException() {
642642
}
643643

644644
@Test
645-
void runWhenHasIncludedProfilesWithProfileSpecificFileThrowsException() {
646-
assertThatExceptionOfType(InvalidConfigDataPropertyException.class).isThrownBy(() -> this.application
647-
.run("--spring.config.name=application-include-profiles-in-profile-specific-file"));
645+
void runWhenHasIncludedProfilesWithListSyntaxWithProfileSpecificDocumentThrowsException() {
646+
assertThatExceptionOfType(InvalidConfigDataPropertyException.class).isThrownBy(() -> this.application.run(
647+
"--spring.config.name=application-include-profiles-list-in-profile-specific-file",
648+
"--spring.profiles.active=test"));
648649
}
649650

650651
@Test

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ void throwOrWarnWhenHasWarningPropertyLogsWarning() {
169169
+ "'spring.config.activate.on-profile' [origin: \"spring.profiles\" from property source \"mockProperties\"]");
170170
}
171171

172+
@Test
173+
void throwOrWarnWhenHasWarningPropertyWithListSyntaxLogsWarning() {
174+
MockPropertySource propertySource = new MockPropertySource();
175+
propertySource.setProperty("spring.profiles[0]", "a");
176+
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofExisting(propertySource);
177+
InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor);
178+
verify(this.logger).warn("Property 'spring.profiles[0]' is invalid and should be replaced with "
179+
+ "'spring.config.activate.on-profile' [origin: \"spring.profiles[0]\" from property source \"mockProperties\"]");
180+
}
181+
172182
private static class TestConfigDataResource extends ConfigDataResource {
173183

174184
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring:
2+
profiles:
3+
include:
4+
- p

0 commit comments

Comments
 (0)