diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java index b3e89c50cf..92437839be 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java @@ -297,6 +297,8 @@ class ConfigDataEnvironment { binder.bind(Profiles.INCLUDE_PROFILES, STRING_LIST).ifBound((includes) -> { if (!contributor.isActive(activationContext)) { InactiveConfigDataAccessException.throwIfPropertyFound(contributor, Profiles.INCLUDE_PROFILES); + InactiveConfigDataAccessException.throwIfPropertyFound(contributor, + Profiles.INCLUDE_PROFILES.append("[0]")); } result.addAll(includes); }); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentTests.java index 81715f4e85..9b4430682f 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentTests.java @@ -27,6 +27,8 @@ import java.util.stream.Collectors; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInfo; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.DefaultBootstrapContext; @@ -216,6 +218,53 @@ class ConfigDataEnvironmentTests { assertThat(this.environment.getActiveProfiles()).containsExactly("test"); } + @ParameterizedTest + @CsvSource({ "include", "include[0]" }) + void processAndApplyWhenHasProfileIncludeInProfileSpecificDocumentThrowsException(String property, TestInfo info) { + this.environment.setProperty("spring.config.location", getConfigLocation(info)); + ConfigDataEnvironment configDataEnvironment = new ConfigDataEnvironment(this.logFactory, this.bootstrapContext, + this.environment, this.resourceLoader, this.additionalProfiles, null) { + + @Override + protected ConfigDataEnvironmentContributors createContributors( + List contributors) { + Map source = new LinkedHashMap<>(); + source.put("spring.config.activate.on-profile", "activate"); + source.put("spring.profiles." + property, "include"); + ConfigData data = new ConfigData(Collections.singleton(new MapPropertySource("test", source))); + contributors.add(ConfigDataEnvironmentContributor.ofUnboundImport(ConfigDataLocation.of("test"), + mock(ConfigDataResource.class), false, data, 0)); + return super.createContributors(contributors); + } + + }; + assertThatExceptionOfType(InactiveConfigDataAccessException.class) + .isThrownBy(configDataEnvironment::processAndApply); + } + + @ParameterizedTest + @CsvSource({ "spring.profiles.include", "spring.profiles.include[0]" }) + void processAndApplyIncludesProfilesFromSpringProfilesInclude(String property, TestInfo info) { + this.environment.setProperty("spring.config.location", getConfigLocation(info)); + ConfigDataEnvironment configDataEnvironment = new ConfigDataEnvironment(this.logFactory, this.bootstrapContext, + this.environment, this.resourceLoader, this.additionalProfiles, null) { + + @Override + protected ConfigDataEnvironmentContributors createContributors( + List contributors) { + Map source = new LinkedHashMap<>(); + source.put(property, "included"); + ConfigData data = new ConfigData(Collections.singleton(new MapPropertySource("test", source))); + contributors.add(ConfigDataEnvironmentContributor.ofUnboundImport(ConfigDataLocation.of("test"), + mock(ConfigDataResource.class), false, data, 0)); + return super.createContributors(contributors); + } + + }; + configDataEnvironment.processAndApply(); + assertThat(this.environment.getActiveProfiles()).containsExactly("included"); + } + @Test void processAndApplyDoesNotSetProfilesFromIgnoreProfilesContributorsWhenNoProfilesActive(TestInfo info) { this.environment.setProperty("spring.config.location", getConfigLocation(info));