Search in sources :

Example 1 with ConfigurationProperty

use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.

the class ConfigurationPropertiesReportEndpoint method sanitizeWithPropertySourceIfPresent.

private Object sanitizeWithPropertySourceIfPresent(String qualifiedKey, Object value) {
    ConfigurationPropertyName currentName = getCurrentName(qualifiedKey);
    ConfigurationProperty candidate = getCandidate(currentName);
    PropertySource<?> propertySource = getPropertySource(candidate);
    if (propertySource != null) {
        SanitizableData data = new SanitizableData(propertySource, qualifiedKey, value);
        return this.sanitizer.sanitize(data);
    }
    SanitizableData data = new SanitizableData(null, qualifiedKey, value);
    return this.sanitizer.sanitize(data);
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) ConfigurationPropertyName(org.springframework.boot.context.properties.source.ConfigurationPropertyName) SanitizableData(org.springframework.boot.actuate.endpoint.SanitizableData)

Example 2 with ConfigurationProperty

use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.

the class ConfigDataEnvironmentPostProcessorIntegrationTests method runWhenImportingIncludesParentOrigin.

@Test
void runWhenImportingIncludesParentOrigin() {
    ConfigurableApplicationContext context = this.application.run("--spring.config.location=classpath:application-import-with-placeholder.properties");
    Binder binder = Binder.get(context.getEnvironment());
    List<ConfigurationProperty> properties = new ArrayList<>();
    BindHandler bindHandler = new BindHandler() {

        @Override
        public Object onSuccess(ConfigurationPropertyName name, Bindable<?> target, BindContext context, Object result) {
            properties.add(context.getConfigurationProperty());
            return result;
        }
    };
    binder.bind("my.value", Bindable.of(String.class), bindHandler);
    assertThat(properties).hasSize(1);
    Origin origin = properties.get(0).getOrigin();
    assertThat(origin.toString()).contains("application-import-with-placeholder-imported");
    assertThat(origin.getParent().toString()).contains("application-import-with-placeholder");
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) BindContext(org.springframework.boot.context.properties.bind.BindContext) Origin(org.springframework.boot.origin.Origin) Binder(org.springframework.boot.context.properties.bind.Binder) ConfigurationPropertyName(org.springframework.boot.context.properties.source.ConfigurationPropertyName) ArrayList(java.util.ArrayList) Bindable(org.springframework.boot.context.properties.bind.Bindable) BindHandler(org.springframework.boot.context.properties.bind.BindHandler) Test(org.junit.jupiter.api.Test)

Example 3 with ConfigurationProperty

use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.

the class InvalidConfigDataPropertyExceptionTests method createWhenNoOriginHasCorrectMessage.

@Test
void createWhenNoOriginHasCorrectMessage() {
    ConfigurationProperty property = new ConfigurationProperty(this.invalid, "bad", null);
    assertThat(new InvalidConfigDataPropertyException(property, false, this.replacement, this.resource)).hasMessage("Property 'invalid' imported from location 'test' is invalid and should be replaced with 'replacement'");
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) Test(org.junit.jupiter.api.Test)

Example 4 with ConfigurationProperty

use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.

the class CollectionBinderTests method bindToCollectionWhenNonSequentialShouldThrowException.

@Test
void bindToCollectionWhenNonSequentialShouldThrowException() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo[0]", "2");
    source.put("foo[1]", "1");
    source.put("foo[3]", "3");
    this.sources.add(source);
    assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.binder.bind("foo", INTEGER_LIST)).satisfies((ex) -> {
        Set<ConfigurationProperty> unbound = ((UnboundConfigurationPropertiesException) ex.getCause()).getUnboundProperties();
        assertThat(unbound).hasSize(1);
        ConfigurationProperty property = unbound.iterator().next();
        assertThat(property.getName().toString()).isEqualTo("foo[3]");
        assertThat(property.getValue()).isEqualTo("3");
    });
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 5 with ConfigurationProperty

use of org.springframework.boot.context.properties.source.ConfigurationProperty in project spring-boot by spring-projects.

the class BindFailureAnalyzer method analyzeGenericBindException.

private FailureAnalysis analyzeGenericBindException(BindException cause) {
    StringBuilder description = new StringBuilder(String.format("%s:%n", cause.getMessage()));
    ConfigurationProperty property = cause.getProperty();
    buildDescription(description, property);
    description.append(String.format("%n    Reason: %s", getMessage(cause)));
    return getFailureAnalysis(description, cause);
}
Also used : ConfigurationProperty(org.springframework.boot.context.properties.source.ConfigurationProperty)

Aggregations

ConfigurationProperty (org.springframework.boot.context.properties.source.ConfigurationProperty)18 Test (org.junit.jupiter.api.Test)9 ConfigurationPropertyName (org.springframework.boot.context.properties.source.ConfigurationPropertyName)5 ArrayList (java.util.ArrayList)4 MockConfigurationPropertySource (org.springframework.boot.context.properties.source.MockConfigurationPropertySource)4 ConfigurationPropertySource (org.springframework.boot.context.properties.source.ConfigurationPropertySource)3 Origin (org.springframework.boot.origin.Origin)3 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 Map (java.util.Map)2 SanitizableData (org.springframework.boot.actuate.endpoint.SanitizableData)2 BindContext (org.springframework.boot.context.properties.bind.BindContext)2 Bindable (org.springframework.boot.context.properties.bind.Bindable)2 Binder (org.springframework.boot.context.properties.bind.Binder)2 ConverterNotFoundException (org.springframework.core.convert.ConverterNotFoundException)2 FieldError (org.springframework.validation.FieldError)2 ObjectError (org.springframework.validation.ObjectError)2 Valid (jakarta.validation.Valid)1 Min (jakarta.validation.constraints.Min)1 NotNull (jakarta.validation.constraints.NotNull)1