parent
573fc34e29
commit
8d6b442dc4
|
@ -357,7 +357,7 @@ class ConfigDataEnvironment {
|
|||
|
||||
private void checkForInvalidProperties(ConfigDataEnvironmentContributors contributors) {
|
||||
for (ConfigDataEnvironmentContributor contributor : contributors) {
|
||||
InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor);
|
||||
InvalidConfigDataPropertyException.throwIfPropertyFound(contributor);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,12 +21,9 @@ import java.util.List;
|
|||
import java.util.function.Predicate;
|
||||
|
||||
import org.springframework.boot.cloud.CloudPlatform;
|
||||
import org.springframework.boot.context.properties.bind.BindContext;
|
||||
import org.springframework.boot.context.properties.bind.BindHandler;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.bind.Name;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationProperty;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
|
@ -40,13 +37,8 @@ class ConfigDataProperties {
|
|||
|
||||
private static final ConfigurationPropertyName NAME = ConfigurationPropertyName.of("spring.config");
|
||||
|
||||
private static final ConfigurationPropertyName LEGACY_PROFILES_NAME = ConfigurationPropertyName
|
||||
.of("spring.profiles");
|
||||
|
||||
private static final Bindable<ConfigDataProperties> BINDABLE_PROPERTIES = Bindable.of(ConfigDataProperties.class);
|
||||
|
||||
private static final Bindable<String[]> BINDABLE_STRING_ARRAY = Bindable.of(String[].class);
|
||||
|
||||
private final List<ConfigDataLocation> imports;
|
||||
|
||||
private final Activate activate;
|
||||
|
@ -87,13 +79,6 @@ class ConfigDataProperties {
|
|||
return new ConfigDataProperties(null, this.activate);
|
||||
}
|
||||
|
||||
ConfigDataProperties withLegacyProfiles(String[] legacyProfiles, ConfigurationProperty property) {
|
||||
if (this.activate != null && !ObjectUtils.isEmpty(this.activate.onProfile)) {
|
||||
throw new InvalidConfigDataPropertyException(property, false, NAME.append("activate.on-profile"), null);
|
||||
}
|
||||
return new ConfigDataProperties(this.imports, new Activate(this.activate.onCloudPlatform, legacyProfiles));
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method used to create {@link ConfigDataProperties} from the given
|
||||
* {@link Binder}.
|
||||
|
@ -101,37 +86,7 @@ class ConfigDataProperties {
|
|||
* @return a {@link ConfigDataProperties} instance or {@code null}
|
||||
*/
|
||||
static ConfigDataProperties get(Binder binder) {
|
||||
LegacyProfilesBindHandler legacyProfilesBindHandler = new LegacyProfilesBindHandler();
|
||||
String[] legacyProfiles = binder.bind(LEGACY_PROFILES_NAME, BINDABLE_STRING_ARRAY, legacyProfilesBindHandler)
|
||||
.orElse(null);
|
||||
ConfigDataProperties properties = binder.bind(NAME, BINDABLE_PROPERTIES, new ConfigDataLocationBindHandler())
|
||||
.orElse(null);
|
||||
if (!ObjectUtils.isEmpty(legacyProfiles)) {
|
||||
properties = (properties != null)
|
||||
? properties.withLegacyProfiles(legacyProfiles, legacyProfilesBindHandler.getProperty())
|
||||
: new ConfigDataProperties(null, new Activate(null, legacyProfiles));
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link BindHandler} used to check for legacy processing properties.
|
||||
*/
|
||||
private static class LegacyProfilesBindHandler implements BindHandler {
|
||||
|
||||
private ConfigurationProperty property;
|
||||
|
||||
@Override
|
||||
public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context,
|
||||
Object result) {
|
||||
this.property = context.getConfigurationProperty();
|
||||
return result;
|
||||
}
|
||||
|
||||
ConfigurationProperty getProperty() {
|
||||
return this.property;
|
||||
}
|
||||
|
||||
return binder.bind(NAME, BINDABLE_PROPERTIES, new ConfigDataLocationBindHandler()).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -22,8 +22,6 @@ import java.util.LinkedHashSet;
|
|||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
import org.springframework.boot.context.properties.source.ConfigurationProperty;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
||||
|
@ -38,14 +36,14 @@ import org.springframework.core.env.AbstractEnvironment;
|
|||
*/
|
||||
public class InvalidConfigDataPropertyException extends ConfigDataException {
|
||||
|
||||
private static final Map<ConfigurationPropertyName, ConfigurationPropertyName> WARNINGS;
|
||||
private static final Map<ConfigurationPropertyName, ConfigurationPropertyName> ERRORS;
|
||||
static {
|
||||
Map<ConfigurationPropertyName, ConfigurationPropertyName> warnings = new LinkedHashMap<>();
|
||||
warnings.put(ConfigurationPropertyName.of("spring.profiles"),
|
||||
Map<ConfigurationPropertyName, ConfigurationPropertyName> errors = new LinkedHashMap<>();
|
||||
errors.put(ConfigurationPropertyName.of("spring.profiles"),
|
||||
ConfigurationPropertyName.of("spring.config.activate.on-profile"));
|
||||
warnings.put(ConfigurationPropertyName.of("spring.profiles[0]"),
|
||||
errors.put(ConfigurationPropertyName.of("spring.profiles[0]"),
|
||||
ConfigurationPropertyName.of("spring.config.activate.on-profile"));
|
||||
WARNINGS = Collections.unmodifiableMap(warnings);
|
||||
ERRORS = Collections.unmodifiableMap(errors);
|
||||
}
|
||||
|
||||
private static final Set<ConfigurationPropertyName> PROFILE_SPECIFIC_ERRORS;
|
||||
|
@ -101,20 +99,18 @@ public class InvalidConfigDataPropertyException extends ConfigDataException {
|
|||
}
|
||||
|
||||
/**
|
||||
* Throw an {@link InvalidConfigDataPropertyException} or log a warning if the given
|
||||
* {@link ConfigDataEnvironmentContributor} contains any invalid property. A warning
|
||||
* is logged if the property is still supported, but not recommended. An error is
|
||||
* thrown if the property is completely unsupported.
|
||||
* @param logger the logger to use for warnings
|
||||
* Throw an {@link InvalidConfigDataPropertyException} if the given
|
||||
* {@link ConfigDataEnvironmentContributor} contains any invalid property.
|
||||
* @param contributor the contributor to check
|
||||
*/
|
||||
static void throwOrWarn(Log logger, ConfigDataEnvironmentContributor contributor) {
|
||||
static void throwIfPropertyFound(ConfigDataEnvironmentContributor contributor) {
|
||||
ConfigurationPropertySource propertySource = contributor.getConfigurationPropertySource();
|
||||
if (propertySource != null) {
|
||||
WARNINGS.forEach((name, replacement) -> {
|
||||
ERRORS.forEach((name, replacement) -> {
|
||||
ConfigurationProperty property = propertySource.getConfigurationProperty(name);
|
||||
if (property != null) {
|
||||
logger.warn(getMessage(property, false, replacement, contributor.getResource()));
|
||||
throw new InvalidConfigDataPropertyException(property, false, replacement,
|
||||
contributor.getResource());
|
||||
}
|
||||
});
|
||||
if (contributor.isFromProfileSpecificImport()
|
||||
|
|
|
@ -33,7 +33,6 @@ import org.apache.logging.log4j.util.Strings;
|
|||
import org.assertj.core.api.Condition;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
|
@ -161,7 +160,8 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests {
|
|||
@Test
|
||||
void runWhenActiveProfilesDoesNotLoadDefault() {
|
||||
ConfigurableApplicationContext context = this.application.run("--spring.config.name=testprofilesdocument",
|
||||
"--spring.profiles.default=thedefault", "--spring.profiles.active=other");
|
||||
"--spring.config.location=classpath:configdata/profiles/", "--spring.profiles.default=thedefault",
|
||||
"--spring.profiles.active=other");
|
||||
String property = context.getEnvironment().getProperty("my.property");
|
||||
assertThat(property).isEqualTo("fromotherprofile");
|
||||
}
|
||||
|
@ -591,7 +591,6 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
@Disabled("Disabled until spring.profiles suppport is dropped")
|
||||
void runWhenUsingInvalidPropertyThrowsException() {
|
||||
assertThatExceptionOfType(InvalidConfigDataPropertyException.class).isThrownBy(
|
||||
() -> this.application.run("--spring.config.location=classpath:invalidproperty.properties"));
|
||||
|
|
|
@ -29,7 +29,6 @@ import org.springframework.boot.context.properties.source.MapConfigurationProper
|
|||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests for {@link ConfigDataProperties}.
|
||||
|
@ -193,27 +192,6 @@ class ConfigDataPropertiesTests {
|
|||
assertThat(properties.isActive(context)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void isActiveWhenBindingToLegacyProperty() {
|
||||
MapConfigurationPropertySource source = new MapConfigurationPropertySource();
|
||||
source.put("spring.profiles", "a,b");
|
||||
Binder binder = new Binder(source);
|
||||
ConfigDataProperties properties = ConfigDataProperties.get(binder);
|
||||
ConfigDataActivationContext context = new ConfigDataActivationContext(CloudPlatform.KUBERNETES,
|
||||
createTestProfiles());
|
||||
assertThat(properties.isActive(context)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWhenHasLegacyAndNewPropertyThrowsException() {
|
||||
MapConfigurationPropertySource source = new MapConfigurationPropertySource();
|
||||
source.put("spring.profiles", "a,b");
|
||||
source.put("spring.config.activate.on-profile", "a | b");
|
||||
Binder binder = new Binder(source);
|
||||
assertThatExceptionOfType(InvalidConfigDataPropertyException.class)
|
||||
.isThrownBy(() -> ConfigDataProperties.get(binder));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getImportOriginWhenCommaListReturnsOrigin() {
|
||||
MapConfigurationPropertySource source = new MapConfigurationPropertySource();
|
||||
|
|
|
@ -16,8 +16,6 @@
|
|||
|
||||
package org.springframework.boot.context.config;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.context.config.ConfigDataEnvironmentContributor.Kind;
|
||||
|
@ -30,8 +28,6 @@ import org.springframework.mock.env.MockPropertySource;
|
|||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatNoException;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link InvalidConfigDataPropertyException}.
|
||||
|
@ -49,8 +45,6 @@ class InvalidConfigDataPropertyExceptionTests {
|
|||
|
||||
private ConfigurationProperty property = new ConfigurationProperty(this.invalid, "bad", MockOrigin.of("origin"));
|
||||
|
||||
private Log logger = mock(Log.class);
|
||||
|
||||
@Test
|
||||
void createHasCorrectMessage() {
|
||||
assertThat(new InvalidConfigDataPropertyException(this.property, false, this.replacement, this.resource))
|
||||
|
@ -106,13 +100,12 @@ class InvalidConfigDataPropertyExceptionTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
@Disabled("Disabled until spring.profiles support is dropped")
|
||||
void throwOrWarnWhenHasInvalidPropertyThrowsException() {
|
||||
MockPropertySource propertySource = new MockPropertySource();
|
||||
propertySource.setProperty("spring.profiles", "a");
|
||||
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofExisting(propertySource);
|
||||
assertThatExceptionOfType(InvalidConfigDataPropertyException.class)
|
||||
.isThrownBy(() -> InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor))
|
||||
.isThrownBy(() -> InvalidConfigDataPropertyException.throwIfPropertyFound(contributor))
|
||||
.withMessageStartingWith("Property 'spring.profiles' is invalid and should be replaced with "
|
||||
+ "'spring.config.activate.on-profile'");
|
||||
}
|
||||
|
@ -128,14 +121,13 @@ class InvalidConfigDataPropertyExceptionTests {
|
|||
void throwOrWarnWhenWhenHasInvalidProfileSpecificPropertyOnIgnoringProfilesContributorDoesNotThrowException() {
|
||||
ConfigDataEnvironmentContributor contributor = createInvalidProfileSpecificPropertyContributor(
|
||||
"spring.profiles.active", ConfigData.Option.IGNORE_PROFILES);
|
||||
assertThatNoException()
|
||||
.isThrownBy(() -> InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor));
|
||||
assertThatNoException().isThrownBy(() -> InvalidConfigDataPropertyException.throwIfPropertyFound(contributor));
|
||||
}
|
||||
|
||||
private void throwOrWarnWhenWhenHasInvalidProfileSpecificPropertyThrowsException(String name) {
|
||||
ConfigDataEnvironmentContributor contributor = createInvalidProfileSpecificPropertyContributor(name);
|
||||
assertThatExceptionOfType(InvalidConfigDataPropertyException.class)
|
||||
.isThrownBy(() -> InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor))
|
||||
.isThrownBy(() -> InvalidConfigDataPropertyException.throwIfPropertyFound(contributor))
|
||||
.withMessageStartingWith("Property '" + name + "' is invalid in a profile specific resource");
|
||||
}
|
||||
|
||||
|
@ -153,27 +145,7 @@ class InvalidConfigDataPropertyExceptionTests {
|
|||
void throwOrWarnWhenHasNoInvalidPropertyDoesNothing() {
|
||||
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor
|
||||
.ofExisting(new MockPropertySource());
|
||||
InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor);
|
||||
}
|
||||
|
||||
@Test
|
||||
void throwOrWarnWhenHasWarningPropertyLogsWarning() {
|
||||
MockPropertySource propertySource = new MockPropertySource();
|
||||
propertySource.setProperty("spring.profiles", "a");
|
||||
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofExisting(propertySource);
|
||||
InvalidConfigDataPropertyException.throwOrWarn(this.logger, contributor);
|
||||
then(this.logger).should().warn("Property 'spring.profiles' is invalid and should be replaced with "
|
||||
+ "'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);
|
||||
then(this.logger).should().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\"]");
|
||||
InvalidConfigDataPropertyException.throwIfPropertyFound(contributor);
|
||||
}
|
||||
|
||||
private static class TestConfigDataResource extends ConfigDataResource {
|
||||
|
|
|
@ -1 +1 @@
|
|||
spring.profile=a
|
||||
spring.profiles=a
|
|
@ -3,12 +3,10 @@ my:
|
|||
property: fromyamlfile
|
||||
other: notempty
|
||||
---
|
||||
spring:
|
||||
profiles: dev
|
||||
spring.config.activate.on-profile: dev
|
||||
my:
|
||||
property: fromdevprofile
|
||||
---
|
||||
spring:
|
||||
profiles: other
|
||||
spring.config.activate.on-profile: other
|
||||
my:
|
||||
property: fromotherprofile
|
||||
|
|
Loading…
Reference in New Issue