Skip to content

Commit

Permalink
Throw or warn for invalid config properties with list syntax
Browse files Browse the repository at this point in the history
Fixes gh-25309
  • Loading branch information
mbhave committed Feb 17, 2021
1 parent 6c0ec10 commit 00a358b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
Expand Up @@ -43,15 +43,20 @@ public class InvalidConfigDataPropertyException extends ConfigDataException {
Map<ConfigurationPropertyName, ConfigurationPropertyName> 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);
}

private static final Set<ConfigurationPropertyName> PROFILE_SPECIFIC_ERRORS;
static {
Set<ConfigurationPropertyName> 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);
}

Expand Down
Expand Up @@ -642,9 +642,10 @@ void runWhenHasIncludedProfilesWithProfileSpecificDocumentThrowsException() {
}

@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
Expand Down
Expand Up @@ -169,6 +169,16 @@ void throwOrWarnWhenHasWarningPropertyLogsWarning() {
+ "'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
Expand Down
@@ -0,0 +1,4 @@
spring:
profiles:
include:
- p

0 comments on commit 00a358b

Please sign in to comment.