From b9abcf1615f30c7da0c778b46a2a8435e0baab2c Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Wed, 5 Aug 2020 14:41:32 -0700 Subject: [PATCH] Fail hard if spring.profiles.include is used with new config processing Closes gh-22693 --- .../src/docs/asciidoc/spring-boot-features.adoc | 16 ++-------------- .../InvalidConfigDataPropertyException.java | 6 +++++- .../InvalidConfigDataPropertyExceptionTests.java | 11 +++++++++++ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index 44e0060cb0e..6981e4375dc 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -1742,21 +1742,9 @@ The configprop:spring.profiles.active[] property follows the same ordering rules This means that you can specify active profiles in `application.properties` and then *replace* them by using the command line switch. Sometimes, it is useful to have properties that *add* to the active profiles rather than replace them. -The configprop:spring.profiles.include[] property can be used to unconditionally add active profiles. -The `SpringApplication` entry point also has a Java API for setting additional profiles (that is, on top of those activated by the configprop:spring.profiles.active[] property). +The `SpringApplication` entry point has a Java API for setting additional profiles (that is, on top of those activated by the configprop:spring.profiles.active[] property). See the `setAdditionalProfiles()` method in {spring-boot-module-api}/SpringApplication.html[SpringApplication]. - -For example, when an application with the following properties is run by using the switch, `--spring.profiles.active=custom`, the `other` and `extra` profiles are still activated: - -[source,yaml,indent=0] ----- - --- - my.property: fromyamlfile - spring.profiles.include: - - other - - extra ----- - +Profile groups, which are described in the <> can also be used to add active profiles if a given profile is active. [[boot-features-profiles-groups]] 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 79e3bba4ccd..21f4622b7c7 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 @@ -35,7 +35,7 @@ import org.springframework.boot.context.properties.source.ConfigurationPropertyS */ public class InvalidConfigDataPropertyException extends ConfigDataException { - private static final Map ERROR = Collections.emptyMap(); + private static final Map ERROR; private static final Map WARNING; static { @@ -43,6 +43,10 @@ public class InvalidConfigDataPropertyException extends ConfigDataException { warning.put(ConfigurationPropertyName.of("spring.profiles"), ConfigurationPropertyName.of("spring.config.activate.on-profile")); WARNING = Collections.unmodifiableMap(warning); + Map error = new LinkedHashMap<>(); + error.put(ConfigurationPropertyName.of("spring.profiles.include"), + ConfigurationPropertyName.of("spring.profiles.group")); + ERROR = Collections.unmodifiableMap(error); } private final ConfigurationProperty property; 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 3ce009caf5e..178303c0099 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 @@ -123,6 +123,17 @@ class InvalidConfigDataPropertyExceptionTests { + "'spring.config.activate.on-profile' [origin: \"spring.profiles\" from property source \"mockProperties\"]"); } + @Test + void throwOrWarnWhenHasErrorPropertyThrowsException() { + MockPropertySource propertySource = new MockPropertySource(); + propertySource.setProperty("spring.profiles.include", "a"); + ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofExisting(propertySource); + assertThatExceptionOfType(InvalidConfigDataPropertyException.class) + .isThrownBy(() -> InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor)) + .withMessage("Property 'spring.profiles.include' is invalid and should be replaced with " + + "'spring.profiles.group' [origin: \"spring.profiles.include\" from property source \"mockProperties\"]"); + } + private static class TestConfigDataLocation extends ConfigDataLocation { @Override