From 00a358b4d36f971b025399b8cafc7862f05fcc8f Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Wed, 17 Feb 2021 12:43:46 -0800 Subject: [PATCH] Throw or warn for invalid config properties with list syntax Fixes gh-25309 --- .../config/InvalidConfigDataPropertyException.java | 5 +++++ ...igDataEnvironmentPostProcessorIntegrationTests.java | 7 ++++--- .../InvalidConfigDataPropertyExceptionTests.java | 10 ++++++++++ ...ude-profiles-list-in-profile-specific-file-test.yml | 4 ++++ 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 spring-boot-project/spring-boot/src/test/resources/application-include-profiles-list-in-profile-specific-file-test.yml diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/InvalidConfigDataPropertyException.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/InvalidConfigDataPropertyException.java index 9e3e03af42..141dd469aa 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/InvalidConfigDataPropertyException.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/InvalidConfigDataPropertyException.java @@ -43,6 +43,8 @@ public class InvalidConfigDataPropertyException extends ConfigDataException { Map warnings = new LinkedHashMap<>(); warnings.put(ConfigurationPropertyName.of("spring.profiles"), ConfigurationPropertyName.of("spring.config.activate.on-profile")); + warnings.put(ConfigurationPropertyName.of("spring.profiles[0]"), + ConfigurationPropertyName.of("spring.config.activate.on-profile")); WARNINGS = Collections.unmodifiableMap(warnings); } @@ -50,8 +52,11 @@ public class InvalidConfigDataPropertyException extends ConfigDataException { static { Set errors = new LinkedHashSet<>(); errors.add(Profiles.INCLUDE_PROFILES); + errors.add(Profiles.INCLUDE_PROFILES.append("[0]")); errors.add(ConfigurationPropertyName.of(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME)); + errors.add(ConfigurationPropertyName.of(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME + "[0]")); errors.add(ConfigurationPropertyName.of(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME)); + errors.add(ConfigurationPropertyName.of(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME + "[0]")); PROFILE_SPECIFIC_ERRORS = Collections.unmodifiableSet(errors); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java index bba8dd5d12..7d7722103c 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java @@ -642,9 +642,10 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests { } @Test - void runWhenHasIncludedProfilesWithProfileSpecificFileThrowsException() { - assertThatExceptionOfType(InvalidConfigDataPropertyException.class).isThrownBy(() -> this.application - .run("--spring.config.name=application-include-profiles-in-profile-specific-file")); + void runWhenHasIncludedProfilesWithListSyntaxWithProfileSpecificDocumentThrowsException() { + assertThatExceptionOfType(InvalidConfigDataPropertyException.class).isThrownBy(() -> this.application.run( + "--spring.config.name=application-include-profiles-list-in-profile-specific-file", + "--spring.profiles.active=test")); } @Test diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java index e104a9719c..dd4af2946f 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/InvalidConfigDataPropertyExceptionTests.java @@ -169,6 +169,16 @@ class InvalidConfigDataPropertyExceptionTests { + "'spring.config.activate.on-profile' [origin: \"spring.profiles\" from property source \"mockProperties\"]"); } + @Test + void throwOrWarnWhenHasWarningPropertyWithListSyntaxLogsWarning() { + MockPropertySource propertySource = new MockPropertySource(); + propertySource.setProperty("spring.profiles[0]", "a"); + ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofExisting(propertySource); + InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor); + verify(this.logger).warn("Property 'spring.profiles[0]' is invalid and should be replaced with " + + "'spring.config.activate.on-profile' [origin: \"spring.profiles[0]\" from property source \"mockProperties\"]"); + } + private static class TestConfigDataResource extends ConfigDataResource { @Override diff --git a/spring-boot-project/spring-boot/src/test/resources/application-include-profiles-list-in-profile-specific-file-test.yml b/spring-boot-project/spring-boot/src/test/resources/application-include-profiles-list-in-profile-specific-file-test.yml new file mode 100644 index 0000000000..b6f2fc5558 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/application-include-profiles-list-in-profile-specific-file-test.yml @@ -0,0 +1,4 @@ +spring: + profiles: + include: + - p